ComposerLocator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 41
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 6
A locateClass() 0 9 2
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\ParserReflection\Locator;
12
13
use Composer\Autoload\ClassLoader;
14
use Go\ParserReflection\Instrument\PathResolver;
15
use Go\ParserReflection\LocatorInterface;
16
use Go\ParserReflection\ReflectionException;
17
18
/**
19
 * Locator, that can find a file for the given class name by asking composer
20
 */
21
class ComposerLocator implements LocatorInterface
22
{
23
    /**
24
     * @var ClassLoader
25
     */
26
    private $loader;
27
28 1
    public function __construct(ClassLoader $loader = null)
29
    {
30 1
        if (!$loader) {
31 1
            $loaders = spl_autoload_functions();
32 1
            foreach ($loaders as $loader) {
33 1
                if (is_array($loader) && $loader[0] instanceof ClassLoader) {
34 1
                    $loader = $loader[0];
35 1
                    break;
36
                }
37
            }
38 1
            if (!$loader) {
39
                throw new ReflectionException("Can not found a correct composer loader");
40
            }
41
        }
42 1
        $this->loader = $loader;
43 1
    }
44
45
    /**
46
     * Returns a path to the file for given class name
47
     *
48
     * @param string $className Name of the class
49
     *
50
     * @return string|false Path to the file with given class or false if not found
51
     */
52 1
    public function locateClass($className)
53
    {
54 1
        $filePath = $this->loader->findFile(ltrim($className, '\\'));
55 1
        if (!empty($filePath)) {
56 1
            $filePath = PathResolver::realpath($filePath);
57
        }
58
59 1
        return $filePath;
60
    }
61
}
62