Conditions | 1 |
Paths | 1 |
Total Lines | 58 |
Code Lines | 27 |
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 |
||
23 | public function testWillDetectChangesInMethods() : void |
||
24 | { |
||
25 | $astLocator = (new BetterReflection())->astLocator(); |
||
26 | |||
27 | $fromLocator = new StringSourceLocator( |
||
28 | <<<'PHP' |
||
29 | <?php |
||
30 | |||
31 | class TheClass { |
||
32 | public function a() {} |
||
33 | protected function b() {} |
||
34 | private function c() {} |
||
35 | private static function d() {} |
||
36 | public function G() {} |
||
37 | } |
||
38 | PHP |
||
39 | , |
||
40 | $astLocator |
||
41 | ); |
||
42 | |||
43 | $toLocator = new StringSourceLocator( |
||
44 | <<<'PHP' |
||
45 | <?php |
||
46 | |||
47 | class TheClass { |
||
48 | protected function b() {} |
||
49 | private static function d() {} |
||
50 | public function e() {} |
||
51 | public function f() {} |
||
52 | public function g() {} |
||
53 | } |
||
54 | PHP |
||
55 | , |
||
56 | $astLocator |
||
57 | ); |
||
58 | |||
59 | $comparator = $this->createMock(MethodBased::class); |
||
60 | |||
61 | $comparator |
||
62 | ->expects(self::exactly(3)) |
||
63 | ->method('compare') |
||
64 | ->willReturnCallback(function (ReflectionMethod $from, ReflectionMethod $to) : Changes { |
||
65 | $methodName = $from->getName(); |
||
66 | |||
67 | self::assertSame(strtolower($methodName), strtolower($to->getName())); |
||
68 | |||
69 | return Changes::fromArray([Change::added($methodName, true)]); |
||
70 | }); |
||
71 | |||
72 | self::assertEquals( |
||
73 | Changes::fromArray([ |
||
74 | Change::added('b', true), |
||
75 | Change::added('d', true), |
||
76 | Change::added('G', true), |
||
77 | ]), |
||
78 | (new MethodChanged($comparator))->compare( |
||
79 | (new ClassReflector($fromLocator))->reflect('TheClass'), |
||
80 | (new ClassReflector($toLocator))->reflect('TheClass') |
||
81 | ) |
||
85 |