dead code detector
0.7.0
PHPSTAN EXTENSION เพื่อค้นหารหัส PHP ที่ไม่ได้ใช้ในโครงการของคุณได้อย่างง่ายดาย!
composer require --dev shipmonk/dead-code-detectorใช้ส่วนขยายอย่างเป็นทางการหรือเพียงแค่โหลดกฎ:
# phpstan.neon.dist
includes :
- vendor/shipmonk/dead-code-detector/rules.neon phpstan/phpstan-symfony กับ containerXmlPath#[AsEventListener] แอตทริบิวต์#[AsController] แอตทริบิวต์#[AsCommand] แอตทริบิวต์#[Required] แอตทริบิวต์#[Route] คุณลักษณะEventSubscriberInterface::getSubscribedEventsonKernelResponse , onKernelRequest ฯลฯ #[AsEntityListener] แอตทริบิวต์DoctrineORMEvents::* เหตุการณ์DoctrineCommonEventSubscriber วิธีการ#[PreFlush] , #[PostLoad] , ... testXxx@test , @before , @afterClass ฯลฯ#[Test] , #[Before] , #[AfterClass] ฯลฯ handleXxx , renderXxx , actionXxx , injectXxx , createComponentXxxSmartObject Magic เรียกร้องให้มีคำอธิบายประกอบ @propertyไลบรารีทั้งหมดเหล่านั้นสามารถใช้งานได้โดยอัตโนมัติเมื่อพบในการพึ่งพานักแต่งเพลงของคุณ หากคุณต้องการบังคับให้เปิดใช้งาน/ปิดการใช้งานบางส่วนคุณสามารถ:
# phpstan.neon.dist
parameters :
shipmonkDeadCode :
usageProviders :
phpunit :
enabled : true ReflectionClass ตามที่ใช้$reflection->getConstructor() , $reflection->getConstant('NAME') , $reflection->getMethods() , ... vendor จะไม่ถูกรายงานว่าตายPsrLogLoggerInterface::log จะถูกพิจารณาโดยอัตโนมัติผู้ให้บริการเหล่านั้นเปิดใช้งานโดยค่าเริ่มต้น แต่คุณสามารถปิดการใช้งานได้หากจำเป็น
shipmonk.deadCode.memberUsageProvider และใช้ ShipMonkPHPStanDeadCodeProviderMemberUsageProvider # phpstan.neon.dist
services :
-
class : AppApiOutputUsageProvider
tags :
- shipmonk.deadCode.memberUsageProvider สำคัญ
อินเตอร์เฟสและแท็กเปลี่ยนไปใน 0.7 หากคุณใช้ phpstan 1.x สิ่งเหล่านั้นจะถูกใช้แตกต่างกัน
ShipMonkPHPStanDeadCodeProviderReflectionBasedMemberUsageProvider : use ReflectionMethod ;
use ShipMonk PHPStan DeadCode Provider ReflectionBasedMemberUsageProvider ;
class ApiOutputUsageProvider extends ReflectionBasedMemberUsageProvider
{
public function shouldMarkMethodAsUsed ( ReflectionMethod $ method ): bool
{
// all methods from our ApiOutput interface are called automatically (e.g. during serialization)
return $ method -> getDeclaringClass ()-> implementsInterface (ApiOutput::class);
}
}MemberUsageProvider : use ReflectionMethod ;
use ShipMonk PHPStan DeadCode Graph ClassMethodRef ;
use ShipMonk PHPStan DeadCode Graph ClassMethodUsage ;
use ShipMonk PHPStan DeadCode Provider MemberUsageProvider ;
use Symfony Component Serializer SerializerInterface ;
class DeserializationUsageProvider implements MemberUsageProvider
{
/**
* @return list<ClassMemberUsage>
*/
public function getUsages ( Node $ node , Scope $ scope ): array
{
if (! $ node instanceof MethodCall) {
return [];
}
if (
// our deserialization calls constructor
$ scope -> getType ( $ node -> var )-> getObjectClassNames () === [SerializerInterface::class] &&
$ node -> name -> toString () === ' deserialize '
) {
$ secondArgument = $ node -> getArgs ()[ 1 ]-> value ;
$ serializedClass = $ scope -> getType ( $ secondArgument )-> getConstantStrings ()[ 0 ];
// record the method it was called from (needed for proper transitive dead code elimination)
$ originRef = $ this -> getOriginMethodRef ( $ scope );
// record the hidden constructor call
$ constructorRef = new ClassMethodRef ( $ serializedClass -> getValue (), ' __construct ' , false );
return [ new ClassMethodUsage ( $ originRef , $ constructorRef )];
}
return [];
}
private function getOriginMethodRef ( Scope $ scope ): ? ClassMethodRef
{
return new ClassMethodRef (
$ scope -> getClassReflection ()-> getName (),
$ scope -> getFunction ()-> getName (),
false ,
);
}
} ------ ------------------------------------------------------------------------
Line src/App/Facade/UserFacade.php
------ ------------------------------------------------------------------------
26 Unused AppFacadeUserFacade::updateUserAddress
? shipmonk.deadMethod
Thus AppEntityUser::updateAddress is transitively also unused
Thus AppEntityAddress::setPostalCode is transitively also unused
Thus AppEntityAddress::setCountry is transitively also unused
Thus AppEntityAddress::setStreet is transitively also unused
Thus AppEntityAddress::MAX_STREET_CHARS is transitively also unused
------ ------------------------------------------------------------------------
phpstan.neon.dist ของคุณ: parameters :
shipmonkDeadCode :
reportTransitivelyDeadMethodAsSeparateError : true removeDeadCode : vendor/bin/phpstan analyse --error-format removeDeadCodeclass UserFacade
{
- public const TRANSITIVELY_DEAD = 1;
-
- public function deadMethod(): void
- {
- echo self::TRANSITIVELY_DEAD;
- }
}$unknown->method() ) โดยทำเครื่องหมายวิธีทั้งหมดที่ชื่อตาม method ที่ใช้new $unknown() จะทำเครื่องหมายตัวสร้างทั้งหมดตามที่ใช้phpstan.neon.dist :$unknown::CONSTANT ) parameters :
shipmonkDeadCode :
trackMixedAccess : false-vvv และคุณจะเห็นการวินิจฉัยบางอย่าง: Found 2 usages over unknown type:
• setCountry method, for example in AppEntityUser::updateAddress
• setStreet method, for example in AppEntityUser::updateAddress
__get , __set ฯลฯ ) ไม่เคยรายงานว่าตาย__construct , __clone parameters :
ignoreErrors :
- ' # ^Unused .*?::__construct$ # ' MemberUsageProvider ที่กำหนดเอง: use ShipMonk PHPStan DeadCode Provider ReflectionBasedMemberUsageProvider ;
class IgnoreDeadInterfaceUsageProvider extends ReflectionBasedMemberUsageProvider
{
public function shouldMarkMethodAsUsed ( ReflectionMethod $ method ): bool
{
return $ method -> getDeclaringClass ()-> isInterface ();
}
}composer checkcomposer fix:cs