| Total Complexity | 54 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Executor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Executor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class Executor |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The variable list |
||
| 62 | * @var array<string, mixed> |
||
| 63 | */ |
||
| 64 | protected array $variables = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The callable that will be called if variable not found |
||
| 68 | * @var callable|null |
||
| 69 | */ |
||
| 70 | protected $variableNotFoundHandler = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The callable that will be called for variable validation |
||
| 74 | * @var callable|null |
||
| 75 | */ |
||
| 76 | protected $variableValidationHandler = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The list of operators |
||
| 80 | * @var array<string, Operator> |
||
| 81 | */ |
||
| 82 | protected array $operators = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The list of functions |
||
| 86 | * @var array<string, CustomFunction> |
||
| 87 | */ |
||
| 88 | protected array $functions = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The list of cache |
||
| 92 | * @var array<string, Token[]> |
||
| 93 | */ |
||
| 94 | protected array $caches = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create new instance |
||
| 98 | */ |
||
| 99 | public function __construct() |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * When do clone of this object |
||
| 106 | */ |
||
| 107 | public function __clone() |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Execute the expression and return the result |
||
| 114 | * @param string $expression |
||
| 115 | * @param bool $cache |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function execute(string $expression, bool $cache = true): mixed |
||
| 119 | { |
||
| 120 | $cacheKey = $expression; |
||
| 121 | if (!array_key_exists($cacheKey, $this->caches)) { |
||
| 122 | $tokens = (new Tokenizer($expression, $this->operators)) |
||
| 123 | ->tokenize() |
||
| 124 | ->buildReversePolishNotation(); |
||
| 125 | |||
| 126 | if ($cache) { |
||
| 127 | $this->caches[$cacheKey] = $tokens; |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | $tokens = $this->caches[$cacheKey]; |
||
| 131 | } |
||
| 132 | |||
| 133 | $calculator = new Calculator($this->functions, $this->operators); |
||
| 134 | |||
| 135 | return $calculator->calculate( |
||
| 136 | $tokens, |
||
| 137 | $this->variables, |
||
| 138 | $this->variableNotFoundHandler |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add new operator |
||
| 144 | * @param Operator $operator |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function addOperator(Operator $operator): self |
||
| 148 | { |
||
| 149 | $this->operators[$operator->getOperator()] = $operator; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Add new function |
||
| 155 | * @param string $name |
||
| 156 | * @param callable $function |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function addFunction(string $name, callable $function): self |
||
| 160 | { |
||
| 161 | $this->functions[$name] = new CustomFunction($name, $function); |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Return the list of variables |
||
| 167 | * @return array<string, int|float> |
||
| 168 | */ |
||
| 169 | public function getVariables(): array |
||
| 170 | { |
||
| 171 | return $this->variables; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return the value for the given variable name |
||
| 176 | * @param string $name |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getVariable(string $name): mixed |
||
| 180 | { |
||
| 181 | if (! array_key_exists($name, $this->variables)) { |
||
| 182 | if ($this->variableNotFoundHandler !== null) { |
||
| 183 | return call_user_func($this->variableNotFoundHandler, $name); |
||
| 184 | } |
||
| 185 | |||
| 186 | throw new UnknownVariableException(sprintf( |
||
| 187 | 'Unknown variable [%s]', |
||
| 188 | $name |
||
| 189 | )); |
||
| 190 | } |
||
| 191 | |||
| 192 | return $this->variables[$name]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the variable to be used later |
||
| 197 | * @param string $name |
||
| 198 | * @param mixed $value |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setVariable(string $name, mixed $value): self |
||
| 202 | { |
||
| 203 | if ($this->variableValidationHandler !== null) { |
||
| 204 | call_user_func($this->variableValidationHandler, $name, $value); |
||
| 205 | } |
||
| 206 | $this->variables[$name] = $value; |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the variables using array |
||
| 213 | * @param array<string, mixed> $variables |
||
| 214 | * @param bool $clear whether to clear all existing variables |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setVariables(array $variables, bool $clear = true): self |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check whether the given variable exists |
||
| 232 | * @param string $name |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function variableExist(string $name): bool |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Remove the given variable |
||
| 242 | * @param string $name |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function removeVariable(string $name): self |
||
| 246 | { |
||
| 247 | unset($this->variables[$name]); |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Remove the given operator |
||
| 254 | * @param string $name |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function removeOperator(string $name): self |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Clear all variables |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function clearVariables(): self |
||
| 269 | { |
||
| 270 | $this->variables = []; |
||
| 271 | $this->variableNotFoundHandler = null; |
||
| 272 | |||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the callable to be used for variable not found |
||
| 278 | * @param callable $handler |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function setVariableNotFoundHandler(callable $handler): self |
||
| 282 | { |
||
| 283 | $this->variableNotFoundHandler = $handler; |
||
| 284 | return $this; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set the callable to be used for variable validation |
||
| 289 | * @param callable $handler |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function setVariableValidationHandler(callable $handler): self |
||
| 293 | { |
||
| 294 | $this->variableValidationHandler = $handler; |
||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return the variable not found handler |
||
| 300 | * @return callable|null |
||
| 301 | */ |
||
| 302 | public function getVariableNotFoundHandler(): ?callable |
||
| 303 | { |
||
| 304 | return $this->variableNotFoundHandler; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return the variable validation handler |
||
| 309 | * @return callable|null |
||
| 310 | */ |
||
| 311 | public function getVariableValidationHandler(): ?callable |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return the list of caches |
||
| 318 | * @return array<string, Token[]> |
||
| 319 | */ |
||
| 320 | public function getCaches(): array |
||
| 321 | { |
||
| 322 | return $this->caches; |
||
| 323 | } |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Return the list of operators |
||
| 328 | * @return array<string, Operator> |
||
| 329 | */ |
||
| 330 | public function getOperators(): array |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Return the list of functions |
||
| 337 | * @return array<string, CustomFunction> |
||
| 338 | */ |
||
| 339 | public function getFunctions(): array |
||
| 342 | } |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Add the default values like variables, operators, functions |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | protected function addDefaults(): self |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Return the list of default operators |
||
| 367 | * @return array<string, array{callable, int, bool}> |
||
|
|
|||
| 368 | */ |
||
| 369 | protected function defaultOperators(): array |
||
| 370 | { |
||
| 371 | return [ |
||
| 372 | '+' => [static fn($a, $b) => $a + $b, 170, false], |
||
| 373 | '-' => [static fn($a, $b) => $a - $b, 170, false], |
||
| 374 | // unary positive token |
||
| 375 | 'uPos' => [static fn($a) => $a, 200, false], |
||
| 376 | // unary minus token |
||
| 377 | 'uNeg' => [static fn($a) => 0 - $a, 200, false], |
||
| 378 | '*' => [static fn($a, $b) => $a * $b, 180, false], |
||
| 379 | '/' => [ |
||
| 380 | static function ($a, $b) { |
||
| 381 | if ($b == 0) { |
||
| 382 | throw new DivisionByZeroException(); |
||
| 383 | } |
||
| 384 | |||
| 385 | return $a / $b; |
||
| 386 | }, |
||
| 387 | 180, |
||
| 388 | false |
||
| 389 | ], |
||
| 390 | '^' => [static fn($a, $b) => pow($a, $b), 220, true], |
||
| 391 | '%' => [static fn($a, $b) => $a % $b, 180, false], |
||
| 392 | '&&' => [static fn($a, $b) => $a && $b, 100, false], |
||
| 393 | '||' => [static fn($a, $b) => $a || $b, 90, false], |
||
| 394 | '==' => [static fn($a, $b) => is_string($a) || is_string($b) ? strcmp($a, $b) == 0 : $a == $b, 140, false], |
||
| 395 | '!=' => [static fn($a, $b) => is_string($a) || is_string($b) ? strcmp($a, $b) != 0 : $a != $b, 140, false], |
||
| 396 | '>=' => [static fn($a, $b) => $a >= $b, 150, false], |
||
| 397 | '>' => [static fn($a, $b) => $a > $b, 150, false], |
||
| 398 | '<=' => [static fn($a, $b) => $a <= $b, 150, false], |
||
| 399 | '<' => [static fn($a, $b) => $a < $b, 150, false], |
||
| 400 | '!' => [static fn($a) => ! $a, 190, false], |
||
| 401 | ]; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return the list of default functions |
||
| 406 | * @return array<string, callable> |
||
| 407 | */ |
||
| 408 | protected function defaultFunctions(): array |
||
| 474 | ]; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Return the default variables |
||
| 479 | * @return array<string, mixed> |
||
| 480 | */ |
||
| 481 | protected function defaultVariables(): array |
||
| 489 |