Skip to content

Commit

Permalink
Updated to use PER CS 2.0 (#327)
Browse files Browse the repository at this point in the history
* Updated chained methods to be on separate lines

* Updated short closure CS

* Removed empty class/interface/method bodies

* Updated CHANGELOG
  • Loading branch information
davidbyoung authored Jan 4, 2025
1 parent 7994835 commit 3fe7525
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

return new PhpCsFixer\Config()
->setRules([
'@PSR12' => true,
'@PER-CS2.0' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'compact_nullable_type_declaration' => true,
Expand Down
5 changes: 1 addition & 4 deletions src/CircularDependencyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
/**
* Defines an exception that's thrown when a circular dependency is detected
*/
final class CircularDependencyException extends Exception
{
// Don't do anything
}
final class CircularDependencyException extends Exception {}
3 changes: 1 addition & 2 deletions src/ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ public function __construct(
public mixed $rootValue,
public ?string $propertyName = null,
public ?string $methodName = null
) {
}
) {}
}
4 changes: 1 addition & 3 deletions src/Constraints/Attributes/ConstraintAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ abstract class ConstraintAttribute implements IConstraintAttribute
/**
* @param string|null $errorMessageId The error message ID, or null if there is none
*/
protected function __construct(public readonly ?string $errorMessageId)
{
}
protected function __construct(public readonly ?string $errorMessageId) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ final class FileObjectConstraintsRegistryCache implements IObjectConstraintsRegi
/**
* @param string $path The path to the cache file
*/
public function __construct(private readonly string $path)
{
}
public function __construct(private readonly string $path) {}

/**
* @inheritdoc
Expand Down
4 changes: 1 addition & 3 deletions src/Constraints/ObjectConstraintsRegistrantCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class ObjectConstraintsRegistrantCollection implements IObjectConstraintsRegistr
/**
* @param IObjectConstraintsRegistryCache|null $objectConstraintsCache The optional cache of constraints
*/
public function __construct(private readonly ?IObjectConstraintsRegistryCache $objectConstraintsCache = null)
{
}
public function __construct(private readonly ?IObjectConstraintsRegistryCache $objectConstraintsCache = null) {}

/**
* Adds a registrant to the collection
Expand Down
5 changes: 1 addition & 4 deletions src/ErrorMessages/ErrorMessageInterpolationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
/**
* Defines an exception that occurs if an error message could not be interpolated
*/
final class ErrorMessageInterpolationException extends Exception
{
// Don't do anything
}
final class ErrorMessageInterpolationException extends Exception {}
3 changes: 1 addition & 2 deletions src/ErrorMessages/StringReplaceErrorMessageInterpolator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ final class StringReplaceErrorMessageInterpolator implements IErrorMessageInterp
*/
public function __construct(
private readonly IErrorMessageTemplateRegistry $errorMessageTemplates = new DefaultErrorMessageTemplateRegistry()
) {
}
) {}

/**
* @inheritdoc
Expand Down
4 changes: 1 addition & 3 deletions src/ObjectConstraintsBuilderRegistrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class ObjectConstraintsBuilderRegistrant implements IObjectConstraintsRegi
/**
* @param array<Closure(ObjectConstraintsRegistryBuilder): void> $closures The list of closures to execute (must take in an ObjectConstraintsRegistryBuilder parameter)
*/
public function __construct(private array $closures)
{
}
public function __construct(private array $closures) {}

/**
* @inheritdoc
Expand Down
3 changes: 1 addition & 2 deletions src/ObjectConstraintsRegistryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ final class ObjectConstraintsRegistryBuilder
*/
public function __construct(
private readonly ObjectConstraintsRegistry $objectConstraints = new ObjectConstraintsRegistry()
) {
}
) {}

/**
* Builds the object constraints
Expand Down
3 changes: 1 addition & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ final class Validator implements IValidator
public function __construct(
private readonly ObjectConstraintsRegistry $objectConstraints,
private readonly IErrorMessageInterpolator $errorMessageInterpolator = new StringReplaceErrorMessageInterpolator()
) {
}
) {}

/**
* @inheritdoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function method(): bool
return true;
}
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand All @@ -65,7 +66,8 @@ public function method(): bool
return true;
}
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand All @@ -85,7 +87,8 @@ public function method(): bool
return true;
}
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand All @@ -104,7 +107,8 @@ public function testMultiplePropertyConstraintAttributesCanBeRegistered(): void
#[Required, Email]
public bool $prop = true;
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand All @@ -123,7 +127,8 @@ public function testPropertiesWithConstraintsAreRegistered(): void
#[Required]
public bool $prop = true;
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand All @@ -141,7 +146,8 @@ public function testPropertiesWithNonValidationConstraintAttributesAreNotRegiste
#[NonConstraintAttribute]
public bool $prop = true;
};
$this->typeFinder->expects($this->once())
$this->typeFinder
->expects($this->once())
->method('findAllClasses')
->with([self::PATH])
->willReturn([$object::class]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Constraints/CallbackConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public function testCallbackReturnValueIsRespected(): void

public function testGettingErrorMessageId(): void
{
$constraint = new CallbackConstraint(fn (): bool => true, 'foo');
$constraint = new CallbackConstraint(fn(): bool => true, 'foo');
$this->assertSame('foo', $constraint->errorMessageId);
}

public function testGettingErrorMessagePlaceholdersIncludesValue(): void
{
$this->assertEquals(['value' => 'val'], (new CallbackConstraint(fn (mixed $value): bool => true))->getErrorMessagePlaceholders('val'));
$this->assertEquals(['value' => 'val'], (new CallbackConstraint(fn(mixed $value): bool => true))->getErrorMessagePlaceholders('val'));
}
}
3 changes: 1 addition & 2 deletions tests/Constraints/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public function __toString(): string

public function testGettingErrorMessagePlaceholderForUnserializableObjectsUsesClassName(): void
{
$value = new class () {
};
$value = new class () {};
$this->assertEquals(
['value' => $value::class . ' object'],
$this->constraint->getErrorMessagePlaceholders($value)
Expand Down
30 changes: 20 additions & 10 deletions tests/Constraints/EachConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ public function testGettingErrorMessagePlaceholdersIncludesValue(): void
public function testMultipleConstraintsAreAccepted(): void
{
$constraint1 = $this->createMock(IConstraint::class);
$constraint1->expects($this->once())
$constraint1
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
$constraint2 = $this->createMock(IConstraint::class);
$constraint2->expects($this->once())
$constraint2
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
Expand All @@ -46,12 +48,14 @@ public function testMultipleConstraintsAreAccepted(): void
public function testPassesOnAllPassedConstraintsReturnsTrue(): void
{
$constraint1 = $this->createMock(IConstraint::class);
$constraint1->expects($this->once())
$constraint1
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
$constraint2 = $this->createMock(IConstraint::class);
$constraint2->expects($this->once())
$constraint2
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
Expand All @@ -62,7 +66,8 @@ public function testPassesOnAllPassedConstraintsReturnsTrue(): void
public function testPassesOnEmptyValueReturnsTrue(): void
{
$constraint = $this->createMock(IConstraint::class);
$constraint->expects($this->never())
$constraint
->expects($this->never())
->method('passes');
$eachConstraint = new EachConstraint($constraint, 'foo');
$this->assertTrue($eachConstraint->passes([]));
Expand All @@ -71,12 +76,14 @@ public function testPassesOnEmptyValueReturnsTrue(): void
public function testPassesOnFailedConstraintDoesNotCallSecondConstraint(): void
{
$constraint1 = $this->createMock(IConstraint::class);
$constraint1->expects($this->once())
$constraint1
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(false);
$constraint2 = $this->createMock(IConstraint::class);
$constraint2->expects($this->never())
$constraint2
->expects($this->never())
->method('passes');
$eachConstraint = new EachConstraint([$constraint1, $constraint2], 'foo');
$this->assertFalse($eachConstraint->passes(['foo']));
Expand All @@ -93,12 +100,14 @@ public function testPassesOnNonIterableValueThrowsException(): void
public function testPassesOnPassedAndFailedConstraintsReturnsFalse(): void
{
$constraint1 = $this->createMock(IConstraint::class);
$constraint1->expects($this->once())
$constraint1
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
$constraint2 = $this->createMock(IConstraint::class);
$constraint2->expects($this->once())
$constraint2
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(false);
Expand All @@ -109,7 +118,8 @@ public function testPassesOnPassedAndFailedConstraintsReturnsFalse(): void
public function testSingleConstraintIsAccepted(): void
{
$constraint = $this->createMock(IConstraint::class);
$constraint->expects($this->once())
$constraint
->expects($this->once())
->method('passes')
->with('foo')
->willReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function testCacheHitCopiesCachedConstraintsIntoParameterConstraints(): v
$cachedConstraints = new ObjectConstraintsRegistry();
$cachedConstraints->registerObjectConstraints(new ObjectConstraints(self::class));
$cache = $this->createMock(IObjectConstraintsRegistryCache::class);
$cache->method('get')
$cache
->method('get')
->willReturn($cachedConstraints);
$collection = new ObjectConstraintsRegistrantCollection($cache);
$paramConstraints = new ObjectConstraintsRegistry();
Expand All @@ -58,9 +59,11 @@ public function testCacheMissPopulatesCache(): void
{
$expectedObjectConstraints = new ObjectConstraintsRegistry();
$cache = $this->createMock(IObjectConstraintsRegistryCache::class);
$cache->method('get')
$cache
->method('get')
->willReturn(null);
$cache->method('set')
$cache
->method('set')
->with($expectedObjectConstraints);
$collection = new ObjectConstraintsRegistrantCollection($cache);
$collection->registerConstraints($expectedObjectConstraints);
Expand Down
3 changes: 2 additions & 1 deletion tests/Constraints/RequiredConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function testEmptyArrayFails(): void
$constraint = new RequiredConstraint('foo');
$this->assertFalse($constraint->passes([]));
$countable = $this->createMock(Countable::class);
$countable->expects($this->once())
$countable
->expects($this->once())
->method('count')
->willReturn(0);
$this->assertFalse($constraint->passes($countable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function testInterpolatingCorrectlyFormatsIcuFormattedErrorMessageIdWithP
public function testInterpolatingGetsErrorMessageTemplateFromRegistry(): void
{
$errorMessageTemplates = $this->createMock(IErrorMessageTemplateRegistry::class);
$errorMessageTemplates->expects($this->once())
$errorMessageTemplates
->expects($this->once())
->method('getErrorMessageTemplate')
->with('foo', 'en-US')
->willReturn('bar');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function testErrorMessageIdWithNoPlaceholdersIsReturnedIntact(): void
public function testInterpolatingGetsErrorMessageTemplateFromRegistry(): void
{
$errorMessageTemplates = $this->createMock(IErrorMessageTemplateRegistry::class);
$errorMessageTemplates->expects($this->once())
$errorMessageTemplates
->expects($this->once())
->method('getErrorMessageTemplate')
->with('foo', 'en-US')
->willReturn('bar');
Expand Down
3 changes: 2 additions & 1 deletion tests/ObjectConstraintsBuilderRegistrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function testRegisteringConstraintsRegistersConstraintsFromClosures(): vo
{
$expectedConstraint = $this->createMock(IConstraint::class);
$closures = [function (ObjectConstraintsRegistryBuilder $objectConstraintsRegistryBuilder) use ($expectedConstraint): void {
$objectConstraintsRegistryBuilder->class(self::class)
$objectConstraintsRegistryBuilder
->class(self::class)
->hasPropertyConstraints('prop', $expectedConstraint);
}];
$closureConstraintRegistrant = new ObjectConstraintsBuilderRegistrant($closures);
Expand Down
12 changes: 8 additions & 4 deletions tests/ObjectConstraintsBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,35 @@ public function testBuiltConstraintsHasCorrectClassName(): void
public function testHasMethodConstraintsWithMultipleConstraintsAddsThemToRegistry(): void
{
$expectedMethodConstraints = [$this->createMock(IConstraint::class), $this->createMock(IConstraint::class)];
$objectConstraints = $this->builder->hasMethodConstraints('method', $expectedMethodConstraints)
$objectConstraints = $this->builder
->hasMethodConstraints('method', $expectedMethodConstraints)
->build();
$this->assertSame($expectedMethodConstraints, $objectConstraints->getConstraintsForMethod('method'));
}

public function testHasMethodConstraintsWithSingleConstraintAddsItToRegistry(): void
{
$expectedMethodConstraint = $this->createMock(IConstraint::class);
$objectConstraints = $this->builder->hasMethodConstraints('method', $expectedMethodConstraint)
$objectConstraints = $this->builder
->hasMethodConstraints('method', $expectedMethodConstraint)
->build();
$this->assertSame([$expectedMethodConstraint], $objectConstraints->getConstraintsForMethod('method'));
}

public function testHasPropertyConstraintsWithMultipleConstraintsAddsThemToRegistry(): void
{
$expectedPropertyConstraints = [$this->createMock(IConstraint::class), $this->createMock(IConstraint::class)];
$objectConstraints = $this->builder->hasPropertyConstraints('prop', $expectedPropertyConstraints)
$objectConstraints = $this->builder
->hasPropertyConstraints('prop', $expectedPropertyConstraints)
->build();
$this->assertSame($expectedPropertyConstraints, $objectConstraints->getConstraintsForProperty('prop'));
}

public function testHasPropertyConstraintsWithSingleConstraintAddsItToRegistry(): void
{
$expectedPropertyConstraint = $this->createMock(IConstraint::class);
$objectConstraints = $this->builder->hasPropertyConstraints('prop', $expectedPropertyConstraint)
$objectConstraints = $this->builder
->hasPropertyConstraints('prop', $expectedPropertyConstraint)
->build();
$this->assertSame([$expectedPropertyConstraint], $objectConstraints->getConstraintsForProperty('prop'));
}
Expand Down
6 changes: 2 additions & 4 deletions tests/ObjectConstraintsRegistryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public function testBuildWithInjectedRegistryRegistersConstraintsToIt(): void

public function testBuildWithMultipleSubBuildersCreatesRegistryWithMultipleObjectConstraints(): void
{
$foo = new class () {
};
$bar = new class () {
};
$foo = new class () {};
$bar = new class () {};
$expectedObjectConstraints = new ObjectConstraintsRegistry();
$expectedObjectConstraints->registerObjectConstraints(new ObjectConstraints($foo::class));
$expectedObjectConstraints->registerObjectConstraints(new ObjectConstraints($bar::class));
Expand Down
Loading

0 comments on commit 3fe7525

Please sign in to comment.