ClishCommand   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
eloc 48
c 4
b 1
f 2
dl 0
loc 94
rs 10
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fixFont() 0 12 4
A __construct() 0 21 1
A doExport() 0 13 4
A execute() 0 10 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', null, '')
29
            ->option('-e --echo', 'Forces echo to STDOUT when --output is passed', null, '')
30
            ->option('-l --with-line-no', 'Highlight with line number')
31
            ->option('-f --file', \implode("\n", [
32
                'Input PHP file to highlight and/or export',
33
                '(will read from piped input if file not given)',
34
            ]), null, '')
35
            ->option('-F --font', 'Font to use for export to png', null, '')
36
            ->usage(
37
                '<bold>  $0</end> <comment>--file file.php</end> ## print<eol/>'
38
                . '<comment>  cat file.php |</end> <bold>$0</end> ## from piped stream<eol/>'
39
                . '<bold>  $0</end> <comment>< file.php</end> ## from redirected stdin<eol/>'
40
                . '<bold>  $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>'
41
                . '<bold>  $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>'
42
                . '<bold>  $0</end> <comment>--file file.php --with-line-no</end> ## print with lineno<eol/>'
43
                . '<bold>  $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>'
44
            );
45
    }
46
47
    public function interact(Interactor $io)
48
    {
49
        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...
50
            return;
51
        }
52
53
        $code = $io->readPiped(function ($reader) use ($io) {
54
            $io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true);
55
            $io->warn('(Opening tag `<?php` will be prepended as required):', true);
56
57
            return $reader->readAll();
58
        });
59
60
        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

60
        if ('' !== $code && \substr(/** @scrutinizer ignore-type */ $code, 0, 5) != '<?php') {
Loading history...
61
            $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

61
            $eol  = \substr_count(\rtrim(/** @scrutinizer ignore-type */ $code), "\n") ? "\n\n" : '';
Loading history...
62
            $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

62
            $code = '<?php ' . $eol . /** @scrutinizer ignore-type */ $code;
Loading history...
63
        }
64
65
        $this->set('code', $code);
66
    }
67
68
    public function execute()
69
    {
70
        $code = $this->file ? \file_get_contents($this->file) : $this->code;
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...
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...
71
72
        if ('' === \trim($code)) {
73
            return;
74
        }
75
76
        $this->doExport($code);
77
        $this->doHighlight($code);
78
    }
79
80
    protected function doHighlight(string $code = null)
81
    {
82
        if (!$this->output || $this->echo) {
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...
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...
83
            $this->app()->io()->raw((new Highlighter)->highlight($code, ['lineNo' => $this->lineNo]));
0 ignored issues
show
Bug Best Practice introduced by
The property lineNo does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
84
        }
85
    }
86
87
    protected function doExport(string $code = null)
88
    {
89
        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...
90
            return;
91
        }
92
93
        if (!\is_dir(\dirname($this->output))) {
94
            \mkdir(\dirname($this->output), 0755, true);
95
        }
96
97
        $options = ['font' => $this->fixFont($this->font ?: 'ubuntu'), 'lineNo' => $this->lineNo];
0 ignored issues
show
Bug Best Practice introduced by
The property lineNo 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 font does not exist on Ahc\CliSyntax\Console\ClishCommand. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
99
        (new Exporter($code))->export($this->output, $options);
100
    }
101
102
    /** @codeCoverageIgnore */
103
    protected function fixFont(string $font): string
104
    {
105
        if (\Phar::running()) {
106
            $basename = \basename($font, '.ttf');
107
            $pharfont = __DIR__ . "/../../font/$basename.ttf";
108
109
            if (!\is_file($font) && \is_file($pharfont)) {
110
                \copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf');
111
            }
112
        }
113
114
        return $font;
115
    }
116
}
117