ClassUseStatements::isNotUserDefined()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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