| Conditions | 20 |
| Paths | 227 |
| Total Lines | 149 |
| Code Lines | 79 |
| 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 |
||
| 77 | public function testRepositoryForImplementaion( |
||
| 78 | string $objImplementation, |
||
| 79 | bool $readable, |
||
|
|
|||
| 80 | bool $writeable, |
||
| 81 | array ...$paramsArray |
||
| 82 | ) : void { |
||
| 83 | $interfaceCheck = $objImplementation; |
||
| 84 | |||
| 85 | if ( |
||
| 86 | false === is_a( |
||
| 87 | $interfaceCheck, |
||
| 88 | DefinesOwnIdPropertiesInterface::class, |
||
| 89 | true |
||
| 90 | ) |
||
| 91 | ) { |
||
| 92 | throw new InvalidArgumentException( |
||
| 93 | sprintf( |
||
| 94 | 'Argument 1 passed to %s must be an implementation of %s', |
||
| 95 | __METHOD__, |
||
| 96 | DefinesOwnIdPropertiesInterface::class |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $repo = static::DaftObjectRepositoryByType( |
||
| 102 | $objImplementation |
||
| 103 | ); |
||
| 104 | |||
| 105 | $idProps = []; |
||
| 106 | |||
| 107 | foreach ($objImplementation::DaftObjectIdProperties() as $idProp) { |
||
| 108 | $idProps[] = $idProp; |
||
| 109 | } |
||
| 110 | |||
| 111 | foreach ($paramsArray as $params) { |
||
| 112 | /** |
||
| 113 | * @var DefinesOwnIdPropertiesInterface $obj |
||
| 114 | */ |
||
| 115 | $obj = new $objImplementation($params, $writeable); |
||
| 116 | |||
| 117 | $repoByObject = static::DaftObjectRepositoryByDaftObject( |
||
| 118 | $obj |
||
| 119 | ); |
||
| 120 | |||
| 121 | $this->assertSame(get_class($repo), get_class($repoByObject)); |
||
| 122 | |||
| 123 | $ids = []; |
||
| 124 | |||
| 125 | $repo->RememberDaftObject($obj); |
||
| 126 | |||
| 127 | $props = array_values($idProps); |
||
| 128 | |||
| 129 | foreach ($props as $prop) { |
||
| 130 | $ids[] = $obj->$prop; |
||
| 131 | } |
||
| 132 | |||
| 133 | if (1 === count($ids)) { |
||
| 134 | $this->assertSame($obj, $repo->RecallDaftObject($ids[0])); |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->assertSame($obj, $repo->RecallDaftObject($ids)); |
||
| 138 | |||
| 139 | if (count($ids) < 1) { |
||
| 140 | throw new RuntimeException( |
||
| 141 | 'Insufficient Id properties found!' |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $repo->ForgetDaftObject($obj); |
||
| 146 | |||
| 147 | $retrieved = $repo->RecallDaftObject($ids); |
||
| 148 | |||
| 149 | $this->assertNotNull($retrieved); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var DefinesOwnIdPropertiesInterface $retrieved |
||
| 153 | */ |
||
| 154 | $retrieved = $retrieved; |
||
| 155 | |||
| 156 | $this->assertSame( |
||
| 157 | $objImplementation::DaftObjectIdHash($obj), |
||
| 158 | $objImplementation::DaftObjectIdHash($retrieved) |
||
| 159 | ); |
||
| 160 | $this->assertSame(get_class($obj), get_class($retrieved)); |
||
| 161 | $this->assertNotSame($obj, $retrieved); |
||
| 162 | |||
| 163 | foreach ($objImplementation::DaftObjectProperties() as $prop) { |
||
| 164 | if ( |
||
| 165 | true === method_exists($obj, 'Get' . ucfirst($prop)) && |
||
| 166 | true === method_exists($retrieved, 'Get' . ucfirst($prop)) |
||
| 167 | ) { |
||
| 168 | $this->assertSame($obj->$prop, $retrieved->$prop); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $repo->RemoveDaftObject($obj); |
||
| 173 | |||
| 174 | $this->assertNull($repo->RecallDaftObject($ids)); |
||
| 175 | |||
| 176 | foreach ($objImplementation::DaftObjectProperties() as $prop) { |
||
| 177 | if ( |
||
| 178 | false === in_array($prop, $idProps, true) && |
||
| 179 | true === method_exists($obj, 'Set' . ucfirst($prop)) && |
||
| 180 | true === method_exists( |
||
| 181 | $retrieved, 'Get' . ucfirst($prop) |
||
| 182 | ) && |
||
| 183 | true === is_numeric($obj->$prop) |
||
| 184 | ) { |
||
| 185 | $retrieved->$prop *= 2; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $repo->RememberDaftObject($retrieved); |
||
| 190 | $repo->ForgetDaftObject($obj); |
||
| 191 | $repo->ForgetDaftObject($retrieved); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var DefinesOwnIdPropertiesInterface $retrieved |
||
| 195 | */ |
||
| 196 | $retrieved = $repo->RecallDaftObject($ids); |
||
| 197 | |||
| 198 | $this->assertTrue( |
||
| 199 | is_a($retrieved, $objImplementation, true), |
||
| 200 | ( |
||
| 201 | get_class($repo) . |
||
| 202 | '::RecallDaftObject() must return an implementation of ' . |
||
| 203 | $objImplementation |
||
| 204 | ) |
||
| 205 | ); |
||
| 206 | |||
| 207 | foreach ($objImplementation::DaftObjectProperties() as $prop) { |
||
| 208 | if ( |
||
| 209 | false === in_array($prop, $idProps, true) && |
||
| 210 | true === method_exists($obj, 'Set' . ucfirst($prop)) && |
||
| 211 | true === method_exists( |
||
| 212 | $retrieved, 'Get' . ucfirst($prop) |
||
| 213 | ) && |
||
| 214 | true === is_numeric($obj->$prop) |
||
| 215 | ) { |
||
| 216 | $this->assertSame($obj->$prop * 2, $retrieved->$prop); |
||
| 217 | $retrieved->$prop /= 2; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $repo->RememberDaftObject($retrieved); |
||
| 222 | |||
| 223 | $repo->RemoveDaftObject($retrieved); |
||
| 224 | |||
| 225 | $this->assertNull($repo->RecallDaftObject($ids)); |
||
| 226 | } |
||
| 390 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.