LFFATE /
ccg
| 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
|
|||||
| 12 | Config $config, |
||||
| 13 | Terminal $terminal, |
||||
| 14 | Filesystem $filesystem |
||||
| 15 | ) |
||||
| 16 | { |
||||
| 17 | $this->config = $config; |
||||
|
0 ignored issues
–
show
|
|||||
| 18 | $this->terminal = $terminal; |
||||
|
0 ignored issues
–
show
|
|||||
| 19 | $this->filesystem = $filesystem; |
||||
|
0 ignored issues
–
show
|
|||||
| 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
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
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
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
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
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
Loading history...
|
|||||
| 93 | ); |
||||
| 94 | |||||
| 95 | return $indentedComment; |
||||
| 96 | } |
||||
| 97 | } |
||||
| 98 |
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.