ComposerLocator::locateClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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