FileAnalyser::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * FileAnalyser.php
4
 *
5
 * MIT LICENSE
6
 *
7
 * LICENSE: This source file is subject to the MIT license.
8
 * A copy of the licenses text was distributed alongside this
9
 * file (usually the repository or package root). The text can also
10
 * be obtained through one of the following sources:
11
 * * http://opensource.org/licenses/MIT
12
 * * https://github.com/suralc/pvra/blob/master/LICENSE
13
 *
14
 * @author     suralc <[email protected]>
15
 * @license    http://opensource.org/licenses/MIT  MIT
16
 */
17
namespace Pvra;
18
19
/**
20
 * Class FileAnalyser
21
 *
22
 * @package Pvra
23
 */
24
class FileAnalyser extends Analyser
25
{
26
    /**
27
     * The relative or absolute path to the file target of this analysis.
28
     *
29
     * @var string
30
     */
31
    private $filePath;
32
33
    /**
34
     * FileAnalyser constructor
35
     *
36
     * Validates the given file path and calls the parent's constructor.     *
37
     *
38
     * @param string $file The code to analyse
39
     * @param bool $registerNameResolver If set to true `PhpParser\NodeVisitor\NameResolver` will be added as the first
40
     *     visitor. This may negatively affect performance, some Visitors depend on resolved names, however.
41
     * @see \Pvra\RequirementAnalyser::__construct() Base constructor
42
     */
43 56
    public function __construct($file, $registerNameResolver = true)
44
    {
45 56
        if (!$this->isFileValid($file)) {
46 4
            throw new \RuntimeException(sprintf('The file "%s" could not be found or accessed.', $file));
47
        }
48
49 52
        $this->filePath = realpath($file);
50 52
        parent::__construct($registerNameResolver);
51 52
    }
52
53
    /**
54
     * Validate a given file path.
55
     *
56
     * @param string $file Path to the file
57
     * @return bool Returns true if $file is a file and is readable. Returns fails otherwise.
58
     */
59 56
    private function isFileValid($file)
60
    {
61 56
        return is_file($file) && is_readable($file);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 46
    protected function createAnalysisTargetId()
68
    {
69 46
        return $this->filePath;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 44
    protected function parse()
76
    {
77 44
        return $this->getParser()->parse(file_get_contents($this->filePath));
78
    }
79
}
80