Skip to content

Commit 341b99b

Browse files
authored
Dead Code Detector (#82)
1 parent 0fe8dc7 commit 341b99b

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"phpstan/phpstan-strict-rules": "^2.0.0",
1919
"phpunit/phpunit": "^10.5",
2020
"shipmonk/composer-dependency-analyser": "^1.8.1",
21+
"shipmonk/dead-code-detector": "^0.9.2",
2122
"shipmonk/name-collision-detector": "^2.1.1",
2223
"shipmonk/phpstan-rules": "^4.0.0",
2324
"slevomat/coding-standard": "dev-master"
@@ -29,6 +30,7 @@
2930
},
3031
"autoload-dev": {
3132
"psr-4": {
33+
"ShipMonkDev\\": "src-dev/",
3234
"ShipMonkTests\\InputMapper\\": "tests/"
3335
}
3436
},

phpstan.neon.dist

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ includes:
44
- ./vendor/phpstan/phpstan-phpunit/rules.neon
55
- ./vendor/phpstan/phpstan-strict-rules/rules.neon
66
- ./vendor/shipmonk/phpstan-rules/rules.neon
7+
- ./vendor/shipmonk/dead-code-detector/rules.neon
78

89
parameters:
910
level: 9
@@ -47,3 +48,16 @@ parameters:
4748
message: "#^Method ShipMonkTests\\\\InputMapper\\\\Compiler\\\\Validator\\\\Array\\\\Data\\\\ListItemValidatorWithMultipleValidatorsMapper\\:\\:map\\(\\) should return list\\<int\\<1, max\\>\\> but returns list\\<int\\>\\.$#"
4849
count: 1
4950
path: tests/Compiler/Validator/Array/Data/ListItemValidatorWithMultipleValidatorsMapper.php
51+
-
52+
message: '#\:\:__construct$#'
53+
identifier: shipmonk.deadMethod
54+
path: tests/**/*.php
55+
-
56+
identifier: shipmonk.deadMethod
57+
path: src/Compiler/Php/PhpCodeBuilder.php # consider utility class
58+
59+
services:
60+
-
61+
class: ShipMonkDev\PHPStan\IgnoreDeadInterfaceUsageProvider
62+
tags:
63+
- shipmonk.deadCode.memberUsageProvider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonkDev\PHPStan;
4+
5+
use ReflectionMethod;
6+
use ShipMonk\PHPStan\DeadCode\Provider\ReflectionBasedMemberUsageProvider;
7+
8+
class IgnoreDeadInterfaceUsageProvider extends ReflectionBasedMemberUsageProvider
9+
{
10+
11+
public function shouldMarkMethodAsUsed(ReflectionMethod $method): bool
12+
{
13+
return $method->getDeclaringClass()->isInterface() || $method->isAbstract();
14+
}
15+
16+
}

src/Compiler/MapperFactory/DefaultMapperCompilerFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class DefaultMapperCompilerFactory implements MapperCompilerFactory
8181

8282
final public const DELEGATE_OBJECT_MAPPING = 'delegateObjectMapping';
8383
final public const GENERIC_PARAMETERS = 'genericParameters';
84-
final public const DEFAULT_VALUE = 'defaultValue';
8584

8685
/**
8786
* @param array<class-string, callable(class-string, array<string, mixed>): MapperCompiler> $mapperCompilerFactories

tests/Compiler/Type/PhpDocTypeUtilsTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ public function testIntersect(array $types, string $expected, ?string $expectedR
623623

624624
$expectedTypeNode = $this->parseType($expected);
625625
$expectedTypeNodeReversed = $expectedReversed !== null ? $this->parseType($expectedReversed) : $expectedTypeNode;
626-
self::assertEquals($expectedTypeNode->__toString(), PhpDocTypeUtils::intersect(...$typesNodes)->__toString());
627-
self::assertEquals($expectedTypeNodeReversed->__toString(), PhpDocTypeUtils::intersect(...array_reverse($typesNodes))->__toString());
626+
self::assertSame($expectedTypeNode->__toString(), PhpDocTypeUtils::intersect(...$typesNodes)->__toString());
627+
self::assertSame($expectedTypeNodeReversed->__toString(), PhpDocTypeUtils::intersect(...array_reverse($typesNodes))->__toString());
628628
}
629629

630630
/**

0 commit comments

Comments
 (0)