ToolPathFinder::find()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
namespace PhpGitHooks\Infrastructure\Tool;
4
5
class ToolPathFinder
6
{
7
    const COMPOSER_VENDOR_DIR = '/../../../';
8
    const COMPOSER_INSTALLED_FILE = 'composer/installed.json';
9
10
    /** @var array */
11
    private $tools = array(
12
        'phpcs' => 'squizlabs/php_codesniffer',
13
        'php-cs-fixer' => 'friendsofphp/php-cs-fixer',
14
        'phpmd' => 'phpmd/phpmd',
15
        'phpunit' => 'phpunit/phpunit',
16
        'phpunit-randomizer' => 'fiunchinho/phpunit-randomizer',
17
        'jsonlint' => 'seld/jsonlint',
18
    );
19
    /** @var array */
20
    private $installedPackages = array();
21
22
    /**
23
     * @param string $tool
24
     *
25
     * @return string
26
     */
27
    public function find($tool)
28
    {
29
        if (isset($this->installedPackages[$this->tools[$tool]])) {
30
            $package = $this->installedPackages[$this->tools[$tool]];
31
            foreach ($package['bin'] as $bin) {
32
                if (preg_match("#${tool}$#", $bin)) {
33
                    return __DIR__.self::COMPOSER_VENDOR_DIR.$package['name'].DIRECTORY_SEPARATOR.$bin;
34
                }
35
            }
36
        }
37
38
        $binToolPath = 'bin'.DIRECTORY_SEPARATOR.$tool;
39
        if (file_exists($binToolPath) && is_file($binToolPath)) {
40
            return $binToolPath;
41
        }
42
43
        return 'vendor'.DIRECTORY_SEPARATOR.$binToolPath;
44
    }
45
}
46