InstallCodeSniffer::findComposer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scriptura\QuickStart\Tasks;
6
7
use Scriptura\QuickStart\CommandRunner;
8
use Scriptura\QuickStart\ProjectFilesystem;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class InstallCodeSniffer
13
{
14
    public function __construct()
15
    {
16
    }
17
18
    public function __invoke(string $name, InputInterface $input, OutputInterface $output)
19
    {
20
        $output->writeln('<info># Installing Code Sniffer...</info>');
21
22
        $directory = getcwd() . '/' . $name;
23
24
        $composer = $this->findComposer();
25
26
        $commands = [
27
            $composer . ' require --dev squizlabs/php_codesniffer',
28
        ];
29
30
        $runner = new CommandRunner($output, $directory);
31
        $runner->run($commands);
32
33
        $filesystem = new ProjectFilesystem(getcwd() . '/' . $name);
34
35
        $filesystem->createFile(
36
            'phpcs.xml.dist',
37
            <<<FILE
38
<?xml version="1.0"?>
39
<ruleset name="{$name}">
40
    <description>The coding standard of {$name}</description>
41
42
    <file>app</file>
43
    <file>config</file>
44
    <file>database</file>
45
    <file>routes</file>
46
    <file>tests</file>
47
48
    <arg value="p" />
49
50
    <config name="ignore_warnings_on_exit" value="1" />
51
    <config name="ignore_errors_on_exit" value="1" />
52
    <config name="php_version" value="70205" />
53
54
    <arg name="basepath" value="."/>
55
    <arg name="extensions" value="php" />
56
    <arg name="colors" />
57
    <arg value="s" />
58
59
    <!-- Use the PSR12 Standard-->
60
    <rule ref="PSR12">
61
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
62
    </rule>
63
64
    <!-- Ban some functions -->
65
    <rule ref="Generic.PHP.ForbiddenFunctions">
66
        <properties>
67
            <property name="forbiddenFunctions" type="array">
68
                <element key="sizeof" value="count"/>
69
                <element key="delete" value="unset"/>
70
                <element key="print" value="echo"/>
71
                <element key="is_null" value="null"/>
72
                <element key="create_function" value="null"/>
73
            </property>
74
        </properties>
75
    </rule>
76
</ruleset>
77
FILE
78
        );
79
80
        $output->writeln('<info>Update file: composer.json</info>');
81
        $filesystem->updateFile('composer.json', function (string $content) {
82
            $refreshScript = <<<STRING
83
        "test": "phpunit",
84
        "check-style": "phpcs",
85
        "fix-style": "phpcbf; if [ $? -eq 1 ]; then exit 0; fi",
86
87
STRING;
88
            $patterns = [
89
                '/("scripts": {\n)/',
90
            ];
91
            $replace = [
92
                '${1}' . $refreshScript,
93
            ];
94
95
            return preg_replace($patterns, $replace, $content);
96
        });
97
98
99
        $isSuccessful = $runner->run([
100
            $composer . ' fix-style',
101
            $composer . ' check-style',
102
        ]);
103
104
        if ($isSuccessful) {
105
            $output->writeln('<comment>Code Sniffer installed.</comment>');
106
        }
107
    }
108
109
    /**
110
     * Get the composer command for the environment.
111
     *
112
     * @return string
113
     */
114
    protected function findComposer(): string
115
    {
116
        $composerPath = getcwd() . '/composer.phar';
117
118
        if (file_exists($composerPath)) {
119
            return '"' . PHP_BINARY . '" ' . $composerPath;
120
        }
121
122
        return 'composer';
123
    }
124
}
125