Skip to content

Commit

Permalink
Refactor to use TomasVotruba/unused-public (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 18, 2022
1 parent 2cbc96f commit fe6e403
Show file tree
Hide file tree
Showing 33 changed files with 1,650 additions and 31 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"staabm/phpstan-dba": "^0.2.53",
"spaze/phpstan-disallowed-calls": "^2.11",
"symplify/phpstan-rules": "^11.1",
"staabm/phpstan-baseline-analysis": "^0.9"
"staabm/phpstan-baseline-analysis": "^0.9",
"tomasvotruba/unused-public": "0.0.10.72"
},
"autoload": {
"classmap": [
Expand Down
45 changes: 44 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 9 additions & 23 deletions config/dead-code.neon
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
# these rule focus on the whole-project analysis, see https://phpstan.org/developing-extensions/collectors
rules:
- Symplify\PHPStanRules\Rules\DeadCode\UnusedPublicClassConstRule
- Symplify\PHPStanRules\Rules\DeadCode\UnusedPublicClassMethodRule

services:
# for UnusedPublicClassConstRule
-
class: Symplify\PHPStanRules\Collector\ClassConst\ClassConstFetchCollector
tags: [phpstan.collector]

-
class: Symplify\PHPStanRules\Collector\ClassConst\PublicClassLikeConstCollector
tags: [phpstan.collector]

# for UnusedPublicClassMethodRule
-
class: Symplify\PHPStanRules\Collector\ClassMethod\PublicClassMethodCollector
tags: [phpstan.collector]

-
class: Symplify\PHPStanRules\Collector\ClassMethod\MethodCallCollector
tags: [phpstan.collector]
includes:
- ../vendor/tomasvotruba/unused-public/config/extension.neon

parameters:
unused_public:
methods: true
properties: true
constants: true
static_properties: true
8 changes: 4 additions & 4 deletions lib/RexStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function phpExecutable(): string
if ('Windows' !== PHP_OS_FAMILY) {
$executable = 'php';
$path = '$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin';

if ('Darwin' === PHP_OS_FAMILY) {
$customConfig = '/Library/Application Support/appsolute/MAMP PRO/conf/php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION.'.ini';
if (is_file($customConfig)) {
Expand All @@ -31,12 +31,12 @@ public static function phpExecutable(): string
$mampPhp = '/Applications/MAMP/bin/php/php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION.'/bin/';
if (is_executable($mampPhp.'php')) {
$path .= ':'.$mampPhp;
}
}
}

return 'PATH="'. $path .'" '.$executable;
}

return 'php';
}

Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
return array(
'staabm\\PHPStanDba\\' => array($vendorDir . '/staabm/phpstan-dba/src'),
'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'),
'TomasVotruba\\UnusedPublic\\' => array($vendorDir . '/tomasvotruba/unused-public/src'),
'Symplify\\PHPStanRules\\' => array($vendorDir . '/symplify/phpstan-rules/src', $vendorDir . '/symplify/phpstan-rules/packages'),
'Symfony\\Polyfill\\Php80\\' => array($vendorDir . '/symfony/polyfill-php80'),
'Spaze\\PHPStan\\Rules\\Disallowed\\' => array($vendorDir . '/spaze/phpstan-disallowed-calls/src'),
Expand Down
8 changes: 8 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class ComposerStaticInit9cf8af24a7a084f114b4553be2a1ff9f
array (
'Webmozart\\Assert\\' => 17,
),
'T' =>
array (
'TomasVotruba\\UnusedPublic\\' => 26,
),
'S' =>
array (
'Symplify\\PHPStanRules\\' => 22,
Expand Down Expand Up @@ -137,6 +141,10 @@ class ComposerStaticInit9cf8af24a7a084f114b4553be2a1ff9f
array (
0 => __DIR__ . '/..' . '/webmozart/assert/src',
),
'TomasVotruba\\UnusedPublic\\' =>
array (
0 => __DIR__ . '/..' . '/tomasvotruba/unused-public/src',
),
'Symplify\\PHPStanRules\\' =>
array (
0 => __DIR__ . '/..' . '/symplify/phpstan-rules/src',
Expand Down
46 changes: 46 additions & 0 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,52 @@
},
"install-path": "../thecodingmachine/safe"
},
{
"name": "tomasvotruba/unused-public",
"version": "0.0.10.72",
"version_normalized": "0.0.10.72",
"source": {
"type": "git",
"url": "https://github.com/TomasVotruba/unused-public.git",
"reference": "6c425fcb79c11d6e2124f2db5fa56b69580b164f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/TomasVotruba/unused-public/zipball/6c425fcb79c11d6e2124f2db5fa56b69580b164f",
"reference": "6c425fcb79c11d6e2124f2db5fa56b69580b164f",
"shasum": ""
},
"require": {
"nette/utils": "^3.2",
"php": "^7.2|8.0",
"phpstan/phpstan": "^1.9.3"
},
"time": "2022-12-16T10:34:15+00:00",
"type": "phpstan-extension",
"extra": {
"phpstan": {
"includes": [
"config/extension.neon"
]
}
},
"installation-source": "dist",
"autoload": {
"psr-4": {
"TomasVotruba\\UnusedPublic\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Detect unused public properties, constants and methods in your code",
"support": {
"issues": "https://github.com/TomasVotruba/unused-public/issues",
"source": "https://github.com/TomasVotruba/unused-public/tree/0.0.10.72"
},
"install-path": "../tomasvotruba/unused-public"
},
{
"name": "webmozart/assert",
"version": "1.11.0",
Expand Down
13 changes: 11 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'dd7a6bd3db81a766233bbf42d089ecd4dbc6ce27',
'reference' => '2cbc96f0a498327e40d98a9a40ea6c845e3f12ce',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'dd7a6bd3db81a766233bbf42d089ecd4dbc6ce27',
'reference' => '2cbc96f0a498327e40d98a9a40ea6c845e3f12ce',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down Expand Up @@ -163,6 +163,15 @@
'aliases' => array(),
'dev_requirement' => false,
),
'tomasvotruba/unused-public' => array(
'pretty_version' => '0.0.10.72',
'version' => '0.0.10.72',
'reference' => '6c425fcb79c11d6e2124f2db5fa56b69580b164f',
'type' => 'phpstan-extension',
'install_path' => __DIR__ . '/../tomasvotruba/unused-public',
'aliases' => array(),
'dev_requirement' => false,
),
'webmozart/assert' => array(
'pretty_version' => '1.11.0',
'version' => '1.11.0.0',
Expand Down
9 changes: 9 additions & 0 deletions vendor/tomasvotruba/unused-public/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
4 changes: 4 additions & 0 deletions vendor/tomasvotruba/unused-public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.lock

.phpunit.result.cache
94 changes: 94 additions & 0 deletions vendor/tomasvotruba/unused-public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Find Unused Public Constants, Properties and Methods in Your Code

It's easy to find unused private class elements, because they're not used in the class itself:

```diff
final class Book
{
public function getTitle(): string
{
// ...
}

- private function getSubtitle(): string
- {
- // ...
- }
}
```

But what about public class elements?

<br>

**How can we detect such element?**

* find a e.g. public method
* find all public method calls
* compare those in simple diff
* if the public method is not found, it probably unused

That's exactly what this package does.

<br>

This technique is very useful for private projects and to detect accidentally open public API that should be used only locally.

<br>

## Install

```bash
composer require tomasvotruba/unused-public --dev
```

The package is available on PHP 7.2-8.1 versions in tagged releases.

<br>

## Usage

With PHPStan extension installer, everything is ready to run.

Enable each item on their own with simple configuration:

```neon
# phpstan.neon
parameters:
unused_public:
methods: true
properties: true
constants: true
static_properties: true
```

<br>

## Known Limitations

In some cases, the method reports false positives:

* it's not possible to detect unused public method that are called only in Twig templates
* following cases are skipped
* public function in Twig extensions - those are functions/filters callable

<br>

## Skip False Positives

Is element reported as unused, but it's actually used?

Mark the class or element wit `@api` to declare it as public API and skip it:

```php
final class Book
{
/**
* @api
*/
public function getName()
{
return $this->name;
}
}
```
23 changes: 23 additions & 0 deletions vendor/tomasvotruba/unused-public/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "tomasvotruba/unused-public",
"type": "phpstan-extension",
"description": "Detect unused public properties, constants and methods in your code",
"license": "MIT",
"require": {
"php": "^7.2|8.0",
"phpstan/phpstan": "^1.9.3",
"nette/utils": "^3.2"
},
"autoload": {
"psr-4": {
"TomasVotruba\\UnusedPublic\\": "src"
}
},
"extra": {
"phpstan": {
"includes": [
"config/extension.neon"
]
}
}
}
Loading

0 comments on commit fe6e403

Please sign in to comment.