Conditions | 1 |
Paths | 1 |
Total Lines | 112 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 namespace Limoncello\Application\Commands; |
||
73 | protected static function createCliPassport( |
||
74 | $userIdentity, |
||
75 | Closure $readUserScopes, |
||
76 | array $properties |
||
77 | ): PassportAccountInterface { |
||
78 | return new class ($userIdentity, $readUserScopes, $properties) implements PassportAccountInterface |
||
79 | { |
||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | private $properties; |
||
84 | |||
85 | /** |
||
86 | * @var int |
||
87 | */ |
||
88 | private $userIdentity; |
||
89 | |||
90 | /** |
||
91 | * @var Closure |
||
92 | */ |
||
93 | private $readUserScopes; |
||
94 | |||
95 | /** |
||
96 | * @param int|string $userIdentity |
||
97 | * @param Closure $readUserScopes |
||
98 | * @param array $properties |
||
99 | */ |
||
100 | public function __construct($userIdentity, Closure $readUserScopes, array $properties) |
||
101 | { |
||
102 | assert (is_int($userIdentity) === true || is_string($userIdentity) === true); |
||
103 | |||
104 | $this->userIdentity = $userIdentity; |
||
|
|||
105 | $this->properties = $properties; |
||
106 | $this->readUserScopes = $readUserScopes; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @inheritdoc |
||
111 | */ |
||
112 | public function hasProperty($key): bool |
||
113 | { |
||
114 | return array_key_exists($key, $this->properties); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public function getProperty($key) |
||
121 | { |
||
122 | return $this->properties[$key]; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | public function hasUserIdentity(): bool |
||
129 | { |
||
130 | return true; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | public function getUserIdentity() |
||
137 | { |
||
138 | return $this->userIdentity; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public function hasClientIdentity(): bool |
||
145 | { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @inheritdoc |
||
151 | */ |
||
152 | public function getClientIdentity() |
||
153 | { |
||
154 | return null; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | public function hasScope(string $scope): bool |
||
161 | { |
||
162 | // we typically do just one call during a session so it's fine to work with unsorted data. |
||
163 | return in_array($scope, $this->getScopes()); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | public function hasScopes(): bool |
||
170 | { |
||
171 | return true; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | public function getScopes(): array |
||
178 | { |
||
179 | $scopes = call_user_func($this->readUserScopes, $this->getUserIdentity()); |
||
180 | |||
181 | return $scopes; |
||
182 | } |
||
183 | }; |
||
184 | } |
||
185 | } |
||
186 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.