| Conditions | 2 |
| Total Lines | 56 |
| 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 |
||
| 101 | public function testCache(): void |
||
| 102 | { |
||
| 103 | $parser1 = new ObjectParser(new ObjectForTestingCache1()); |
||
| 104 | $reflectionParser = new ReflectionObject($parser1); |
||
| 105 | |||
| 106 | $cacheProperty = $reflectionParser->getProperty('cache'); |
||
| 107 | if (PHP_VERSION_ID < 80100) { |
||
| 108 | $cacheProperty->setAccessible(true); |
||
| 109 | } |
||
| 110 | |||
| 111 | $cacheKey1 = 'Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache1_7_0'; |
||
| 112 | $this->assertArrayNotHasKey($cacheKey1, $cacheProperty->getValue()); |
||
| 113 | |||
| 114 | $expectedRules1 = [ |
||
| 115 | 'a' => [new Required()], |
||
| 116 | 'b' => [new Number(min: 1)], |
||
| 117 | 'c' => [new Number(max: 2)], |
||
| 118 | ]; |
||
| 119 | $expectedLabels1 = ['c' => 'd']; |
||
| 120 | $rules1 = $parser1->getRules(); |
||
| 121 | $labels1 = $parser1->getLabels(); |
||
| 122 | $this->assertEquals($expectedRules1, $rules1); |
||
| 123 | $cache = $cacheProperty->getValue(); |
||
| 124 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
| 125 | $this->assertArrayHasKey('rules', $cache[$cacheKey1]); |
||
| 126 | $this->assertArrayHasKey('reflectionProperties', $cache[$cacheKey1]); |
||
| 127 | $this->assertArrayHasKey('reflectionSource', $cache[$cacheKey1]); |
||
| 128 | $this->assertArrayHasKey('labels', $cache[$cacheKey1]); |
||
| 129 | $this->assertSame($rules1, $parser1->getRules()); |
||
| 130 | $this->assertSame($labels1, $expectedLabels1); |
||
|
|
|||
| 131 | |||
| 132 | $parser2 = new ObjectParser(new ObjectForTestingCache2()); |
||
| 133 | $cacheKey2 = 'Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache2_7_0'; |
||
| 134 | $cache = $cacheProperty->getValue(); |
||
| 135 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
| 136 | $this->assertArrayNotHasKey($cacheKey2, $cache); |
||
| 137 | |||
| 138 | $reflectionProperties2 = $parser2->getReflectionProperties(); |
||
| 139 | $cache = $cacheProperty->getValue(); |
||
| 140 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
| 141 | $this->assertArrayHasKey($cacheKey2, $cache); |
||
| 142 | $this->assertArrayNotHasKey('rules', $cache[$cacheKey2]); |
||
| 143 | $this->assertArrayHasKey('reflectionProperties', $cache[$cacheKey2]); |
||
| 144 | $this->assertArrayHasKey('reflectionSource', $cache[$cacheKey2]); |
||
| 145 | $this->assertSame($reflectionProperties2, $parser2->getReflectionProperties()); |
||
| 146 | |||
| 147 | $expectedRules2 = [ |
||
| 148 | 'd' => [new Required()], |
||
| 149 | 'e' => [new Number(min: 5)], |
||
| 150 | 'f' => [new Number(max: 6)], |
||
| 151 | ]; |
||
| 152 | $rules2 = $parser2->getRules(); |
||
| 153 | $this->assertEquals($expectedRules2, $parser2->getRules()); |
||
| 154 | $this->assertArrayHasKey('rules', $cacheProperty->getValue()[$cacheKey2]); |
||
| 155 | $this->assertSame($rules2, $parser2->getRules()); |
||
| 156 | $this->assertSame($rules1, $parser1->getRules()); |
||
| 157 | } |
||
| 189 |