TIL Week 18 2022

A roundup of the things I learned in the 18th week of 2022.

Technical

  • PHP Named Parameters are a good way of making different versions of an object you want to test that has a lot of constructor parameters. In your test you can add a method that returns your object with the constructor arguments as nullable parameters, then default them. Then when you need to pass different dependencies you can pass only the ones you want to modify as named parameters. Mini example:
private function makeObjectToTest( ?string $name, ?string $description, ?SomeDependency $someDependency ): ObjectToTest {
    return new ObjectToTest(
        $name ?? 'Default',
        $description ?? 'Default',
        $someDependency ?? new SomeDependency()
    );
}

$objectWithCustomName = $this->makeObjectToTest( name: 'Name override' );
$objectWithCustomDependency = $this->makeObjectToTest( someDependency: new SomeDependencySpy() );

Professional

Personal Notes

Coming soon to a post near you