Total Lines | 114 |
Code Lines | 48 |
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 |
||
30 | protected function createContext( |
||
31 | CharBufferInterface $buffer, |
||
32 | TokenFactoryInterface $tokenFactory |
||
33 | ): TokenMatcherContextInterface { |
||
34 | $onConstruct = function (): void { |
||
35 | unset($this->token); |
||
36 | }; |
||
37 | $onSetNewToken = function (int $tokenType) use ($tokenFactory): void { |
||
38 | $this->token = $tokenFactory->createToken($tokenType); |
||
39 | }; |
||
40 | $onGetToken = function (): Token { |
||
41 | return $this->getToken(); |
||
42 | }; |
||
43 | $onSetMode = function (string $mode): void { |
||
44 | $this->mode = $mode; |
||
45 | }; |
||
46 | $onGetMode = function (): string { |
||
47 | return $this->mode; |
||
48 | }; |
||
49 | |||
50 | return new class ( |
||
51 | $buffer, |
||
52 | $onConstruct, |
||
53 | $onSetNewToken, |
||
54 | $onGetToken, |
||
55 | $onSetMode, |
||
56 | $onGetMode |
||
57 | ) implements TokenMatcherContextInterface |
||
58 | { |
||
59 | private $buffer; |
||
60 | |||
61 | private $onSetNewToken; |
||
62 | |||
63 | private $onGetToken; |
||
64 | |||
65 | private $onSetMode; |
||
66 | |||
67 | private $onGetMode; |
||
68 | |||
69 | public function __construct( |
||
70 | CharBufferInterface $buffer, |
||
71 | callable $onConstruct, |
||
72 | callable $onSetNewToken, |
||
73 | callable $onGetToken, |
||
74 | callable $onSetMode, |
||
75 | callable $onGetMode |
||
76 | ) { |
||
77 | $this->buffer = $buffer; |
||
78 | $this->onSetNewToken = $onSetNewToken; |
||
79 | $this->onGetToken = $onGetToken; |
||
80 | $this->onSetMode = $onSetMode; |
||
81 | $this->onGetMode = $onGetMode; |
||
82 | call_user_func($onConstruct); |
||
83 | } |
||
84 | |||
85 | public function setNewToken(int $tokenType): TokenMatcherContextInterface |
||
86 | { |
||
87 | call_user_func($this->onSetNewToken, $tokenType); |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $name |
||
94 | * @param $value |
||
95 | * @return TokenMatcherContextInterface |
||
96 | */ |
||
97 | public function setTokenAttribute(string $name, $value): TokenMatcherContextInterface |
||
98 | { |
||
99 | $this |
||
100 | ->getToken() |
||
101 | ->setAttribute($name, $value); |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | public function getToken(): Token |
||
107 | { |
||
108 | return call_user_func($this->onGetToken); |
||
109 | } |
||
110 | |||
111 | public function getBuffer(): CharBufferInterface |
||
112 | { |
||
113 | return $this->buffer; |
||
114 | } |
||
115 | |||
116 | public function getSymbolString(): string |
||
117 | { |
||
118 | $buffer = $this->getBuffer(); |
||
119 | if ($buffer instanceof TokenExtractInterface) { |
||
120 | return $buffer->getTokenAsString(); |
||
121 | } |
||
122 | throw new Exception("Extracting strings is not supported by buffer"); |
||
123 | } |
||
124 | |||
125 | public function getSymbolList(): array |
||
126 | { |
||
127 | $buffer = $this->getBuffer(); |
||
128 | if ($buffer instanceof TokenExtractInterface) { |
||
129 | return $buffer->getTokenAsArray(); |
||
130 | } |
||
131 | throw new Exception("Extracting arrays is not supported by buffer"); |
||
132 | } |
||
133 | |||
134 | public function getMode(): string |
||
135 | { |
||
136 | return call_user_func($this->onGetMode); |
||
137 | } |
||
138 | |||
139 | public function setMode(string $mode): TokenMatcherContextInterface |
||
140 | { |
||
141 | call_user_func($this->onSetMode, $mode); |
||
142 | |||
143 | return $this; |
||
144 | } |
||
148 |