| Conditions | 14 |
| Paths | 181 |
| Total Lines | 92 |
| Code Lines | 57 |
| 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 |
||
| 29 | public function isValid($middleware) |
||
| 30 | { |
||
| 31 | if (false === class_exists($middleware)) { |
||
| 32 | $this->addError( |
||
| 33 | 'Class name given was not found: "%s".', |
||
| 34 | 1489070202, |
||
| 35 | [$middleware] |
||
| 36 | ); |
||
| 37 | } else { |
||
| 38 | $interfaces = class_implements($middleware); |
||
| 39 | |||
| 40 | if (false === in_array(MiddlewareInterface::class, $interfaces)) { |
||
| 41 | $this->addError( |
||
| 42 | 'Class "%s" must implement "%s".', |
||
| 43 | 1489070282, |
||
| 44 | [$middleware, MiddlewareInterface::class] |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | $signalsFound = []; |
||
| 49 | foreach ($interfaces as $interface) { |
||
| 50 | if (in_array(MiddlewareSignal::class, class_implements($interface))) { |
||
| 51 | $signalsFound[] = $interface; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | if (empty($signalsFound)) { |
||
| 56 | $this->addError( |
||
| 57 | 'Class "%s" must implement one interface that extends "%s".', |
||
| 58 | 1489074248, |
||
| 59 | [$middleware, MiddlewareSignal::class] |
||
| 60 | ); |
||
| 61 | } elseif (count($signalsFound) > 1) { |
||
| 62 | $this->addError( |
||
| 63 | 'Class "%s" must implement only one interface that extends "%s"; %s were found: "%s"', |
||
| 64 | 1489074852, |
||
| 65 | [ |
||
| 66 | $middleware, |
||
| 67 | MiddlewareSignal::class, |
||
| 68 | count($signalsFound), |
||
| 69 | implode('", "', $signalsFound) |
||
| 70 | ] |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (false === in_array(Before::class, $interfaces) |
||
| 75 | && false === in_array(After::class, $interfaces) |
||
| 76 | ) { |
||
| 77 | $this->addError( |
||
| 78 | 'Class "%s" must implement at least one of these interfaces: "%s", "%s".', |
||
| 79 | 1489074986, |
||
| 80 | [ |
||
| 81 | $middleware, |
||
| 82 | Before::class, |
||
| 83 | After::class |
||
| 84 | ] |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (false === $this->result->hasErrors()) { |
||
| 89 | if (in_array(BeginSignal::class, $interfaces) |
||
| 90 | && in_array(Before::class, $interfaces) |
||
| 91 | ) { |
||
| 92 | $this->addError( |
||
| 93 | 'Class "%s" implements interfaces "%s" and "%s", but the signal "before beginning" is (obviously) invalid. Please remove "%s" dependency.', |
||
| 94 | 1489075185, |
||
| 95 | [ |
||
| 96 | $middleware, |
||
| 97 | Before::class, |
||
| 98 | BeginSignal::class, |
||
| 99 | Before::class, |
||
| 100 | ] |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (in_array(EndSignal::class, $interfaces) |
||
| 105 | && in_array(After::class, $interfaces) |
||
| 106 | ) { |
||
| 107 | $this->addError( |
||
| 108 | 'Class "%s" implements interfaces "%s" and "%s", but the signal "after the end" is (obviously) invalid. Please remove "%s" dependency.', |
||
| 109 | 1489075242, |
||
| 110 | [ |
||
| 111 | $middleware, |
||
| 112 | After::class, |
||
| 113 | EndSignal::class, |
||
| 114 | After::class, |
||
| 115 | ] |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 |