Test Failed
Push — master ( e75b27...c4cefa )
by Alexey
20:10
created

ReflectionUseStatements::readUsesBlock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Reflection;
6
7
use \ReflectionClass;
8
use \RuntimeException;
9
10
/**
11
 * Class ReflectionUseStatements
12
 * @package UsesReflection
13
 */
14
class ReflectionUseStatements extends ReflectionClass {
15
16
    /**
17
     * @var UseStatements
18
     */
19
    private $useStatements;
20
21
    /**
22
     * @var boolean
23
     */
24
    private $isUseStatementsParsed = false;
25
26
    /**
27
     * @return UseStatements
28
     */
29 1
    public function getUseStatements(): UseStatements {
30 1
        if ($this->isUseStatementsNotParsed()) {
31 1
            $this->useStatements = $this->createUseStatements();
32
        }
33
34 1
        return $this->useStatements;
35
    }
36
37
    /**
38
     * @param string $class
39
     * @return boolean
40
     */
41 1
    public function hasUseStatement(string $class): bool {
42 1
        return $this->getUseStatements()
43 1
            ->hasClass($class);
44
    }
45
46
    /**
47
     * @return bool
48
     */
49 1
    public function isNotUserDefined(): bool {
50 1
        return !$this->isUserDefined();
51
    }
52
53
    /**
54
     * @return boolean
55
     */
56 1
    private function isUseStatementsNotParsed(): bool {
57 1
        return !$this->isUseStatementsParsed;
58
    }
59
60
    /**
61
     * @return ReflectionUseStatements
62
     */
63 1
    private function setUseStatementsIsParsed(): ReflectionUseStatements {
64 1
        $this->isUseStatementsParsed = true;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @return UseStatements
71
     */
72 1
    private function createUseStatements(): UseStatements {
73 1
        if ($this->isNotUserDefined()) {
74 1
            throw new RuntimeException('Can get use statements from user defined classes only.');
75
        }
76
77 1
        $this->setUseStatementsIsParsed();
78
79 1
        return (new UsesBlockParser($this->readUsesBlock()))
80 1
            ->getUseStatements();
81
    }
82
83
    /**
84
     * @return string
85
     */
86 1
    private function readUsesBlock(): string {
87 1
        $file = fopen($this->getFileName(), 'r');
88 1
        $line = 0;
89 1
        $source = '';
90
91 1
        while (!feof($file)) {
92 1
            ++$line;
93
94 1
            if ($line >= $this->getStartLine()) {
95 1
                break;
96
            }
97
98 1
            $source .= fgets($file);
99
        }
100
101 1
        fclose($file);
102
103 1
        return $source;
104
    }
105
106
}