Skip to content

Commit 701b485

Browse files
authored
Readme: improve, mainly config section (#127)
1 parent 06ab7c6 commit 701b485

File tree

1 file changed

+19
-51
lines changed

1 file changed

+19
-51
lines changed

README.md

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This tool reads your `composer.json` and scans all paths listed in `autoload` &
7373
- Any class that cannot be autoloaded gets reported as we cannot say if that one is shadowed or not
7474

7575
### Unknown functions
76-
- Any function that is used, but not defined gets reported as we cannot say if that one is shadowed or not
76+
- Any function that is used, but not defined during runtime gets reported as we cannot say if that one is shadowed or not
7777

7878
## Cli options:
7979
- `--composer-json path/to/composer.json` for custom path to composer.json
@@ -106,69 +106,39 @@ use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
106106
$config = new Configuration();
107107

108108
return $config
109-
// disable scanning autoload & autoload-dev paths from composer.json
110-
// with such option, you should add custom paths by addPathToScan() or addPathsToScan()
111-
->disableComposerAutoloadPathScan()
112-
113-
// report unused dependencies even for dev packages
114-
// dev packages are often used only in CI, so this is not enabled by default
115-
// but you may want to ignore those packages manually to be sure
116-
->enableAnalysisOfUnusedDevDependencies()
117-
118-
// do not report ignores that never matched any error
119-
->disableReportingUnmatchedIgnores()
120-
121-
// globally disable specific error type
122-
->ignoreErrors([ErrorType::DEV_DEPENDENCY_IN_PROD])
123-
124-
// overwrite file extensions to scan, defaults to 'php'
125-
// applies only to directory scanning, not directly listed files
126-
->setFileExtensions(['php'])
127-
128-
// add extra path to scan
129-
// for multiple paths at once, use addPathsToScan()
109+
//// Adjusting scanned paths
130110
->addPathToScan(__DIR__ . '/build', isDev: false)
131-
132-
// exclude path from scanning
133-
// for multiple paths at once, use addPathsToExclude()
134111
->addPathToExclude(__DIR__ . '/samples')
112+
->disableComposerAutoloadPathScan() // disable automatic scan of autoload & autoload-dev paths from composer.json
113+
->setFileExtensions(['php']) // applies only to directory scanning, not directly listed files
135114

136-
// ignore errors on specific paths
137-
// this can be handy when DIC container file was passed as extra path, but you want to ignore shadow dependencies there
138-
// for multiple paths at once, use ignoreErrorsOnPaths()
115+
//// Ignoring errors
116+
->ignoreErrors([ErrorType::DEV_DEPENDENCY_IN_PROD])
139117
->ignoreErrorsOnPath(__DIR__ . '/cache/DIC.php', [ErrorType::SHADOW_DEPENDENCY])
140-
141-
// ignore errors on specific packages
142-
// you might have various reasons to ignore certain errors
143-
// e.g. polyfills are often used in libraries, but those are obviously unused when running with latest PHP
144-
// for multiple packages at once, use ignoreErrorsOnPackages()
145118
->ignoreErrorsOnPackage('symfony/polyfill-php73', [ErrorType::UNUSED_DEPENDENCY])
146-
147-
// ignore errors on specific packages and paths
148-
// for multiple, use ignoreErrorsOnPackagesAndPaths() or ignoreErrorsOnPackageAndPaths()
149119
->ignoreErrorsOnPackageAndPath('symfony/console', __DIR__ . '/src/OptionalCommand.php', [ErrorType::SHADOW_DEPENDENCY])
150120

151-
// allow using non-autoloadable classes
152-
// e.g. a library may conditionally support some feature only when Memcached is available
121+
//// Ignoring unknown symbols
153122
->ignoreUnknownClasses(['Memcached'])
154123
->ignoreUnknownClassesRegex('~^DDTrace~')
155-
156-
// allow using functions not defined during runtime
157124
->ignoreUnknownFunctions(['opcache_invalidate'])
158125
->ignoreUnknownFunctionsRegex('~^opcache_~')
159126

160-
// force certain classes to be treated as used
161-
// handy when dealing with dependencies in non-php files (e.g. DIC config), see example below
162-
// beware that those are not validated and do not even trigger unknown class error
127+
//// Adjust analysis
128+
->enableAnalysisOfUnusedDevDependencies() // dev packages are often used only in CI, so this is not enabled by default
129+
->disableReportingUnmatchedIgnores() // do not report ignores that never matched any error
130+
131+
//// Use symbols from yaml/xml/neon files
132+
// - designed for DIC config files (see below)
133+
// - beware that those are not validated and do not even trigger unknown class error
163134
->addForceUsedSymbols($classesExtractedFromNeonJsonYamlXmlEtc)
164-
;
165135
```
166136

167137
All paths are expected to exist. If you need some glob functionality, you can do it in your config file and pass the expanded list to e.g. `ignoreErrorsOnPaths`.
168138

169139
### Detecting classes from non-php files:
170140

171-
Simplest fuzzy search for classnames within your yaml/neon/xml/json files might look like this:
141+
Some classes might be used only in your DIC config files. Here is a simple way to extract those:
172142

173143
```php
174144
$classNameRegex = '[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'; // https://www.php.net/manual/en/language.oop5.basic.php
@@ -183,18 +153,17 @@ preg_match_all(
183153
$config->addForceUsedSymbols($matches[1]); // possibly filter by class_exists || interface_exists
184154
```
185155

186-
Similar approach should help you to avoid false positives in unused dependencies due to the usages being present in e.g. DIC config files only.
156+
Similar approach should help you to avoid false positives in unused dependencies.
187157
Another approach for DIC-only usages is to scan the generated php file, but that gave us worse results.
188158

159+
### Scanning codebase located elsewhere:
160+
- This can be done by pointing `--composer-json` to `composer.json` of the other codebase
161+
189162
## Limitations:
190163
- Extension dependencies are not analysed (e.g. `ext-json`)
191164
- Files without namespace has limited support
192165
- Only symbols with use statements and FQNs are detected
193166

194-
-----
195-
196-
Despite those limitations, our experience is that this composer-dependency-analyser works much better than composer-unused and composer-require-checker.
197-
198167
## Contributing:
199168
- Check your code by `composer check`
200169
- Autofix coding-style by `composer fix:cs`
@@ -203,4 +172,3 @@ Despite those limitations, our experience is that this composer-dependency-analy
203172
## Supported PHP versions
204173
- Runtime requires PHP 7.2 - 8.3
205174
- Scanned codebase should use PHP >= 5.3
206-
- This can be done by pointing `--composer-json` to codebase located elsewhere

0 commit comments

Comments
 (0)