Conditions | 2 |
Total Lines | 52 |
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 | $rules1 = $parser1->getRules(); |
||
120 | $this->assertEquals($expectedRules1, $rules1); |
||
121 | $cache = $cacheProperty->getValue(); |
||
122 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
123 | $this->assertArrayHasKey('rules', $cache[$cacheKey1]); |
||
124 | $this->assertArrayHasKey('reflectionProperties', $cache[$cacheKey1]); |
||
125 | $this->assertArrayHasKey('reflectionSource', $cache[$cacheKey1]); |
||
126 | $this->assertSame($rules1, $parser1->getRules()); |
||
127 | |||
128 | $parser2 = new ObjectParser(new ObjectForTestingCache2()); |
||
129 | $cacheKey2 = 'Yiisoft\Validator\Tests\Support\Data\ObjectForTestingCache2_7_0'; |
||
130 | $cache = $cacheProperty->getValue(); |
||
131 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
132 | $this->assertArrayNotHasKey($cacheKey2, $cache); |
||
133 | |||
134 | $reflectionProperties2 = $parser2->getReflectionProperties(); |
||
135 | $cache = $cacheProperty->getValue(); |
||
136 | $this->assertArrayHasKey($cacheKey1, $cache); |
||
137 | $this->assertArrayHasKey($cacheKey2, $cache); |
||
138 | $this->assertArrayNotHasKey('rules', $cache[$cacheKey2]); |
||
139 | $this->assertArrayHasKey('reflectionProperties', $cache[$cacheKey2]); |
||
140 | $this->assertArrayHasKey('reflectionSource', $cache[$cacheKey2]); |
||
141 | $this->assertSame($reflectionProperties2, $parser2->getReflectionProperties()); |
||
142 | |||
143 | $expectedRules2 = [ |
||
144 | 'd' => [new Required()], |
||
145 | 'e' => [new Number(min: 5)], |
||
146 | 'f' => [new Number(max: 6)], |
||
147 | ]; |
||
148 | $rules2 = $parser2->getRules(); |
||
149 | $this->assertEquals($expectedRules2, $parser2->getRules()); |
||
150 | $this->assertArrayHasKey('rules', $cacheProperty->getValue()[$cacheKey2]); |
||
151 | $this->assertSame($rules2, $parser2->getRules()); |
||
152 | $this->assertSame($rules1, $parser1->getRules()); |
||
153 | } |
||
181 |