Passed
Pull Request — master (#1)
by Jitendra
01:39
created

ClishCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 39
c 1
b 0
f 1
dl 0
loc 76
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A doExport() 0 11 3
A execute() 0 11 3
A doHighlight() 0 4 3
A interact() 0 19 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the CLI-SYNTAX package.
7
 *
8
 * (c) Jitendra Adhikari <[email protected]>
9
 *     <https://github.com/adhocore>
10
 *
11
 * Licensed under MIT license.
12
 */
13
14
namespace Ahc\CliSyntax\Console;
15
16
use Ahc\Cli\Input\Command;
17
use Ahc\Cli\IO\Interactor;
18
use Ahc\CliSyntax\Exporter;
19
use Ahc\CliSyntax\Highlighter;
20
21
class ClishCommand extends Command
22
{
23
    public function __construct()
24
    {
25
        parent::__construct('clish', 'PHP CLI syntax highlight and/or export.');
26
27
        $this
28
            ->option('-o --output', 'Output filepath where PNG image is exported')
29
            ->option('-e --echo', 'Forces echo to STDOUT when --output is passed')
30
            ->option('-f --file', \implode("\n", [
31
                'Input PHP file to highlight and/or export',
32
                '(will read from piped input if file not given)',
33
            ]))
34
            ->option('-F --font', 'Font to use for export to png')
35
            ->usage(
36
                '<bold>  $0</end> <comment>--file file.php</end> ## print<eol/>'
37
                . '<bold>  cat file.php | $0</end> ## from piped stream<eol/>'
38
                . '<bold>  $0</end> <comment>< file.php ## from redirected stdin<eol/>'
39
                . '<bold>  $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>'
40
                . '<bold>  $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>'
41
                . '<bold>  $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>'
42
            );
43
    }
44
45
    public function interact(Interactor $io)
46
    {
47
        if ($this->file) {
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
48
            return;
49
        }
50
51
        $code = $io->readPiped(function ($reader) use ($io) {
52
            $io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true);
53
            $io->warn('(Opening tag `<?php` will be prepended as required):', true);
54
55
            return $reader->readAll();
56
        });
57
58
        if ('' !== $code && \substr($code, 0, 5) != '<?php') {
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type Ahc\Cli\Output\Writer; however, parameter $string of substr() 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

58
        if ('' !== $code && \substr(/** @scrutinizer ignore-type */ $code, 0, 5) != '<?php') {
Loading history...
59
            $eol  = \substr_count(\rtrim($code), "\n") ? "\n\n" : '';
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type Ahc\Cli\Output\Writer; however, parameter $str of rtrim() 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

59
            $eol  = \substr_count(\rtrim(/** @scrutinizer ignore-type */ $code), "\n") ? "\n\n" : '';
Loading history...
60
            $code = '<?php ' . $eol . $code;
0 ignored issues
show
Bug introduced by
Are you sure $code of type Ahc\Cli\Output\Writer|string can be used in concatenation? ( Ignorable by Annotation )

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

60
            $code = '<?php ' . $eol . /** @scrutinizer ignore-type */ $code;
Loading history...
61
        }
62
63
        $this->set('code', $code);
64
    }
65
66
    public function execute()
67
    {
68
        $code = $this->file ? \file_get_contents($this->file) : $this->code;
0 ignored issues
show
Bug Best Practice introduced by
The property code does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property file does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
69
        $code = \trim($code);
70
71
        if ('' === $code) {
72
            return;
73
        }
74
75
        $this->doExport($code);
76
        $this->doHighlight($code);
77
    }
78
79
    protected function doHighlight(string $code = null)
80
    {
81
        if (!$this->output || $this->echo) {
0 ignored issues
show
Bug Best Practice introduced by
The property echo does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property output does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
82
            echo new Highlighter($code);
83
        }
84
    }
85
86
    protected function doExport(string $code = null)
87
    {
88
        if (!$this->output) {
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
89
            return;
90
        }
91
92
        if (!\is_dir(\dirname($this->output))) {
93
            \mkdir(\dirname($this->output), 0755, true);
94
        }
95
96
        (new Exporter($code))->export($this->output, \array_filter(['font' => $this->font]));
0 ignored issues
show
Bug Best Practice introduced by
The property font does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
97
    }
98
}
99