Skip to content

Instantly share code, notes, and snippets.

Avatar
🔬
In your repositories, watching your code. Always watching.

Marco Pivetta Ocramius

🔬
In your repositories, watching your code. Always watching.
View GitHub Profile
@Ocramius
Ocramius / handling-optional-input-fields-with-type-safe-abstractions.md
Last active Nov 17, 2022
Handling optional input parameters in PHP with `vimeo/psalm` and `azjezz/psl`
View handling-optional-input-fields-with-type-safe-abstractions.md

Handling optional input parameters in PHP with vimeo/psalm and azjezz/psl

I had an interesting use-case with a customer for which I provide consulting services: they needed multiple fields to be marked as "optional".

Example: updating a user

We will take a CRUD-ish example, for the sake of simplicity.

For example, in the following scenario, does a null $description mean "remove the description",

View git-flow_vs_github-flow.md

Git-flow vs GitHub-flow

What we want

A list of requirements:

  • stakeholders expect a list of provided features, every few days, in a human-friendly report
  • every change must have been reviewed, before being deployed
  • every change must have passed our automated checks, before being deployed
  • every change must have been verified by QA staff, before being deployed
@Ocramius
Ocramius / healthy-git-flow.sh
Created Oct 7, 2022
A healthy git-flow (if you can call git-flow healthy at all), in which `master` is correctly merged back to `develop`
View healthy-git-flow.sh
git checkout -b master
git log -1
# commit 65345471c1040ceb90b1817d7427d58d7b09fdca (HEAD -> master)
git checkout -b develop
git log -1
# both branches are at the same ref:
# commit 65345471c1040ceb90b1817d7427d58d7b09fdca (HEAD -> develop, master)
echo "a feature" > feature.txt
@Ocramius
Ocramius / check-source-baseline-size.sh
Last active Nov 18, 2022
Script to check whether `psalm-baseline.xml` improved/worsened over time
View check-source-baseline-size.sh
#!/usr/bin/env bash
export TARGET_REF=${TARGET_REF:-HEAD}
git show "$TARGET_REF:psalm-baseline.xml" | xmllint --xpath '//file[not(starts-with(@src, "test"))]//@occurrences' - | awk -F\" '{ print $2 }' | awk '{s+=$1} END {print s}'
View ClearAllNonPhpunitProperties.php
<?php
declare(strict_types=1);
namespace Tests\Integration;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;
use Webmozart\Assert\Assert;
@Ocramius
Ocramius / Day.php
Created Apr 3, 2020
Example abstraction that only allows a resolution of "day" for time inside a specific business domain
View Day.php
<?php
declare(strict_types=1);
namespace Core\Domain\Time;
use DateTimeImmutable;
use DateTimeZone;
use Generator;
use Iterator;
@Ocramius
Ocramius / psalm-compendium.php
Last active Oct 31, 2022
A small compendium of what is possible with `vimeo/psalm` 3.9.x to add some decent type system features to PHP
View psalm-compendium.php
<?php
// -- types are a compile-time propagated concept
// https://psalm.dev/r/338f74a96c
class TheType
{
/** @var string */
public $foo = 'bar';
}
View wait-for-elasticsearch.php
#!/usr/bin/env php
<?php
declare(strict_types=1);
namespace WaitForElasticsearch;
use InvalidArgumentException;
use UnexpectedValueException;
use function curl_close;
View EventSourcedAggregateContext.php
<?php
declare(strict_types=1);
namespace Test\Feature;
use Shared\Domain\DomainEvent;
use Shared\Domain\EventSourcedAggregateRoot;
use Webmozart\Assert\Assert;
use function array_shift;
@Ocramius
Ocramius / compiler-pass.php
Last active Mar 16, 2020
Compiler pass to declare a repository as service for each declared doctrine entity in a symfony application
View compiler-pass.php
<?php
final class RegisterRepositoriesForAllEntitiesPass implements CompilerPassInterface
{
/** {@inheritdoc} */
public function process(ContainerBuilder $container)
{
/** @var MappingDriver $metadata */
$metadata = $container->get($this->mappingDriverServiceName());
$entityManagerReference = new Reference('doctrine.orm.entity_manager');