Skip to content

Commit

Permalink
Update phpstan to v2 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
bram123 authored Dec 30, 2024
1 parent 4761b75 commit 317cc8f
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 52 deletions.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"require-dev": {
"phpmd/phpmd": "@stable",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"roave/security-advisories": "dev-latest",
"squizlabs/php_codesniffer": "^3.6"
},
Expand All @@ -37,6 +37,9 @@
}
},
"scripts": {
"baseline": ["@baseline:phpstan", "@baseline:phpmd"],
"baseline:phpstan": "phpstan --generate-baseline",
"baseline:phpmd": "phpmd src,tests xml phpmd.xml.dist --generate-baseline",
"check": ["@check:phpstan", "@check:phpmd", "@check:phpcs"],
"check:phpstan": "phpstan analyse",
"check:phpmd": "phpmd src,tests text phpmd.xml.dist --suffixes php --exclude /tests/*/data/*",
Expand Down
16 changes: 16 additions & 0 deletions phpmd.baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<phpmd-baseline>
<violation rule="PHPMD\Rule\Design\EvalExpression" file="src/Constraint/ValueProvider/Compound/IntersectionProvider.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/Constraint/ValueProvider/NativeValueProviderFactory.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/Constraint/ValueProvider/PseudoValueProviderFactory.php"/>
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Constraint/ValueProvider/PseudoValueProviderFactory.php" method="getPseudoStringProvider"/>
<violation rule="PHPMD\Rule\Design\NumberOfChildren" file="tests/Unit/Constraint/MethodPair/AbstractDataClass.php"/>
<violation rule="PHPMD\Rule\UnusedPrivateMethod" file="tests/Unit/Constraint/MethodPair/AccessorPair/data/failure/GetPrivateSet.php" method="getProperty"/>
<violation rule="PHPMD\Rule\UnusedPrivateMethod" file="tests/Unit/Constraint/MethodPair/AccessorPair/data/failure/GetSetPrivate.php" method="setProperty"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="tests/Unit/Constraint/MethodPair/AccessorPair/data/parentchild/ExcludeParentGetterChildSetter.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="tests/Unit/Constraint/MethodPair/AccessorPair/data/parentchild/IncludeParentGetterChildSetter.php"/>
<violation rule="PHPMD\Rule\Design\NumberOfChildren" file="tests/Unit/Constraint/ValueProvider/AbstractValueProviderTestCase.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="tests/Unit/Constraint/ValueProvider/NativeValueProviderFactoryTest.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="tests/Unit/Constraint/ValueProvider/PseudoValueProviderFactoryTest.php"/>
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="tests/Unit/Constraint/ValueProvider/ValueProviderFactoryTest.php"/>
</phpmd-baseline>
69 changes: 57 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,46 +1,91 @@
parameters:
ignoreErrors:
-
message: "#^Call to function method_exists\\(\\) with 'PHPUnit\\\\\\\\Runner\\\\\\\\Version' and 'majorVersionNumber' will always evaluate to true\\.$#"
message: '#^Call to an undefined method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:getMock\(\)\.$#'
identifier: method.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Call to method getMock\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
message: '#^Call to function method_exists\(\) with ''PHPUnit\\\\Runner\\\\Version'' and ''majorVersionNumber'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Call to method testDouble\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
message: '#^Call to function method_exists\(\) with PHPUnit\\Framework\\MockObject\\Generator\\Generator and ''testDouble'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Instantiated class PHPUnit\\\\Framework\\\\MockObject\\\\Generator not found\\.$#"
message: '#^Call to method getMock\(\) on an unknown class PHPUnit\\Framework\\MockObject\\Generator\.$#'
identifier: class.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^PHPDoc tag @var for variable \\$mockGenerator contains unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
message: '#^Instantiated class PHPUnit\\Framework\\MockObject\\Generator not found\.$#'
identifier: class.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: "#^Call to method getMockForAbstractClass\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
count: 2
message: '#^Method DigitalRevolution\\AccessorPairConstraint\\Constraint\\ValueProvider\\Compound\\InstanceProvider\:\:getMockObject\(\) should return object but returns mixed\.$#'
identifier: return.type
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: '#^Parameter \#3 \$methods of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects array\|null, true given\.$#'
identifier: argument.type
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: '#^Parameter \#5 \$mockClassName of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects string, array given\.$#'
identifier: argument.type
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: '#^Parameter \#6 \$callOriginalConstructor of method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:testDouble\(\) expects bool, string given\.$#'
identifier: argument.type
count: 1
path: src/Constraint/ValueProvider/Compound/InstanceProvider.php

-
message: '#^Call to an undefined method PHPUnit\\Framework\\MockObject\\Generator\\Generator\:\:getMockForAbstractClass\(\)\.$#'
identifier: method.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^Call to method mockObjectForAbstractClass\\(\\) on an unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
message: '#^Call to function method_exists\(\) with PHPUnit\\Framework\\MockObject\\Generator\\Generator and ''mockObjectForAbstra…'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^Instantiated class PHPUnit\\\\Framework\\\\MockObject\\\\Generator not found\\.$#"
message: '#^Call to method getMockForAbstractClass\(\) on an unknown class PHPUnit\\Framework\\MockObject\\Generator\.$#'
identifier: class.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: "#^PHPDoc tag @var for variable \\$mockGenerator contains unknown class PHPUnit\\\\Framework\\\\MockObject\\\\Generator\\.$#"
message: '#^Instantiated class PHPUnit\\Framework\\MockObject\\Generator not found\.$#'
identifier: class.notFound
count: 1
path: src/Constraint/ValueProvider/Compound/IntersectionProvider.php

-
message: '#^Call to function array_filter\(\) requires parameter \#2 to be passed to avoid loose comparison semantics\.$#'
identifier: arrayFilter.strict
count: 1
path: src/Constraint/ValueProvider/Pseudo/NonEmptyValueProvider.php

-
message: '#^Parameter \#1 \$typehint of class DigitalRevolution\\AccessorPairConstraint\\Constraint\\ValueProvider\\Compound\\InstanceProvider constructor expects class\-string, string given\.$#'
identifier: argument.type
count: 1
path: tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ parameters:
html-escaped-string: 'string'
lowercase-string: 'string'
non-empty-string: 'string'

ignoreErrors:
-
identifier: phpunit.coversMethod
3 changes: 3 additions & 0 deletions src/Constraint/ConstraintConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function setExcludedMethods(array $excludedMethods): self
return $this;
}

/**
* @return callable(): mixed[]|null
*/
public function getConstructorCallback(): ?callable
{
return $this->constructorCallback;
Expand Down
4 changes: 2 additions & 2 deletions src/Constraint/ValueProvider/Compound/ArrayProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getValues(): array
{
$keys = [];
if ($this->keyProvider !== null) {
$keys = array_filter($this->keyProvider->getValues());
$keys = array_filter($this->keyProvider->getValues(), static fn($key): bool => $key !== '');
}

$testArray = [];
Expand All @@ -38,7 +38,7 @@ public function getValues(): array
}

/**
* @return array<int, mixed>
* @return mixed[]
* @throws Exception
*/
protected function getArrayValues(): array
Expand Down
8 changes: 5 additions & 3 deletions src/Constraint/ValueProvider/Compound/InstanceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

class InstanceProvider implements ValueProvider
{
/** @var string */
/** @var class-string */
protected $typehint;

/**
* @param class-string $typehint
*/
public function __construct(string $typehint)
{
$this->typehint = ltrim($typehint, '\\');
Expand All @@ -32,7 +35,7 @@ public function __construct(string $typehint)
public function getValues(): array
{
if (enum_exists($this->typehint)) {
/** @var UnitEnum $enum */
/** @var class-string<UnitEnum> $enum */
$enum = $this->typehint;

return $enum::cases();
Expand All @@ -49,7 +52,6 @@ public function getValues(): array
private function getMockObject(string $typehint): object
{
if (class_exists('PHPUnit\Framework\MockObject\Generator\Generator')) {
/** @var \PHPUnit\Framework\MockObject\Generator $mockGenerator */
$mockGenerator = new Generator();
if (method_exists($mockGenerator, 'testDouble')) {
if (method_exists(Version::class, 'majorVersionNumber') && Version::majorVersionNumber() >= 11) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use phpDocumentor\Reflection\Type;
use PHPUnit\Framework\MockObject\Generator\Generator;

/**
* @SuppressWarnings(PHPMD.EvalExpression)
*/
class IntersectionProvider implements ValueProvider
{
/** @var Type[] */
Expand Down Expand Up @@ -57,7 +54,6 @@ public function getValues(): array
}

if (class_exists('PHPUnit\Framework\MockObject\Generator\Generator')) {
/** @var \PHPUnit\Framework\MockObject\Generator $mockGenerator */
$mockGenerator = new Generator();
if (method_exists($mockGenerator, 'mockObjectForAbstractClass')) {
$instance = $mockGenerator->mockObjectForAbstractClass($className, [], '', false, false);
Expand Down
3 changes: 0 additions & 3 deletions src/Constraint/ValueProvider/NativeValueProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
use phpDocumentor\Reflection\PseudoTypes\True_;
use ReflectionMethod;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class NativeValueProviderFactory
{
private ValueProviderFactory $valueProviderFactory;
Expand Down
8 changes: 1 addition & 7 deletions src/Constraint/ValueProvider/PseudoValueProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
use phpDocumentor\Reflection\Types\ClassString;
use ReflectionMethod;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PseudoValueProviderFactory
{
private ValueProviderFactory $valueProviderFactory;
Expand Down Expand Up @@ -80,16 +77,13 @@ public function getProvider(Type $typehint, ?ReflectionMethod $method = null): ?
return $this->getPseudoStringProvider($typehint);
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getPseudoStringProvider(Type $typehint): ?ValueProvider
{
switch (get_class($typehint)) {
case ClassString::class:
$fqsen = null;
if ($typehint->getFqsen() !== null) {
/** @var class-string|null $fqsen */
/** @var class-string $fqsen */
$fqsen = (string)$typehint->getFqsen();
}

Expand Down
1 change: 0 additions & 1 deletion src/Constraint/ValueProvider/Special/ResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ResourceProvider implements ValueProvider
{
/**
* @return resource[]
* @throws Exception
*/
public function getValues(): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/Constraint/ValueProvider/ValueProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
use phpDocumentor\Reflection\Types\Object_;
use ReflectionMethod;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ValueProviderFactory
{
private NativeValueProviderFactory $nativeProviderFactory;
Expand Down Expand Up @@ -53,7 +50,10 @@ public function getProvider(Type $typehint, ?ReflectionMethod $method = null): V

// Support for fully namespaced class name
if (($typehint instanceof Object_ || $typehint instanceof Collection) && $typehint->getFqsen() !== null) {
return new InstanceProvider((string)$typehint->getFqsen());
/** @var class-string $fqsen */
$fqsen = (string)$typehint->getFqsen();

return new InstanceProvider($fqsen);
}

// Check if the provider typehint is a PHP scalar type
Expand Down
5 changes: 4 additions & 1 deletion src/Test/AbstractDtoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public function testModel(): void
if ($attribute->getName() === 'PHPUnit\Framework\Attributes\CoversClass') {
$testedAttributes = true;
$config = $this->getAccessorPairConfig();
static::assertAccessorPairs($attribute->getArguments()[0], $config);

/** @var array{0: class-string} $arguments */
$arguments = $attribute->getArguments();
static::assertAccessorPairs($arguments[0], $config);
}
}
static::assertTrue($testedAttributes, 'Missing CoversClass attribute');
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/Constraint/MethodPair/AbstractDataClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

use DigitalRevolution\AccessorPairConstraint\Constraint\ConstraintConfig;

/**
* @suppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class AbstractDataClass
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

use DigitalRevolution\AccessorPairConstraint\Tests\TestCase;

/**
* @suppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class AbstractValueProviderTestCase extends TestCase
{
abstract public function testGetValues(): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArrayProviderTest extends AbstractValueProviderTestCase
public function testGetValues(): void
{
$valueProvider = new ArrayProvider();
$values = $valueProvider->getValues();
$values = $valueProvider->getValues();

static::assertValueTypes($values, ['iterable']);
static::assertValueTypes(array_merge(...$values), ['integer', 'double', 'string', 'NULL']);
Expand All @@ -41,7 +41,7 @@ public function testGetValues(): void
public function testGetValuesTyped(): void
{
$valueProvider = new ArrayProvider(new IntProvider(), new StringProvider());
$values = $valueProvider->getValues();
$values = $valueProvider->getValues();

static::assertValueTypes($values, ['iterable']);
static::assertValueTypes(array_merge(...$values), ['integer']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProviderList
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\PseudoValueProviderFactory
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProviderFactory
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class NativeValueProviderFactoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function testGetValues(): void
}

/**
* @covers ::__construct
* @covers ::getValues
* @throws Exception
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Scalar\IntProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProviderList
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\NativeValueProviderFactory
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProviderFactory
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PseudoValueProviderFactoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProviderList
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\PseudoValueProviderFactory
* @uses \DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\NativeValueProviderFactory
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ValueProviderFactoryTest extends TestCase
{
Expand Down

0 comments on commit 317cc8f

Please sign in to comment.