Complex classes like StrictIterableTrait 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 StrictIterableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait StrictIterableTrait |
||
| 8 | { |
||
| 9 | use CommonMutableContainerTrait; |
||
| 10 | |||
| 11 | public function concatAll() |
||
| 12 | { |
||
| 13 | $results = new static(); |
||
| 14 | $this->each(function (Iterable $subArray) use ($results) { |
||
| 15 | $subArray->each(function ($item) use ($results) { |
||
| 16 | $results->add($item); |
||
|
|
|||
| 17 | }); |
||
| 18 | }); |
||
| 19 | |||
| 20 | return $results; |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritDoc} |
||
| 25 | * @return $this |
||
| 26 | */ |
||
| 27 | 1 | public function map(callable $callable) |
|
| 28 | { |
||
| 29 | 1 | $res = new static(); |
|
| 30 | 1 | foreach ($this as $v) { |
|
| 31 | 1 | $res[] = $callable($v); |
|
| 32 | } |
||
| 33 | |||
| 34 | 1 | return $res; |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | 2 | public function filter(callable $callable) |
|
| 42 | { |
||
| 43 | 2 | $res = new static(); |
|
| 44 | 2 | foreach ($this as $v) { |
|
| 45 | 2 | if ($callable($v)) { |
|
| 46 | 2 | $res[] = $v; |
|
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | 2 | return $res; |
|
| 51 | } |
||
| 52 | |||
| 53 | public function zip($iterable) |
||
| 54 | { |
||
| 55 | $res = new static(); |
||
| 56 | $it = $iterable->getIterator(); |
||
| 57 | foreach ($this as $v) { |
||
| 58 | if (!$it->valid()) { |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | $res[] = new Pair($v, $it->current()); |
||
| 62 | $it->next(); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $res; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 1 | public function take($size = 1) |
|
| 73 | { |
||
| 74 | 1 | $res = new static(); |
|
| 75 | |||
| 76 | 1 | if ($size <= 0) { |
|
| 77 | return $res; |
||
| 78 | } |
||
| 79 | |||
| 80 | 1 | foreach ($this as $v) { |
|
| 81 | 1 | $res[] = $v; |
|
| 82 | 1 | if (--$size === 0) { |
|
| 83 | 1 | break; |
|
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | 1 | return $res; |
|
| 88 | } |
||
| 89 | |||
| 90 | public function takeWhile(callable $callable) |
||
| 91 | { |
||
| 92 | $res = new static(); |
||
| 93 | foreach ($this as $v) { |
||
| 94 | if (!$callable($v)) { |
||
| 95 | break; |
||
| 96 | } |
||
| 97 | $res[] = $v; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $res; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function skip($n) |
||
| 104 | { |
||
| 105 | $res = new static(); |
||
| 106 | foreach ($this as $v) { |
||
| 107 | if ($n <= 0) { |
||
| 108 | $res[] = $v; |
||
| 109 | } else { |
||
| 110 | --$n; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return $res; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function skipWhile(callable $callable) |
||
| 118 | { |
||
| 119 | $res = new static(); |
||
| 120 | $skip = true; |
||
| 121 | foreach ($this as $v) { |
||
| 122 | if ($skip) { |
||
| 123 | if ($callable($v)) { |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | $skip = false; |
||
| 127 | } |
||
| 128 | $res[] = $v; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $res; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function slice($start, $length) |
||
| 135 | { |
||
| 136 | $res = new static(); |
||
| 137 | if ($length <= 0) { |
||
| 138 | return $res; |
||
| 139 | } |
||
| 140 | foreach ($this as $v) { |
||
| 141 | if ($start !== 0) { |
||
| 142 | --$start; |
||
| 143 | continue; |
||
| 144 | } |
||
| 145 | $res[] = $v; |
||
| 146 | if (--$length === 0) { |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return $res; |
||
| 152 | } |
||
| 153 | |||
| 154 | 1 | public function concat(\Traversable $iterable) |
|
| 155 | { |
||
| 156 | 1 | $res = []; |
|
| 157 | |||
| 158 | 1 | foreach ($this as $v) { |
|
| 159 | 1 | $res[] = $v; |
|
| 160 | } |
||
| 161 | |||
| 162 | 1 | foreach ($iterable as $v) { |
|
| 163 | 1 | $res[] = $v; |
|
| 164 | } |
||
| 165 | 1 | $this->container = $res; |
|
| 166 | |||
| 167 | 1 | return $this; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritDoc} |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | 2 | public function first() |
|
| 175 | { |
||
| 176 | 2 | foreach ($this as $v) { |
|
| 177 | 1 | return $v; |
|
| 178 | } |
||
| 179 | |||
| 180 | 1 | return null; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritDoc} |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | 2 | public function last() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritDoc} |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function each(callable $callable) |
||
| 199 | { |
||
| 200 | foreach ($this as $v) { |
||
| 201 | $callable($v); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | 1 | public function exists(callable $fn) |
|
| 220 | } |
||
| 221 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.