| Conditions | 1 |
| Paths | 1 |
| Total Lines | 61 |
| 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 |
||
| 19 | function demonstrate() |
||
|
|
|||
| 20 | { |
||
| 21 | $blockFactory = $this->getBlockGeneratorFactory(); |
||
| 22 | $classFactory = $this->getClassGeneratorFactory(); |
||
| 23 | $constantFactory = $this->getConstantGeneratorFactory(); |
||
| 24 | $documentationFactory = $this->getDocumentationGeneratorFactory(); |
||
| 25 | $methodFactory = $this->getMethodGeneratorFactory(); |
||
| 26 | $propertyFactory = $this->getPropertyGeneratorFactory(); |
||
| 27 | $traitFactory = $this->getTraitGeneratorFactory(); |
||
| 28 | |||
| 29 | $myConstant = $constantFactory->create(); |
||
| 30 | $myConstant->setName('MY_CONSTANT'); |
||
| 31 | $myConstant->setValue('foobar'); |
||
| 32 | |||
| 33 | $myProperty = $propertyFactory->create(); |
||
| 34 | $myProperty->setDocumentation($documentationFactory->create()); |
||
| 35 | $myProperty->markAsProtected(); |
||
| 36 | $myProperty->setName('myProperty'); |
||
| 37 | $myProperty->setValue(12345678.90); |
||
| 38 | $myProperty->addTypeHint('float'); |
||
| 39 | |||
| 40 | $myMethod = $methodFactory->create(); |
||
| 41 | $myMethod->setDocumentation($documentationFactory->create()); |
||
| 42 | $myMethod->markAsPublic(); |
||
| 43 | $myMethod->markAsFinal(); |
||
| 44 | $myMethod->setName('myMethod'); |
||
| 45 | $myMethod->addParameter('foo', null, 'Foo'); |
||
| 46 | $myMethod->addParameter('bar', 'null', 'Bar'); |
||
| 47 | $myMethodBody = $blockFactory->create(); |
||
| 48 | $myMethodBody |
||
| 49 | ->add('$foobar = $foo->toString();') |
||
| 50 | ->add('') |
||
| 51 | ->add('if (!is_null($bar)) {') |
||
| 52 | ->startIndention() |
||
| 53 | ->add('$foobar .= $bar->toString();') |
||
| 54 | ->stopIndention() |
||
| 55 | ->add('}') |
||
| 56 | ->add('') |
||
| 57 | ->add('return $foobar'); |
||
| 58 | $myMethod->setBody($myMethodBody, 'string'); |
||
| 59 | |||
| 60 | $myTrait = $traitFactory->create(); |
||
| 61 | $myTrait->setDocumentation($documentationFactory->create()); |
||
| 62 | $myTrait->setName('myTrait'); |
||
| 63 | |||
| 64 | $myClass = $classFactory->create(); |
||
| 65 | $myClass->setDocumentation($documentationFactory->create()); |
||
| 66 | $myClass->setNamespace('My\Namespace'); |
||
| 67 | $myClass->setName('MyClass'); |
||
| 68 | $myClass->markAsFinal(); |
||
| 69 | $myClass->setExtends('Foo\Bar', true); |
||
| 70 | $myClass->addImplements('Bar\FooInterface', true); |
||
| 71 | $myClass->addConstant($myConstant); |
||
| 72 | $myClass->addMethod($myMethod); |
||
| 73 | $myClass->addProperty($myProperty); |
||
| 74 | $myClass->addTrait($myTrait); |
||
| 75 | $myClass->getDocumentation()->setAuthor('stev leibelt', '[email protected]'); |
||
| 76 | $myClass->getDocumentation()->setVersion('0.8.15', 'available since 2014-05-24'); |
||
| 77 | |||
| 78 | echo $myClass->generate() . PHP_EOL; |
||
| 79 | } |
||
| 80 | } |
||
| 83 | $example->demonstrate(); |
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.