Skip to content

Commit

Permalink
Detect callables invoked via register_shutdown_function (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 6, 2024
1 parent c82b690 commit 177f344
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ services:
- phpstan.collector

-
class: TomasVotruba\UnusedPublic\Collectors\Callable_\CallUserFuncCollector
class: TomasVotruba\UnusedPublic\Collectors\Callable_\CallbackFunctionCollector
tags:
- phpstan.collector

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @implements Collector<FuncCall, non-empty-array<string>|null>
*/
final readonly class CallUserFuncCollector implements Collector
final readonly class CallbackFunctionCollector implements Collector
{
public function __construct(
private Configuration $configuration,
Expand Down Expand Up @@ -92,6 +92,10 @@ private function shouldSkipNode(Node $node): bool
return true;
}

return strtolower($node->name->toString()) !== 'call_user_func';
return !in_array(
strtolower($node->name->toString()),
['call_user_func', 'register_shutdown_function'],
true
);
}
}
4 changes: 2 additions & 2 deletions src/NodeCollectorExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPStan\Node\CollectedDataNode;
use TomasVotruba\UnusedPublic\CollectorMapper\MethodCallCollectorMapper;
use TomasVotruba\UnusedPublic\Collectors\Callable_\AttributeCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallUserFuncCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallbackFunctionCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallCollector;
use TomasVotruba\UnusedPublic\Collectors\StaticCall\StaticMethodCallableCollector;
Expand Down Expand Up @@ -48,7 +48,7 @@ private function extractCollectedDatas(CollectedDataNode $collectedDataNode): ar
$collectedDataNode->get(StaticMethodCallCollector::class),
$collectedDataNode->get(StaticMethodCallableCollector::class),
$collectedDataNode->get(AttributeCallableCollector::class),
$collectedDataNode->get(CallUserFuncCollector::class),
$collectedDataNode->get(CallbackFunctionCollector::class),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use TomasVotruba\UnusedPublic\Collectors\Callable_\AttributeCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallUserFuncCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallbackFunctionCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallCollector;
use TomasVotruba\UnusedPublic\Collectors\PublicClassMethodCollector;
use TomasVotruba\UnusedPublic\Collectors\StaticCall\StaticMethodCallCollector;
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function getCollectors(): array
self::getContainer()->getByType(MethodCallCollector::class),
self::getContainer()->getByType(StaticMethodCallCollector::class),
self::getContainer()->getByType(AttributeCallableCollector::class),
self::getContainer()->getByType(CallUserFuncCollector::class),
self::getContainer()->getByType(CallbackFunctionCollector::class),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use TomasVotruba\UnusedPublic\Collectors\Callable_\AttributeCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallUserFuncCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallbackFunctionCollector;
use TomasVotruba\UnusedPublic\Collectors\FormTypeClassCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallCollector;
Expand Down Expand Up @@ -64,7 +64,7 @@ protected function getCollectors(): array
// callables
self::getContainer()->getByType(StaticMethodCallableCollector::class),
self::getContainer()->getByType(AttributeCallableCollector::class),
self::getContainer()->getByType(CallUserFuncCollector::class),
self::getContainer()->getByType(CallbackFunctionCollector::class),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture;

/**
* @var \TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Source\Caller1 $caller1
*/

register_shutdown_function([$caller1, 'callIt']);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use TomasVotruba\UnusedPublic\Collectors\Callable_\AttributeCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallUserFuncCollector;
use TomasVotruba\UnusedPublic\Collectors\Callable_\CallbackFunctionCollector;
use TomasVotruba\UnusedPublic\Collectors\FormTypeClassCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallableCollector;
use TomasVotruba\UnusedPublic\Collectors\MethodCall\MethodCallCollector;
Expand Down Expand Up @@ -174,6 +174,7 @@ public static function provideData(): Iterator

yield [[__DIR__ . '/Fixture/plain.php', __DIR__ . '/Source/Caller1.php'], []];
yield [[__DIR__ . '/Fixture/plain-call-user-func.php', __DIR__ . '/Source/Caller1.php'], []];
yield [[__DIR__ . '/Fixture/plain-call-shutdown-function.php', __DIR__ . '/Source/Caller1.php'], []];
yield [[__DIR__ . '/Fixture/SkipCrashBug89.php.inc'], []];

yield [[__DIR__ . '/Fixture/SkipJsonSerialize.php'], []];
Expand Down Expand Up @@ -203,7 +204,7 @@ protected function getCollectors(): array
// callables
self::getContainer()->getByType(StaticMethodCallableCollector::class),
self::getContainer()->getByType(AttributeCallableCollector::class),
self::getContainer()->getByType(CallUserFuncCollector::class),
self::getContainer()->getByType(CallbackFunctionCollector::class),
];
}

Expand Down

0 comments on commit 177f344

Please sign in to comment.