HelpTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 37
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHelp() 0 26 3
A help() 0 21 3
A _purifyCommentForHelp() 0 9 1
A _extractCommentForHelp() 0 5 1
A __construct() 0 9 1
1
<?php
2
3
namespace controllers;
4
5
use terminal\Terminal;
6
use filesystem\Filesystem;
7
use \Config;
8
9
trait HelpTrait
10
{
11
    function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
        Config              $config,
13
        Terminal            $terminal,
14
        Filesystem          $filesystem
15
    )
16
    {
17
        $this->config               = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->terminal             = $terminal;
0 ignored issues
show
Bug Best Practice introduced by
The property terminal does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->filesystem           = $filesystem;
0 ignored issues
show
Bug Best Practice introduced by
The property filesystem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    public function help($search = '')
23
    {
24
        $this->terminal->echo('Usage: php ccg.php [generator] [command] [options]');
25
26
        $docs = $this->getHelp();
27
28
        array_walk($docs, function(array $doc) use ($search) {
29
30
            if ($search && strpos($doc['name'], $search) === false) {
31
                return;
32
            }
33
34
            $this->terminal->echo(str_repeat(PHP_EOL, 2));
35
36
            $docLines           = preg_split('/\r\n|\r|\n/', $doc['help']);
37
            $descriptionLines   = $docLines;
38
39
            unset($descriptionLines[0]);
40
41
            $this->terminal->success($docLines[0]);
42
            $this->terminal->echo(implode(PHP_EOL, $descriptionLines));
0 ignored issues
show
Bug introduced by
It seems like $descriptionLines can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $this->terminal->echo(implode(PHP_EOL, /** @scrutinizer ignore-type */ $descriptionLines));
Loading history...
43
        });
44
    }
45
46
    public function getHelp()
47
    {
48
        $ref = new \ReflectionClass($this);
49
        $methods = $ref->getMethods();
50
51
        $docs = array_map(function(\ReflectionMethod $method) {
52
            $helpCommentPurified = '';
53
            $helpComment = self::_extractCommentForHelp($method->getDocComment() ?: '');
0 ignored issues
show
Bug introduced by
It seems like $method->getDocComment() ?: '' can also be of type true; however, parameter $comment of controllers\HelpTrait::_extractCommentForHelp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            $helpComment = self::_extractCommentForHelp(/** @scrutinizer ignore-type */ $method->getDocComment() ?: '');
Loading history...
54
55
            if (!empty($helpComment)) {
56
                $helpCommentPurified = self::_purifyCommentForHelp($helpComment[1]);
57
            }
58
59
            return [
60
                'name' => $method->getName(),
61
                'help' => $helpCommentPurified,
62
            ];
63
        }, $methods);
64
65
        $docsFiltered = array_values(
66
            array_filter($docs, function($doc) {
67
                return (bool) $doc['help'];
68
            })
69
        );
70
71
        return $docsFiltered;
72
    }
73
74
    /**
75
     * @todo test
76
     */
77
    private static function _extractCommentForHelp(string $comment): array
78
    {
79
        preg_match('/\/\*{2}\s+\*\shelp:\s+(.*)/usmi', $comment, $matches);
80
81
        return $matches;
82
    }
83
84
    /**
85
     * @todo test
86
     */
87
    private static function _purifyCommentForHelp(string $comment): string
88
    {
89
        $clearedComment = trim(preg_replace('/(\*\s)|(\*\/)/sm', '', $comment));
90
        $indentedComment = implode(
91
            PHP_EOL . str_repeat(' ', 8),
92
            preg_split('/\r\n|\r|\n/', $clearedComment)
0 ignored issues
show
Bug introduced by
It seems like preg_split('/\r\n|\r|\n/', $clearedComment) can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            /** @scrutinizer ignore-type */ preg_split('/\r\n|\r|\n/', $clearedComment)
Loading history...
93
        );
94
95
        return $indentedComment;
96
    }
97
}
98