Completed
Push — master ( c3819c...a367a7 )
by Jitendra
16s queued 12s
created

ClishCommand::fixFont()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 6
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
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('-f --file', \implode("\n", [
31
                'Input PHP file to highlight and/or export',
32
                '(will read from piped input if file not given)',
33
            ]), null, '')
34
            ->option('-F --font', 'Font to use for export to png', null, '')
35
            ->usage(
36
                '<bold>  $0</end> <comment>--file file.php</end> ## print<eol/>'
37
                . '<comment>  cat file.php |</end> <bold>$0</end> ## from piped stream<eol/>'
38
                . '<bold>  $0</end> <comment>< file.php</end> ## 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
70
        if ('' === \trim($code)) {
71
            return;
72
        }
73
74
        $this->doExport($code);
75
        $this->doHighlight($code);
76
    }
77
78
    protected function doHighlight(string $code = null)
79
    {
80
        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...
81
            $this->app()->io()->raw((string) new Highlighter($code));
82
        }
83
    }
84
85
    protected function doExport(string $code = null)
86
    {
87
        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...
88
            return;
89
        }
90
91
        if (!\is_dir(\dirname($this->output))) {
92
            \mkdir(\dirname($this->output), 0755, true);
93
        }
94
95
        $options = ['font' => $this->fixFont($this->font ?: 'ubuntu')];
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...
96
97
        (new Exporter($code))->export($this->output, $options);
98
    }
99
100
    /** @codeCoverageIgnore */
101
    protected function fixFont(string $font): string
102
    {
103
        if (\Phar::running()) {
104
            $basename = \basename($font, '.ttf');
105
            $pharfont = __DIR__ . "/../../font/$basename.ttf";
106
107
            if (!\is_file($font) && \is_file($pharfont)) {
108
                \copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf');
109
            }
110
        }
111
112
        return $font;
113
    }
114
}
115