Conditions | 3 |
Paths | 3 |
Total Lines | 78 |
Code Lines | 44 |
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 | #!/bin/php |
||
20 | function demonstrate() |
||
|
|||
21 | { |
||
22 | //---- begin of factories |
||
23 | $classFactory = $this->getClassGeneratorFactory(); |
||
24 | $documentationFactory = $this->getDocumentationGeneratorFactory(); |
||
25 | $methodFactory = $this->getMethodGeneratorFactory(); |
||
26 | $propertyFactory = $this->getPropertyGeneratorFactory(); |
||
27 | //---- end of factories |
||
28 | |||
29 | $class = $classFactory->create(); |
||
30 | $class->setDocumentation($documentationFactory->create()); |
||
31 | $class->setName('ExampleDTO'); |
||
32 | |||
33 | $properties = [ |
||
34 | [ |
||
35 | 'name' => 'foo', |
||
36 | 'typeHint' => null, |
||
37 | 'value' => 42 |
||
38 | ], |
||
39 | [ |
||
40 | 'name' => 'foobar', |
||
41 | 'typeHint' => 'array', |
||
42 | 'value' => null |
||
43 | ], |
||
44 | [ |
||
45 | 'name' => 'bar', |
||
46 | 'typeHint' => 'Bar', |
||
47 | 'value' => null |
||
48 | ] |
||
49 | ]; |
||
50 | |||
51 | foreach ($properties as $value) { |
||
52 | //---- begin of properties |
||
53 | $property = $propertyFactory->create(); |
||
54 | $property->setDocumentation($documentationFactory->create()); |
||
55 | $property->setName($value['name']); |
||
56 | if (!is_null($value['value'])) { |
||
57 | $property->setValue('value'); |
||
58 | } |
||
59 | $property->markAsPrivate(); |
||
60 | $property->setDocumentation($documentationFactory->create()); |
||
61 | //---- end of properties |
||
62 | |||
63 | //---- begin of getter method |
||
64 | $getterMethod = $methodFactory->create(); |
||
65 | $getterMethod->setDocumentation($documentationFactory->create()); |
||
66 | $getterMethod->setName('get' . ucfirst($value['name'])); |
||
67 | $getterMethod->setBody( |
||
68 | [ |
||
69 | '$this->' . $value['name'] . ' = $' . $value['name'] . ';' |
||
70 | ], |
||
71 | [ |
||
72 | $value['typeHint'] |
||
73 | ] |
||
74 | ); |
||
75 | //---- end of getter method |
||
76 | |||
77 | //---- begin of setter method |
||
78 | $setterMethod = $methodFactory->create(); |
||
79 | $setterMethod->setDocumentation($documentationFactory->create()); |
||
80 | $setterMethod->addParameter($value['name'], null, $value['typeHint']); |
||
81 | $setterMethod->setName('set' . ucfirst($value['name'])); |
||
82 | $setterMethod->setBody( |
||
83 | [ |
||
84 | 'return $this->' . $value['name'] . ';' |
||
85 | ] |
||
86 | ); |
||
87 | //---- end of setter method |
||
88 | |||
89 | $class->addProperty($property); |
||
90 | $class->addMethod($getterMethod); |
||
91 | $class->addMethod($setterMethod); |
||
92 | } |
||
93 | |||
94 | echo 'generated content' . PHP_EOL; |
||
95 | echo '----' . PHP_EOL; |
||
96 | echo $class->generate() . PHP_EOL; |
||
97 | } |
||
98 | } |
||
102 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.