Skip to content

Commit

Permalink
Upgrade PHPStan to v1.11.1 (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 15, 2024
1 parent 7f3d515 commit 4b04088
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
if: matrix.type == 'StaticAnalysis'
run: |
echo "memory_limit = 2G" > /usr/local/etc/php/conf.d/custom-memory-limit.ini
vendor/bin/phpstan analyse
vendor/bin/phpstan analyse -v
unit-test:
name: Unit
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"ergebnis/phpunit-slow-test-detector": "^2.9",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^1.3",
"phpunit/phpunit": "^9.5.5 || ^10.0 || ^11.0"
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,50 @@ parameters:

-
path: 'tests/DynamicMethodTraitTest.php'
identifier: method.notFound
message: '~^Call to an undefined method Atk4\\Core\\Tests\\(DynamicMethodMock|DynamicMethodWithoutHookMock)::\w+\(\)\.$~'
count: 10

# remove once PHPUnit 9.x support is removed
-
path: 'src/Phpunit/TestCase.php'
identifier: class.notFound
message: '~^Access to constant (STATUS_INCOMPLETE|STATUS_SKIPPED) on an unknown class PHPUnit\\Runner\\BaseTestRunner\.$~'
count: 2
-
path: 'src/Phpunit/TestCase.php'
identifier: method.notFound
message: '~^Call to an undefined method Atk4\\Core\\Phpunit\\TestCase::(getName|getStatus|getTestResultObject)\(\)\.$~'
count: 5
-
path: 'src/Phpunit/TestCase.php'
identifier: staticMethod.notFound
message: '~^Call to an undefined static method PHPUnit\\Util\\Test::(getLinesToBeCovered|getLinesToBeUsed|parseTestMethodAnnotations)\(\)\.$~'
count: 3
-
path: 'src/Phpunit/TestCase.php'
identifier: method.nonObject
message: '~^Cannot call method (className|methodName)\(\) on array<int, string>\|PHPUnit\\Metadata\\Metadata\.$~'
count: 2
-
path: 'tests/HookTraitTest.php'
identifier: method.notFound
message: '~^Call to an undefined method Atk4\\Core\\Tests\\HookTraitTest::getName\(\)\.$~'
count: 2
-
path: 'tests/Phpunit/TestCaseTest.php'
identifier: method.notFound
message: '~^Call to an undefined method Atk4\\Core\\Tests\\Phpunit\\TestCaseTest::getStatus\(\)\.$~'
count: 1
-
path: 'tests/Phpunit/TestCaseTest.php'
identifier: class.notFound
message: '~^Access to constant STATUS_INCOMPLETE on an unknown class PHPUnit\\Runner\\BaseTestRunner\.$~'
count: 1

# https://github.com/phpstan/phpstan/issues/10150
-
path: 'src/DebugTrait.php'
identifier: method.missingOverride
message: '~^Method Atk4\\Core\\Tests\\DebugPsrMock::\w+\(\) overrides method Psr\\Log\\LoggerInterface::\w+\(\) but is missing the #\[\\Override\] attribute\.$~'
count: 9
4 changes: 2 additions & 2 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function _addIntoCollection(string $name, object $item, string $collec
$item->shortName = $name;
$item->setOwner($this);
if (TraitUtil::hasTrackableTrait($this) && TraitUtil::hasNameTrait($this) && TraitUtil::hasNameTrait($item)) {
$item->name = $this->_shortenMl($this->name ?? '', $collection, $item->shortName, $item->name ?? null); // @phpstan-ignore-line
$item->name = $this->_shortenMl($this->name ?? '', $collection, $item->shortName, $item->name ?? null); // @phpstan-ignore property.notFound
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ protected function _shortenMl(string $ownerName, string $collectionName, string
$ownerName .= '-' . $collectionName;

if (TraitUtil::hasContainerTrait($this)) {
return $this->_shorten($ownerName, $itemShortName, $origItemName); // @phpstan-ignore-line
return $this->_shorten($ownerName, $itemShortName, $origItemName); // @phpstan-ignore method.notFound
}

// ugly hack to deduplicate code
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected function _addContainer(object $element, array $args): void
$element->setOwner($this);
$element->shortName = $name;
if (TraitUtil::hasTrackableTrait($this) && TraitUtil::hasNameTrait($this) && TraitUtil::hasNameTrait($element)) {
$element->name = $this->_shorten($this->name ?? '', $element->shortName, $element->name ?? null); // @phpstan-ignore-line
$element->name = $this->_shorten($this->name ?? '', $element->shortName, $element->name ?? null); // @phpstan-ignore property.notFound
}

$this->elements[$element->shortName] = $element;
Expand Down
8 changes: 4 additions & 4 deletions src/DiContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ trait DiContainerTrait
public function setDefaults(array $properties, bool $passively = false)
{
foreach ($properties as $k => $v) {
if (is_int($k)) { // @phpstan-ignore-line
$k = (string) $k; // @phpstan-ignore-line
if (is_int($k)) { // @phpstan-ignore function.impossibleType
$k = (string) $k; // @phpstan-ignore cast.useless
}

if (property_exists($this, $k)) {
Expand Down Expand Up @@ -102,7 +102,7 @@ public static function assertInstanceOf(object $object)// :static supported by P
private static function _fromSeedPrecheck($seed, bool $unsafe): void
{
if (!is_object($seed)) {
if (!is_array($seed)) { // @phpstan-ignore-line
if (!is_array($seed)) { // @phpstan-ignore function.alreadyNarrowedType
throw (new Exception('Seed must be an array or an object'))
->addMoreInfo('seed', $seed);
}
Expand Down Expand Up @@ -153,6 +153,6 @@ public static function fromSeedUnsafe($seed = [], $defaults = [])
self::_fromSeedPrecheck($seed, true);
$object = Factory::factory($seed, $defaults);

return $object; // @phpstan-ignore-line
return $object; // @phpstan-ignore return.type
}
}
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function _newObject(string $className, array $ctorArgs): object
*/
protected function _factory($seed, array $defaults): object
{
if (!is_array($seed) && !is_object($seed)) { // @phpstan-ignore-line
if (!is_array($seed) && !is_object($seed)) { // @phpstan-ignore function.alreadyNarrowedType, booleanAnd.alwaysFalse
throw new Exception('Use of non-array (' . gettype($seed) . ') seed is not supported');
}

Expand Down Expand Up @@ -195,7 +195,7 @@ final public static function factory($seed, $defaults = []): object
throw new \Error('Too many method arguments');
}

if ($defaults === null) { // @phpstan-ignore-line should be deprecated soon
if ($defaults === null) { // @phpstan-ignore identical.alwaysFalse (should be deprecated soon)
$defaults = [];
}

Expand Down
6 changes: 3 additions & 3 deletions src/HookTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ private function _makeHookDynamicFx(?\Closure $getFxThisFx, \Closure $fx, bool $
if ($getFxThisFx === null) {
$fxThis = $target;
} else {
$fxThis = $getFxThisFx($target); // @phpstan-ignore-line
if (!is_object($fxThis)) { // @phpstan-ignore-line
$fxThis = $getFxThisFx($target); // @phpstan-ignore argument.type
if (!is_object($fxThis)) { // @phpstan-ignore function.alreadyNarrowedType
throw new \TypeError('New $this must be an object');
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public function hook(string $spot, array $args = [], &$brokenBy = null)
}
}

if (!isset($this->hooks[$spot])) { // @phpstan-ignore-line
if (!isset($this->hooks[$spot])) { // @phpstan-ignore isset.offset
break;
} elseif ($hooksBackup !== $this->hooks[$spot]) {
krsort($this->hooks[$spot]);
Expand Down
2 changes: 1 addition & 1 deletion src/TrackableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function destroy(): void

// GC remove reference to app is AppScope in use
if (TraitUtil::hasAppScopeTrait($this) && $this->issetApp()) {
$this->_app = null; // @phpstan-ignore-line
$this->_app = null; // @phpstan-ignore property.notFound
}

// GC remove reference to owner
Expand Down
4 changes: 2 additions & 2 deletions tests/CollectionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public function testCloneCollection(): void

self::assertNotSame($m->getField('a'), $mCloned->getField('a'));
self::assertNotSame($m->getField('b'), $mCloned->getField('b'));
self::assertSame('b', $m->getField('b')->shortName); // @phpstan-ignore-line
self::assertSame('b', $mCloned->getField('b')->shortName); // @phpstan-ignore-line
self::assertSame('b', $m->getField('b')->shortName); // @phpstan-ignore property.notFound
self::assertSame('b', $mCloned->getField('b')->shortName); // @phpstan-ignore property.notFound
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/ContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ public function testExceptionArg2(): void
$m = new ContainerMock();

if (\PHP_MAJOR_VERSION === 7) {
self::assertNotNull('Expecting E_WARNING is deprecated in PHPUnit 9'); // @phpstan-ignore-line
self::assertNotNull('Expecting E_WARNING is deprecated in PHPUnit 9'); // @phpstan-ignore staticMethod.alreadyNarrowedType

return;
}

$this->expectException(\TypeError::class);
$this->expectExceptionMessage('array_diff_key(): Argument #1 ($array) must be of type array, int given');
$m->add(new TrackableMock(), 123); // @phpstan-ignore-line
$m->add(new TrackableMock(), 123); // @phpstan-ignore argument.type
}

public function testException3(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/DiContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testNoPropExNumeric(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Property for specified object is not defined');
$m->setDefaults([5 => 'qwerty']); // @phpstan-ignore-line
$m->setDefaults([5 => 'qwerty']); // @phpstan-ignore argument.type
}

public function testProperties(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/DynamicMethodTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testExceptionProtectedMethod(): void
$this->expectExceptionMessage('Call to protected method ' . DynamicMethodMock::class
. '::protectedMethod() from global scope');
\Closure::bind(static function () use ($m) {
$m->protectedMethod(); // @phpstan-ignore-line
$m->protectedMethod(); // @phpstan-ignore method.protected
}, null, null)();
}

Expand Down
6 changes: 3 additions & 3 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function testBasic(): void

public function testInjection(): void
{
$s1 = Factory::factory(new FactoryTestDiMock(), null); // @phpstan-ignore-line
$s1 = Factory::factory(new FactoryTestDiMock(), null); // @phpstan-ignore argument.type
self::assertNotSame('bar', $s1->foo);

$s1 = Factory::factory(new FactoryTestDiMock(), ['foo' => 'bar']);
Expand Down Expand Up @@ -329,7 +329,7 @@ public function testMustHaveProperty(): void
public function testGiveClassFirst(): void
{
$this->expectException(\TypeError::class);
Factory::factory(['foo' => 'bar'], new FactoryTestDiMock()); // @phpstan-ignore-line
Factory::factory(['foo' => 'bar'], new FactoryTestDiMock()); // @phpstan-ignore argument.type
}

public function testStringDefault(): void
Expand Down Expand Up @@ -382,7 +382,7 @@ public function testFactory(): void

// from string seed
$this->expectException(Exception::class);
Factory::factory(FactoryFactoryMock::class); // @phpstan-ignore-line
Factory::factory(FactoryFactoryMock::class); // @phpstan-ignore argument.type
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/HookTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function testUpdateWhenActive(): void

$m->removeHook('spot', $indexPrevious, true);
$m->removeHook('spot', $indexCurrent, true);
$m->removeHook('spot', $indexNext, true); // @phpstan-ignore-line
$m->removeHook('spot', $indexNext, true); // @phpstan-ignore argument.type

$m->onHook('spot', static function () use ($m, $priority) {
$m->removeHook('spot', $priority);
Expand Down Expand Up @@ -457,7 +457,7 @@ public function testOnHookDynamicGetterNullException(): void
{
$m = new HookMock();

$m->onHookDynamic('inc', static function (HookMock $m) { // @phpstan-ignore-line
$m->onHookDynamic('inc', static function (HookMock $m) { // @phpstan-ignore argument.type
return null;
}, $m->makeIncrementResultFx());

Expand Down Expand Up @@ -495,7 +495,7 @@ public function testPassByReference(): void
$m->onHookDynamic('inc', static function () use ($m) {
return clone $m;
}, function ($ignoreObject, $ignore1st, int &$value) {
$this->makeIncrementResultFx(); // @phpstan-ignore-line prevent PHP CS Fixer to make this anonymous function static
$this->makeIncrementResultFx(); // @phpstan-ignore method.notFound (prevent PHP CS Fixer to make this anonymous function static)

++$value;
});
Expand Down
2 changes: 1 addition & 1 deletion tests/QuietObjectWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testNotCloneable(): void
$o = new QuietObjectWrapper(new \stdClass());

$this->expectException(\Error::class);
clone $o;
clone $o; // @phpstan-ignore expr.resultUnused
}

public function testNotSerializeable(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/StaticAddToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testBasic(): void

// not the same or extended class
$this->expectException(\TypeError::class);
StdSat::addTo($m, $tr); // @phpstan-ignore-line
StdSat::addTo($m, $tr); // @phpstan-ignore argument.type
}

public function testAssertInstanceOf(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/WarnDynamicPropertyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testIssetException(): void

$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception::$xxx');
isset($test->xxx); // @phpstan-ignore-line
isset($test->xxx); // @phpstan-ignore property.notFound, expr.resultUnused
});
}

Expand All @@ -43,7 +43,7 @@ public function testGetException(): void

$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception::$xxx');
$test->xxx; // @phpstan-ignore-line
$test->xxx; // @phpstan-ignore property.notFound, expr.resultUnused
});
}

Expand All @@ -54,7 +54,7 @@ public function testSetException(): void

$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception::$xxx');
$test->xxx = 5; // @phpstan-ignore-line
$test->xxx = 5; // @phpstan-ignore property.notFound
});
}

Expand All @@ -65,7 +65,7 @@ public function testUnsetException(): void

$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception::$xxx');
unset($test->{'xxx'}); // @phpstan-ignore-line
unset($test->{'xxx'}); // @phpstan-ignore property.notFound
});
}

Expand All @@ -82,7 +82,7 @@ public function testGetSetPublicProperty(): void
$this->runWithErrorConvertedToException(function () use ($test) {
$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception@anonymous::$p');
$test->p; // @phpstan-ignore-line
$test->p; // @phpstan-ignore expr.resultUnused
});
}

Expand All @@ -92,7 +92,7 @@ public function testGetProtectedPropertyException(): void

$this->expectException(\Error::class);
$this->expectExceptionMessage('Cannot access protected property Atk4\Core\Exception::$customExceptionTitle');
$test->customExceptionTitle; // @phpstan-ignore-line
$test->customExceptionTitle; // @phpstan-ignore property.protected, expr.resultUnused
}

public function testGetPrivateProperty(): void
Expand All @@ -103,7 +103,7 @@ public function testGetPrivateProperty(): void

$this->expectException(WarnError::class);
$this->expectExceptionMessage('Undefined property: Atk4\Core\Exception::$trace');
$test->trace; // @phpstan-ignore-line
$test->trace; // @phpstan-ignore property.notFound, expr.resultUnused
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/config_test/config_bad_format.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

declare(strict_types=1);

1 === 1; // @phpstan-ignore-line prevent CS fixer to remove strict_types declaration above
1 === 1; // @phpstan-ignore identical.alwaysTrue, expr.resultUnused (prevent CS fixer to remove strict_types declaration above)

0 comments on commit 4b04088

Please sign in to comment.