Skip to content

Commit eec8f67

Browse files
author
BackEndTea
committed
Initial commit
0 parents  commit eec8f67

21 files changed

+687
-0
lines changed

.build/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset=utf-8
5+
end_of_line=lf
6+
trim_trailing_whitespace=true
7+
insert_final_newline=true
8+
indent_style=space
9+
indent_size=4
10+
11+
[Makefile]
12+
indent_style=tab

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
composer.lock
3+
4+
*.swp
5+
.idea/

LICENSE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Gert de Pagter
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# PHP Depend resolver
2+
3+
## What is this
4+
5+
This library allows you to find the dependencies of a php file.
6+
7+
It was created to help with another libary, that allows you to run tests for only changes files (and their dependencies)
8+
9+
## Usage
10+
```php
11+
<?php
12+
require_once __DIR__.'/vendor/autoload.php';
13+
14+
use Depend\DependencyFinder;
15+
16+
17+
$finder = new DependencyFinder([__DIR__.'/src/', './vendor/psr/container/src', __DIR__.'/tests']);
18+
19+
$finder->build();
20+
21+
$deps = $finder->getAllFilesDependingOn('./tests/Fixtures/Circular/A.php');
22+
23+
foreach ($deps as $dep) {
24+
var_dump($dep);
25+
}
26+
27+
$finder->reBuild(['./src/Domain/User.php', './tests/Domain/User.php', './src/functions.php']);
28+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "backendtea/dependency-finder",
3+
"description": "Find out exactly what classes depend on what other classes",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Gert de Pagter",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.2",
14+
"nikic/php-parser": "^4.2",
15+
"symfony/finder": "^4.3"
16+
},
17+
"require-dev": {
18+
"doctrine/coding-standard": "^6.0",
19+
"infection/infection": "^0.13.3",
20+
"phpstan/phpstan": "^0.11",
21+
"phpunit/phpunit": "^8.2"
22+
},
23+
"config": {
24+
"preferred-install": "dist",
25+
"sort-packages": true
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Depend\\": "src/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Depend\\Test\\": "tests/"
35+
}
36+
}
37+
}

infection.json.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeout": 5,
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
"logs": {
9+
"text": ".build/infection.log"
10+
},
11+
"mutators": {
12+
"@default": true
13+
}
14+
}

phpcs.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="."/>
4+
<arg name="extensions" value="php"/>
5+
<arg name="parallel" value="80"/>
6+
<arg name="cache" value=".build/phpcs-cache.cache" />
7+
<arg name="colors"/>
8+
9+
<!-- Ignore warnings, show progress of the run and show sniff names -->
10+
<arg value="nps"/>
11+
12+
<!-- Directories to be checked -->
13+
<file>src</file>
14+
<file>tests</file>
15+
16+
<!-- Include full Doctrine Coding Standard -->
17+
<rule ref="Doctrine"/>
18+
</ruleset>

phpstan.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
3+
- vendor/phpstan/phpstan/conf/config.levelmax.neon
4+
5+
parameters:
6+
paths:
7+
- src
8+
- tests
9+
tmpDir: %currentWorkingDirectory%/.build

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="./vendor/autoload.php"
5+
colors="true"
6+
executionOrder="random"
7+
verbose="true"
8+
cacheResultFile="./.build/.phpunit.cache"
9+
>
10+
<testsuites>
11+
<testsuite name="Application Test Suite">
12+
<directory>./tests/</directory>
13+
<exclude>./tests/Fixtures</exclude>
14+
</testsuite>
15+
</testsuites>
16+
17+
<filter>
18+
<whitelist>
19+
<directory>./src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Dependency/DependencyResolver.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Depend\Dependency;
6+
7+
use function array_key_exists;
8+
use function array_push;
9+
10+
class DependencyResolver
11+
{
12+
/** @var array<string, true> */
13+
private $resolved = [];
14+
15+
/** @var array<string> */
16+
private $knownDependencies = [];
17+
18+
/**
19+
* Key: Class/ function name
20+
* Value: Files that have a dependency on the class.
21+
*
22+
* @var array<string, array<string>> $dependantMap
23+
*/
24+
private $dependantMap = [];
25+
26+
/**
27+
* Key: File name
28+
* Value: Classes it declares
29+
*
30+
* @var array<string, array<string>> $declareMap
31+
*/
32+
private $declareMap = [];
33+
34+
/**
35+
* @param array<string, array<string>> $declareMap
36+
* @param array<string, array<string>> $dependantMap
37+
*
38+
* @return array<string>
39+
*/
40+
public function resolve(string $fileName, array $declareMap, array $dependantMap) : array
41+
{
42+
$this->declareMap = $declareMap;
43+
$this->dependantMap = $dependantMap;
44+
foreach ($this->declareMap[$fileName]?? [] as $class) {
45+
$this->resolveClass($class);
46+
}
47+
48+
return $this->knownDependencies;
49+
}
50+
51+
private function resolveClass(string $className) : void
52+
{
53+
if (array_key_exists($className, $this->resolved)) {
54+
return;
55+
}
56+
$this->resolved[$className] = true;
57+
58+
$dependants = $this->dependantMap['\\' . $className] ?? [];
59+
60+
array_push($this->knownDependencies, ...$dependants);
61+
foreach ($dependants as $dependant) {
62+
foreach ($this->declareMap[$dependant]?? [] as $class) {
63+
$this->resolveClass($class);
64+
}
65+
}
66+
}
67+
}

src/DependencyFinder.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Depend;
6+
7+
use Depend\Dependency\DependencyResolver;
8+
use Depend\PHPParser\Visitor\DeclarationCollector;
9+
use Depend\PHPParser\Visitor\NameCollector;
10+
use Depend\PHPParser\Visitor\ParentConnectorVisitor;
11+
use PhpParser\NodeTraverser;
12+
use PhpParser\NodeVisitor\NameResolver;
13+
use PhpParser\Parser;
14+
use PhpParser\ParserFactory;
15+
use Symfony\Component\Finder\Finder;
16+
use Symfony\Component\Finder\SplFileInfo;
17+
use function array_unique;
18+
use function file_get_contents;
19+
use function realpath;
20+
21+
class DependencyFinder
22+
{
23+
/** @var array|string[] */
24+
private $directories;
25+
/** @var array|string[] */
26+
private $exclude;
27+
28+
/**
29+
* Key: File name
30+
* Value: Classes/functions that this file depends on.
31+
*
32+
* @var array<string, array<string>> $dependencyMap
33+
*/
34+
private $dependencyMap = [];
35+
36+
/**
37+
* Key: Class/ function name
38+
* Value: Files that have a dependency on the class.
39+
*
40+
* @var array<string, array<string>> $dependantMap
41+
*/
42+
private $dependantMap = [];
43+
44+
/**
45+
* Key: File name
46+
* Value: Classes it declares
47+
*
48+
* @var array<string, array<string>> $declareMap
49+
*/
50+
private $declareMap = [];
51+
52+
/** @var Parser */
53+
private $parser;
54+
55+
/**
56+
* List of directories to build dependencies from.
57+
* for example: `new DependencyFinder(['./src', './tests', './lib']);`
58+
*
59+
* @param array|string[] $directories
60+
* @param array|string[] $exclude
61+
*/
62+
public function __construct(array $directories, array $exclude = [])
63+
{
64+
$this->directories = $directories;
65+
$this->exclude = $exclude;
66+
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
67+
}
68+
69+
public function build() : void
70+
{
71+
/** @var SplFileInfo $file */
72+
foreach (Finder::create()
73+
->in($this->directories)
74+
->exclude($this->exclude)
75+
->files() as $file) {
76+
$path = $file->getRealPath();
77+
if ($path === false) {
78+
$path = '';
79+
}
80+
81+
$this->reBuildFor($path);
82+
}
83+
}
84+
85+
/**
86+
* @return array<string>
87+
*/
88+
public function getAllFilesDependingOn(string $fileName) : array
89+
{
90+
$path = realpath($fileName);
91+
if ($path === false) {
92+
return [];
93+
}
94+
95+
return(new DependencyResolver())->resolve($path, $this->declareMap, $this->dependantMap);
96+
}
97+
98+
/**
99+
* @param array|string[] $fileNames
100+
*/
101+
public function reBuild(array $fileNames) : void
102+
{
103+
foreach ($fileNames as $fileName) {
104+
$this->reBuildFor($fileName);
105+
}
106+
}
107+
108+
private function reBuildFor(string $fileName) : void
109+
{
110+
$info = $this->traversePath($fileName);
111+
$this->dependencyMap[$fileName] = array_unique($info->dependencies);
112+
$this->declareMap[$fileName] = $info->declares;
113+
foreach ($info->dependencies as $dependency) {
114+
$this->dependantMap[$dependency][] = $fileName;
115+
}
116+
}
117+
118+
private function traversePath(string $filePath) : File
119+
{
120+
$content = file_get_contents($filePath);
121+
if ($content === false) {
122+
return new File();
123+
}
124+
125+
$nodes = $this->parser->parse($content);
126+
$traverser = new NodeTraverser();
127+
$traverser->addVisitor(new NameResolver());
128+
$traverser->addVisitor(new ParentConnectorVisitor());
129+
$nameCollector = new NameCollector();
130+
$declareCollector = new DeclarationCollector();
131+
$traverser->addVisitor($nameCollector);
132+
$traverser->addVisitor($declareCollector);
133+
$traverser->traverse($nodes);
134+
135+
return new File($declareCollector->declared, $nameCollector->resolvedNames);
136+
}
137+
}

0 commit comments

Comments
 (0)