Total Lines | 132 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | protected function createContext( |
||
30 | CharBufferInterface $buffer, |
||
31 | TokenFactoryInterface $tokenFactory |
||
32 | ): TokenMatcherContextInterface { |
||
33 | $onConstruct = function (): void { |
||
34 | unset($this->token); |
||
35 | }; |
||
36 | $onSetNewToken = function (int $tokenType) use ($tokenFactory): void { |
||
37 | $this->token = $tokenFactory->createToken($tokenType); |
||
38 | }; |
||
39 | $onGetToken = function (): Token { |
||
40 | return $this->getToken(); |
||
41 | }; |
||
42 | $onSetMode = function (string $mode): void { |
||
43 | $this->mode = $mode; |
||
44 | }; |
||
45 | $onGetMode = function (): string { |
||
46 | return $this->mode; |
||
47 | }; |
||
48 | |||
49 | return new class ( |
||
50 | $buffer, |
||
51 | $onConstruct, |
||
52 | $onSetNewToken, |
||
53 | $onGetToken, |
||
54 | $onSetMode, |
||
55 | $onGetMode |
||
56 | ) implements TokenMatcherContextInterface { |
||
57 | |||
58 | private $buffer; |
||
59 | |||
60 | private $onSetNewToken; |
||
61 | |||
62 | private $onGetToken; |
||
63 | |||
64 | private $onSetMode; |
||
65 | |||
66 | private $onGetMode; |
||
67 | |||
68 | private $visitedTransitions = []; |
||
69 | |||
70 | public function __construct( |
||
71 | CharBufferInterface $buffer, |
||
72 | callable $onConstruct, |
||
73 | callable $onSetNewToken, |
||
74 | callable $onGetToken, |
||
75 | callable $onSetMode, |
||
76 | callable $onGetMode |
||
77 | ) { |
||
78 | $this->buffer = $buffer; |
||
79 | $this->onSetNewToken = $onSetNewToken; |
||
80 | $this->onGetToken = $onGetToken; |
||
81 | $this->onSetMode = $onSetMode; |
||
82 | $this->onGetMode = $onGetMode; |
||
83 | call_user_func($onConstruct); |
||
84 | } |
||
85 | |||
86 | public function setNewToken(int $tokenType): TokenMatcherContextInterface |
||
87 | { |
||
88 | call_user_func($this->onSetNewToken, $tokenType); |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | * @param $value |
||
96 | * @return TokenMatcherContextInterface |
||
97 | */ |
||
98 | public function setTokenAttribute(string $name, $value): TokenMatcherContextInterface |
||
99 | { |
||
100 | $this |
||
101 | ->getToken() |
||
102 | ->setAttribute($name, $value); |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function getToken(): Token |
||
108 | { |
||
109 | return call_user_func($this->onGetToken); |
||
110 | } |
||
111 | |||
112 | public function getBuffer(): CharBufferInterface |
||
113 | { |
||
114 | return $this->buffer; |
||
115 | } |
||
116 | |||
117 | public function getSymbolString(): string |
||
118 | { |
||
119 | $buffer = $this->getBuffer(); |
||
120 | if ($buffer instanceof TokenExtractInterface) { |
||
121 | return $buffer->getTokenAsString(); |
||
122 | } |
||
123 | throw new Exception("Extracting strings is not supported by buffer"); |
||
124 | } |
||
125 | |||
126 | public function getSymbolList(): array |
||
127 | { |
||
128 | $buffer = $this->getBuffer(); |
||
129 | if ($buffer instanceof TokenExtractInterface) { |
||
130 | return $buffer->getTokenAsArray(); |
||
131 | } |
||
132 | throw new Exception("Extracting arrays is not supported by buffer"); |
||
133 | } |
||
134 | |||
135 | public function getMode(): string |
||
136 | { |
||
137 | return call_user_func($this->onGetMode); |
||
138 | } |
||
139 | |||
140 | public function setMode(string $mode): TokenMatcherContextInterface |
||
141 | { |
||
142 | call_user_func($this->onSetMode, $mode); |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function visitTransition(string $hash): void |
||
148 | { |
||
149 | $this->visitedTransitions[$hash] = true; |
||
150 | } |
||
151 | |||
152 | public function isVisitedTransition(string ...$hashes): bool |
||
153 | { |
||
154 | foreach ($hashes as $hash) { |
||
155 | if (isset($this->visitedTransitions[$hash])) { |
||
156 | return true; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return false; |
||
161 | } |
||
165 |