Skip to content

Dead Code Detector #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"phpstan/phpstan-strict-rules": "^2.0.0",
"phpunit/phpunit": "^10.5",
"shipmonk/composer-dependency-analyser": "^1.8.1",
"shipmonk/dead-code-detector": "^0.9.2",
"shipmonk/name-collision-detector": "^2.1.1",
"shipmonk/phpstan-rules": "^4.0.0",
"slevomat/coding-standard": "dev-master"
Expand All @@ -29,6 +30,7 @@
},
"autoload-dev": {
"psr-4": {
"ShipMonkDev\\": "src-dev/",
"ShipMonkTests\\InputMapper\\": "tests/"
}
},
Expand Down
14 changes: 14 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ includes:
- ./vendor/phpstan/phpstan-phpunit/rules.neon
- ./vendor/phpstan/phpstan-strict-rules/rules.neon
- ./vendor/shipmonk/phpstan-rules/rules.neon
- ./vendor/shipmonk/dead-code-detector/rules.neon

parameters:
level: 9
Expand Down Expand Up @@ -47,3 +48,16 @@ parameters:
message: "#^Method ShipMonkTests\\\\InputMapper\\\\Compiler\\\\Validator\\\\Array\\\\Data\\\\ListItemValidatorWithMultipleValidatorsMapper\\:\\:map\\(\\) should return list\\<int\\<1, max\\>\\> but returns list\\<int\\>\\.$#"
count: 1
path: tests/Compiler/Validator/Array/Data/ListItemValidatorWithMultipleValidatorsMapper.php
-
message: '#\:\:__construct$#'
identifier: shipmonk.deadMethod
path: tests/**/*.php
-
identifier: shipmonk.deadMethod
path: src/Compiler/Php/PhpCodeBuilder.php # consider utility class

services:
-
class: ShipMonkDev\PHPStan\IgnoreDeadInterfaceUsageProvider
tags:
- shipmonk.deadCode.memberUsageProvider
16 changes: 16 additions & 0 deletions src-dev/PHPStan/IgnoreDeadInterfaceUsageProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace ShipMonkDev\PHPStan;

use ReflectionMethod;
use ShipMonk\PHPStan\DeadCode\Provider\ReflectionBasedMemberUsageProvider;

class IgnoreDeadInterfaceUsageProvider extends ReflectionBasedMemberUsageProvider
{

public function shouldMarkMethodAsUsed(ReflectionMethod $method): bool
{
return $method->getDeclaringClass()->isInterface() || $method->isAbstract();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class DefaultMapperCompilerFactory implements MapperCompilerFactory

final public const DELEGATE_OBJECT_MAPPING = 'delegateObjectMapping';
final public const GENERIC_PARAMETERS = 'genericParameters';
final public const DEFAULT_VALUE = 'defaultValue';

/**
* @param array<class-string, callable(class-string, array<string, mixed>): MapperCompiler> $mapperCompilerFactories
Expand Down
4 changes: 2 additions & 2 deletions tests/Compiler/Type/PhpDocTypeUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ public function testIntersect(array $types, string $expected, ?string $expectedR

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

/**
Expand Down