|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Foo\Bar; |
| 4 | + |
| 5 | +use ArithmeticError; |
| 6 | +use BadFunctionCallException; |
| 7 | +use Exception; |
| 8 | +use LogicException; |
| 9 | + |
| 10 | +abstract class Calculator |
| 11 | +{ |
| 12 | + abstract public function complexNotRelevantToCheck(array $data); |
| 13 | +} |
| 14 | + |
| 15 | +class CustomFooException extends Exception {} |
| 16 | + |
| 17 | +class Foo extends Calculator |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * @var CallulationService |
| 22 | + */ |
| 23 | + private $calculationService; |
| 24 | + |
| 25 | + public function __construct(CallulationService $calculationService) |
| 26 | + { |
| 27 | + $this->calculationService = $calculationService; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param array $data |
| 32 | + * @return array|mixed |
| 33 | + */ |
| 34 | + private function vaildateFunction(array $data) |
| 35 | + { |
| 36 | + try { |
| 37 | + if (!isset($data['formData'])) { |
| 38 | + throw new CustomFooException('Bar is not set.'); |
| 39 | + } |
| 40 | + return $data['formData']; |
| 41 | + } catch (CustomFooException $exception) { |
| 42 | + // Rule find: Exceptions MUST NOT handled in same function |
| 43 | + // handle $exception code |
| 44 | + } |
| 45 | + |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param array $data |
| 51 | + * @return int |
| 52 | + */ |
| 53 | + public function complexNotRelevantToCheck(array $data) |
| 54 | + { |
| 55 | + $result = $this->vaildateFunction($data); |
| 56 | + $previous = 1; |
| 57 | + |
| 58 | + foreach ($result as $number) { |
| 59 | + if ($number === 0) { |
| 60 | + throw new LogicException('Cant not deviated by null'); |
| 61 | + } |
| 62 | + |
| 63 | + $previous /= $number; |
| 64 | + |
| 65 | + // more complex code |
| 66 | + |
| 67 | + if ($previous === 0) { |
| 68 | + throw new ArithmeticError('Calculation error'); |
| 69 | + } |
| 70 | + |
| 71 | + // more complex code |
| 72 | + |
| 73 | + $result = $this->calculationService->call($previous); |
| 74 | + |
| 75 | + if ($result === false) { |
| 76 | + throw new BadFunctionCallException('Cant not reach calculationService'); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return (int)$previous; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array $data |
| 85 | + * @return int |
| 86 | + * @throws BadFunctionCallException |
| 87 | + */ |
| 88 | + public function complexDivisionFunction(array $data) |
| 89 | + { |
| 90 | + try { |
| 91 | + try { |
| 92 | + |
| 93 | + $result = $this->vaildateFunction($data); |
| 94 | + $previous = 1; |
| 95 | + |
| 96 | + foreach ($result as $number) { |
| 97 | + if ($number === 0) { |
| 98 | + throw new LogicException('Cant not deviated by null'); |
| 99 | + } |
| 100 | + |
| 101 | + $previous /= $number; |
| 102 | + |
| 103 | + // more complex code |
| 104 | + |
| 105 | + if ($previous === 0) { |
| 106 | + throw new ArithmeticError('Calculation error'); |
| 107 | + } |
| 108 | + |
| 109 | + // more complex code |
| 110 | + |
| 111 | + $result = $this->calculationService->call($previous); |
| 112 | + |
| 113 | + if ($result === false) { |
| 114 | + throw new BadFunctionCallException('Cant not reach calculationService'); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return (int)$previous; |
| 119 | + |
| 120 | + } catch (LogicException $logicException) { |
| 121 | + // Rule find: Exceptions MUST NOT handled in same function |
| 122 | + // handle $exception code |
| 123 | + } catch (CustomFooException $exception) { |
| 124 | + // handle $exception code |
| 125 | + } |
| 126 | + } catch (ArithmeticError $arithmeticError) { |
| 127 | + // Rule find: Exceptions MUST NOT handled in same function |
| 128 | + // handle $exception code |
| 129 | + } |
| 130 | + |
| 131 | + return 0; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +// bad logic function in .phtml |
| 137 | + |
| 138 | +function formatFunction(array $data) |
| 139 | +{ |
| 140 | + try { |
| 141 | + if (!isset($data['formData'])) { |
| 142 | + throw new CustomFooException('Bar is not set.'); |
| 143 | + } |
| 144 | + return $data['formData']; |
| 145 | + } catch (CustomFooException $exception) { |
| 146 | + // Rule find: Exceptions MUST NOT handled in same function |
| 147 | + // handle $exception code |
| 148 | + } |
| 149 | + |
| 150 | + return []; |
| 151 | +} |
| 152 | + |
| 153 | +$d = function ($data) { |
| 154 | + try { |
| 155 | + throw new CustomFooException('Bar is not set.'); |
| 156 | + } catch (CustomFooException $exception) { |
| 157 | + // Rule find: Exceptions MUST NOT handled in same function |
| 158 | + // handle $exception code |
| 159 | + } |
| 160 | +}; |
0 commit comments