Completed
Push — master ( 29472a...0fa5c2 )
by Richan
04:47 queued 03:37
created

Command::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Suitmedia\LighthouseAudit;
4
5
use LogicException;
6
use Suitmedia\LighthouseAudit\Audit\AuditManager;
7
use Suitmedia\LighthouseAudit\Audit\Concerns\SanitizeInput;
8
use Symfony\Component\Console\Command\Command as AbstractCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
final class Command extends AbstractCommand
15
{
16
    use SanitizeInput;
17
18
    public const DEFAULT_URL_PREFIX = 'http://localhost:8000/';
19
    public const DEFAULT_MODE = 'mobile';
20
    public const DEFAULT_PERFORMANCE = '80';
21
    public const DEFAULT_BEST_PRACTICES = '80';
22
    public const DEFAULT_ACCESSIBILITY = '80';
23
    public const DEFAULT_SEO = '80';
24
    public const DEFAULT_PWA = '0';
25
26
    /**
27
     * Configures the lighthouse-audit command.
28
     *
29
     * @return void
30
     */
31
    protected function configure() :void
32
    {
33
        $this->setName('lighthouse-audit')
34
            ->setDefinition([new InputArgument(
35
                'path',
36
                InputArgument::REQUIRED,
37
                'Specify the path of a directory to analyse.'
38
            )])
39
            ->addOption(
40
                'url-prefix',
41
                null,
42
                InputOption::VALUE_OPTIONAL,
43
                'Define the url prefix that should be used when testing.',
44
                self::DEFAULT_URL_PREFIX
45
            )
46
            ->addOption(
47
                'mode',
48
                null,
49
                InputOption::VALUE_OPTIONAL,
50
                'Define the mode to run Lighthouse audit. Option: mobile,desktop',
51
                self::DEFAULT_MODE
52
            )
53
            ->addOption(
54
                'performance',
55
                null,
56
                InputOption::VALUE_OPTIONAL,
57
                'Define the minimal performance score for audit to pass',
58
                self::DEFAULT_PERFORMANCE
59
            )
60
            ->addOption(
61
                'best-practices',
62
                null,
63
                InputOption::VALUE_OPTIONAL,
64
                'Define the minimal best-practices score for audit to pass',
65
                self::DEFAULT_BEST_PRACTICES
66
            )
67
            ->addOption(
68
                'accessibility',
69
                null,
70
                InputOption::VALUE_OPTIONAL,
71
                'Define the minimal accessibility score for audit to pass',
72
                self::DEFAULT_ACCESSIBILITY
73
            )
74
            ->addOption(
75
                'seo',
76
                null,
77
                InputOption::VALUE_OPTIONAL,
78
                'Define the minimal seo score for audit to pass',
79
                self::DEFAULT_SEO
80
            )
81
            ->addOption(
82
                'pwa',
83
                null,
84
                InputOption::VALUE_OPTIONAL,
85
                'Define the minimal pwa score for audit to pass',
86
                self::DEFAULT_PWA
87
            )
88
            ->addOption(
89
                'except',
90
                null,
91
                InputOption::VALUE_OPTIONAL,
92
                'Provide a list of filenames that you wish to exclude, separated by commas.'.PHP_EOL
93
            )
94
            ->addOption(
95
                'chrome-flags',
96
                null,
97
                InputOption::VALUE_OPTIONAL,
98
                'Custom flags to pass to Chrome (space-delimited). For a full list of flags, '.PHP_EOL.'see http://peter.sh/experiments/chromium-command-line-switches/.'.PHP_EOL
99
            );
100
    }
101
102
    /**
103
     * Executes the current command.
104
     *
105
     * This method is not abstract because you can use this class
106
     * as a concrete class. In this case, instead of defining the
107
     * execute() method, you set the code to execute by passing
108
     * a Closure to the setCode() method.
109
     *
110
     * @param InputInterface $input
111
     * @param OutputInterface $output
112
     * @return int
113
     * @throws LogicException|\Exception
114
     */
115
    protected function execute(InputInterface $input, OutputInterface $output) :int
116
    {
117
        $app = $this->getApplication();
118
        $excludedFiles = $this->getExcludedFiles($input);
119
120
        $output->writeln($app->getLongVersion());
121
        $output->writeln('');
122
123
        $finder = new HtmlFileFinder($input->getArgument('path'));
124
        $files = $finder->getFiles();
125
126
        foreach ($files as $file) {
127
            if (in_array($file, $excludedFiles, true)) {
128
                continue;
129
            }
130
131
            $audit = new AuditManager(
132
                $this->getApplication()->getProcessBuilder(),
133
                $input,
134
                $output,
135
                $file
136
            );
137
            $audit->run();
0 ignored issues
show
Documentation Bug introduced by
The method run does not exist on object<Suitmedia\Lightho...dit\Audit\AuditManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
138
        }
139
140
        return 0;
141
    }
142
143
    /**
144
     * Get the list of files that should be excluded from analysis.
145
     *
146
     * @param InputInterface $input
147
     * @return array
148
     */
149
    public function getExcludedFiles(InputInterface $input) :array
150
    {
151
        $except = $input->getOption('except');
152
153
        $files = is_string($except) ? explode(',', $this->trimDoubleQuotes($except)) : [];
154
155
        return array_filter(array_map('trim', $files));
156
    }
157
}
158