Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea committed Aug 10, 2019
0 parents commit eec8f67
Show file tree
Hide file tree
Showing 21 changed files with 687 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset=utf-8
end_of_line=lf
trim_trailing_whitespace=true
insert_final_newline=true
indent_style=space
indent_size=4

[Makefile]
indent_style=tab
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
composer.lock

*.swp
.idea/
16 changes: 16 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The MIT License (MIT)

Copyright (c) 2019 Gert de Pagter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# PHP Depend resolver

## What is this

This library allows you to find the dependencies of a php file.

It was created to help with another libary, that allows you to run tests for only changes files (and their dependencies)

## Usage
```php
<?php
require_once __DIR__.'/vendor/autoload.php';

use Depend\DependencyFinder;


$finder = new DependencyFinder([__DIR__.'/src/', './vendor/psr/container/src', __DIR__.'/tests']);

$finder->build();

$deps = $finder->getAllFilesDependingOn('./tests/Fixtures/Circular/A.php');

foreach ($deps as $dep) {
var_dump($dep);
}

$finder->reBuild(['./src/Domain/User.php', './tests/Domain/User.php', './src/functions.php']);
```
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "backendtea/dependency-finder",
"description": "Find out exactly what classes depend on what other classes",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Gert de Pagter",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2",
"nikic/php-parser": "^4.2",
"symfony/finder": "^4.3"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"infection/infection": "^0.13.3",
"phpstan/phpstan": "^0.11",
"phpunit/phpunit": "^8.2"
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": {
"Depend\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Depend\\Test\\": "tests/"
}
}
}
14 changes: 14 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"timeout": 5,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": ".build/infection.log"
},
"mutators": {
"@default": true
}
}
18 changes: 18 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".build/phpcs-cache.cache" />
<arg name="colors"/>

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<!-- Directories to be checked -->
<file>src</file>
<file>tests</file>

<!-- Include full Doctrine Coding Standard -->
<rule ref="Doctrine"/>
</ruleset>
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- vendor/phpstan/phpstan/conf/config.levelmax.neon

parameters:
paths:
- src
- tests
tmpDir: %currentWorkingDirectory%/.build
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
executionOrder="random"
verbose="true"
cacheResultFile="./.build/.phpunit.cache"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
<exclude>./tests/Fixtures</exclude>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
67 changes: 67 additions & 0 deletions src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Depend\Dependency;

use function array_key_exists;
use function array_push;

class DependencyResolver
{
/** @var array<string, true> */
private $resolved = [];

/** @var array<string> */
private $knownDependencies = [];

/**
* Key: Class/ function name
* Value: Files that have a dependency on the class.
*
* @var array<string, array<string>> $dependantMap
*/
private $dependantMap = [];

/**
* Key: File name
* Value: Classes it declares
*
* @var array<string, array<string>> $declareMap
*/
private $declareMap = [];

/**
* @param array<string, array<string>> $declareMap
* @param array<string, array<string>> $dependantMap
*
* @return array<string>
*/
public function resolve(string $fileName, array $declareMap, array $dependantMap) : array
{
$this->declareMap = $declareMap;
$this->dependantMap = $dependantMap;
foreach ($this->declareMap[$fileName]?? [] as $class) {
$this->resolveClass($class);
}

return $this->knownDependencies;
}

private function resolveClass(string $className) : void
{
if (array_key_exists($className, $this->resolved)) {
return;
}
$this->resolved[$className] = true;

$dependants = $this->dependantMap['\\' . $className] ?? [];

array_push($this->knownDependencies, ...$dependants);
foreach ($dependants as $dependant) {
foreach ($this->declareMap[$dependant]?? [] as $class) {
$this->resolveClass($class);
}
}
}
}
137 changes: 137 additions & 0 deletions src/DependencyFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

namespace Depend;

use Depend\Dependency\DependencyResolver;
use Depend\PHPParser\Visitor\DeclarationCollector;
use Depend\PHPParser\Visitor\NameCollector;
use Depend\PHPParser\Visitor\ParentConnectorVisitor;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use function array_unique;
use function file_get_contents;
use function realpath;

class DependencyFinder
{
/** @var array|string[] */
private $directories;
/** @var array|string[] */
private $exclude;

/**
* Key: File name
* Value: Classes/functions that this file depends on.
*
* @var array<string, array<string>> $dependencyMap
*/
private $dependencyMap = [];

/**
* Key: Class/ function name
* Value: Files that have a dependency on the class.
*
* @var array<string, array<string>> $dependantMap
*/
private $dependantMap = [];

/**
* Key: File name
* Value: Classes it declares
*
* @var array<string, array<string>> $declareMap
*/
private $declareMap = [];

/** @var Parser */
private $parser;

/**
* List of directories to build dependencies from.
* for example: `new DependencyFinder(['./src', './tests', './lib']);`
*
* @param array|string[] $directories
* @param array|string[] $exclude
*/
public function __construct(array $directories, array $exclude = [])
{
$this->directories = $directories;
$this->exclude = $exclude;
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
}

public function build() : void
{
/** @var SplFileInfo $file */
foreach (Finder::create()
->in($this->directories)
->exclude($this->exclude)
->files() as $file) {
$path = $file->getRealPath();
if ($path === false) {
$path = '';
}

$this->reBuildFor($path);
}
}

/**
* @return array<string>
*/
public function getAllFilesDependingOn(string $fileName) : array
{
$path = realpath($fileName);
if ($path === false) {
return [];
}

return(new DependencyResolver())->resolve($path, $this->declareMap, $this->dependantMap);
}

/**
* @param array|string[] $fileNames
*/
public function reBuild(array $fileNames) : void
{
foreach ($fileNames as $fileName) {
$this->reBuildFor($fileName);
}
}

private function reBuildFor(string $fileName) : void
{
$info = $this->traversePath($fileName);
$this->dependencyMap[$fileName] = array_unique($info->dependencies);
$this->declareMap[$fileName] = $info->declares;
foreach ($info->dependencies as $dependency) {
$this->dependantMap[$dependency][] = $fileName;
}
}

private function traversePath(string $filePath) : File
{
$content = file_get_contents($filePath);
if ($content === false) {
return new File();
}

$nodes = $this->parser->parse($content);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor(new ParentConnectorVisitor());
$nameCollector = new NameCollector();
$declareCollector = new DeclarationCollector();
$traverser->addVisitor($nameCollector);
$traverser->addVisitor($declareCollector);
$traverser->traverse($nodes);

return new File($declareCollector->declared, $nameCollector->resolvedNames);
}
}
Loading

0 comments on commit eec8f67

Please sign in to comment.