Completed
Pull Request — master (#924)
by Greg
03:26
created

testGenerateDocumentation()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 7.3882
c 0
b 0
f 0
cc 8
nc 1
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class GenerateMarkdownDocTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Collection\loadTasks;
11
    use Task\Development\loadTasks;
12
13
    protected $fixtures;
14
15
    public function setUp()
16
    {
17
        $this->fixtures = new Fixtures();
18
        $this->initTestTasksTrait();
19
        $this->fixtures->createAndCdToSandbox();
20
    }
21
22
    public function tearDown()
23
    {
24
        $this->fixtures->cleanup();
25
    }
26
27
    public function testGenerateDocumentation()
28
    {
29
        $sourceFile = $this->fixtures->dataFile('TestedRoboTask.php');
30
        $this->assertFileExists($sourceFile);
31
        include $sourceFile;
32
        $this->assertTrue(class_exists('TestedRoboTask'));
33
34
        $collection = $this->collectionBuilder();
35
        $taskGenerator = $collection->taskGenDoc("TestedRoboTask.md");
0 ignored issues
show
Documentation Bug introduced by
The method taskGenDoc does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
36
        $taskGenerator->filterClasses(function (\ReflectionClass $r) {
37
            return !($r->isAbstract() || $r->isTrait()) && $r->implementsInterface('Robo\Contract\TaskInterface');
38
        })->prepend("# TestedRoboTask Tasks");
39
        $taskGenerator->docClass('TestedRoboTask');
40
41
        $taskGenerator->filterMethods(
42
            function (\ReflectionMethod $m) {
43
                if ($m->isConstructor() || $m->isDestructor() || $m->isStatic()) {
44
                    return false;
45
                }
46
                $undocumentedMethods =
47
                [
48
                    '',
49
                    'run',
50
                    '__call',
51
                    'inflect',
52
                    'injectDependencies',
53
                    'getCommand',
54
                    'getPrinted',
55
                    'getConfig',
56
                    'setConfig',
57
                    'logger',
58
                    'setLogger',
59
                    'setProgressIndicator',
60
                    'progressIndicatorSteps',
61
                    'setBuilder',
62
                    'getBuilder',
63
                    'collectionBuilder',
64
                ];
65
                return !in_array($m->name, $undocumentedMethods) && $m->isPublic(); // methods are not documented
66
            }
67
        )->processClassSignature(
68
            function ($c) {
69
                return "## " . preg_replace('~Task$~', '', $c->getShortName()) . "\n";
70
            }
71
        )->processClassDocBlock(
72
            function (\ReflectionClass $c, $doc) {
73
                $doc = preg_replace('~@method .*?(.*?)\)~', '* `$1)` ', $doc);
74
                $doc = str_replace('\\'.$c->name, '', $doc);
75
                return $doc;
76
            }
77
        )->processMethodSignature(
78
            function (\ReflectionMethod $m, $text) {
79
                return str_replace('#### *public* ', '* `', $text) . '`';
80
            }
81
        )->processMethodDocBlock(
82
            function (\ReflectionMethod $m, $text) {
83
84
                return $text ? ' ' . trim(strtok($text, "\n"), "\n") : '';
85
            }
86
        );
87
88
        $result = $collection->run();
89
        $this->assertTrue($result->wasSuccessful());
90
91
        $this->assertFileExists('TestedRoboTask.md');
92
93
        $contents = file_get_contents('TestedRoboTask.md');
94
        $this->assertContains('A test task file. Used for testig documentation generation.', $contents);
95
        $this->assertContains('taskTestedRoboTask', $contents);
96
        $this->assertContains('Set the destination file', $contents);
97
    }
98
99
}
100