| Conditions | 8 |
| Paths | 1 |
| Total Lines | 71 |
| 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 |
||
| 28 | public function testGenerateDocumentation() |
||
| 29 | { |
||
| 30 | $sourceFile = $this->fixtures->dataFile('TestedRoboTask.php'); |
||
| 31 | $this->assertFileExists($sourceFile); |
||
| 32 | include $sourceFile; |
||
| 33 | $this->assertTrue(class_exists('TestedRoboTask')); |
||
| 34 | |||
| 35 | $collection = $this->collectionBuilder(); |
||
| 36 | $taskGenerator = $collection->taskGenDoc("TestedRoboTask.md"); |
||
|
|
|||
| 37 | $taskGenerator->filterClasses(function (\ReflectionClass $r) { |
||
| 38 | return !($r->isAbstract() || $r->isTrait()) && $r->implementsInterface('Robo\Contract\TaskInterface'); |
||
| 39 | })->prepend("# TestedRoboTask Tasks"); |
||
| 40 | $taskGenerator->docClass('TestedRoboTask'); |
||
| 41 | |||
| 42 | $taskGenerator->filterMethods( |
||
| 43 | function (\ReflectionMethod $m) { |
||
| 44 | if ($m->isConstructor() || $m->isDestructor() || $m->isStatic()) { |
||
| 45 | return false; |
||
| 46 | } |
||
| 47 | $undocumentedMethods = |
||
| 48 | [ |
||
| 49 | '', |
||
| 50 | 'run', |
||
| 51 | '__call', |
||
| 52 | 'inflect', |
||
| 53 | 'injectDependencies', |
||
| 54 | 'getCommand', |
||
| 55 | 'getPrinted', |
||
| 56 | 'getConfig', |
||
| 57 | 'setConfig', |
||
| 58 | 'logger', |
||
| 59 | 'setLogger', |
||
| 60 | 'setProgressIndicator', |
||
| 61 | 'progressIndicatorSteps', |
||
| 62 | 'setBuilder', |
||
| 63 | 'getBuilder', |
||
| 64 | 'collectionBuilder', |
||
| 65 | ]; |
||
| 66 | return !in_array($m->name, $undocumentedMethods) && $m->isPublic(); // methods are not documented |
||
| 67 | } |
||
| 68 | )->processClassSignature( |
||
| 69 | function ($c) { |
||
| 70 | return "## " . preg_replace('~Task$~', '', $c->getShortName()) . "\n"; |
||
| 71 | } |
||
| 72 | )->processClassDocBlock( |
||
| 73 | function (\ReflectionClass $c, $doc) { |
||
| 74 | $doc = preg_replace('~@method .*?(.*?)\)~', '* `$1)` ', $doc); |
||
| 75 | $doc = str_replace('\\'.$c->name, '', $doc); |
||
| 76 | return $doc; |
||
| 77 | } |
||
| 78 | )->processMethodSignature( |
||
| 79 | function (\ReflectionMethod $m, $text) { |
||
| 80 | return str_replace('#### *public* ', '* `', $text) . '`'; |
||
| 81 | } |
||
| 82 | )->processMethodDocBlock( |
||
| 83 | function (\ReflectionMethod $m, $text) { |
||
| 84 | |||
| 85 | return $text ? ' ' . trim(strtok($text, "\n"), "\n") : ''; |
||
| 86 | } |
||
| 87 | ); |
||
| 88 | |||
| 89 | $result = $collection->run(); |
||
| 90 | $this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
||
| 91 | |||
| 92 | $this->assertFileExists('TestedRoboTask.md'); |
||
| 93 | |||
| 94 | $contents = file_get_contents('TestedRoboTask.md'); |
||
| 95 | $this->assertContains('A test task file. Used for testig documentation generation.', $contents); |
||
| 96 | $this->assertContains('taskTestedRoboTask', $contents); |
||
| 97 | $this->assertContains('Set the destination file', $contents); |
||
| 98 | } |
||
| 99 | |||
| 101 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: