Test Failed
Push — master ( 31f356...780ea5 )
by Alexey
02:13
created

ClassUseStatements::getUseStatements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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\UsesBlockParser;
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 $class
42
     * @return boolean
43
     */
44
    public function hasUseStatement(string $class): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
        return true;
46
    }
47
48
    /**
49
     * @return bool
50
     */
51 1
    public function isNotUserDefined(): bool {
52 1
        return !$this->isUserDefined();
53
    }
54
55
    /**
56
     * @return boolean
57
     */
58 1
    private function isUseStatementsNotParsed(): bool {
59 1
        return !$this->isUseStatementsParsed;
60
    }
61
62
    /**
63
     * @return ClassUseStatements
64
     */
65 1
    private function setUseStatementsIsParsed(): ClassUseStatements {
66 1
        $this->isUseStatementsParsed = true;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @return UseStatements
73
     */
74 1
    private function createUseStatements(): UseStatements {
75 1
        if ($this->isNotUserDefined()) {
76
            throw new RuntimeException('Can get use statements from user defined classes only.');
77
        }
78
79 1
        $this->setUseStatementsIsParsed();
80
81 1
        return (new UsesBlockParser($this->readUsesBlock()))
82 1
            ->getUseStatements();
83
    }
84
85
    /**
86
     * @return string
87
     */
88 1
    private function readUsesBlock(): string {
89 1
        $file = fopen($this->getFileName(), 'r');
90 1
        $line = 0;
91 1
        $source = '';
92
93 1
        while (!feof($file)) {
94 1
            ++$line;
95
96 1
            if ($line >= $this->getStartLine()) {
97 1
                break;
98
            }
99
100 1
            $source .= fgets($file);
101
        }
102
103 1
        fclose($file);
104
105 1
        return $source;
106
    }
107
108
}