Completed
Push — master ( 0ab984...c72dd5 )
by Théo
02:46
created

AddPrefixCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 2
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Console\Command;
16
17
use Humbug\PhpScoper\Handler\HandleAddPrefix;
18
use Humbug\PhpScoper\Logger\ConsoleLogger;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Exception\RuntimeException;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Style\SymfonyStyle;
25
use Symfony\Component\Filesystem\Filesystem;
26
27
final class AddPrefixCommand extends Command
28
{
29
    /** @private */
30
    const PREFIX_ARG = 'prefix';
31
    /** @private */
32
    const PATH_ARG = 'paths';
33
34
    private $fileSystem;
35
    private $handle;
36
37
    /**
38
     * @inheritdoc
39
     */
40 12
    public function __construct(HandleAddPrefix $handle)
41
    {
42 12
        parent::__construct();
43
44 12
        $this->fileSystem = new Filesystem();
45 12
        $this->handle = $handle;
46 12
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 12
    protected function configure()
52
    {
53
        $this
54 12
            ->setName('add-prefix')
55 12
            ->setDescription('Goes through all the PHP files found in the given paths to apply the given prefix to namespaces & FQNs.')
56 12
            ->addArgument(self::PREFIX_ARG, InputArgument::REQUIRED, 'The namespace prefix to add')
57 12
            ->addArgument(self::PATH_ARG, InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to process.')
58
        ;
59 12
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 10
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66 10
        $this->validatePrefix($input);
67 5
        $this->validatePaths($input);
68
69 5
        $logger = new ConsoleLogger(
70 5
            $this->getApplication(),
0 ignored issues
show
Bug introduced by
It seems like $this->getApplication() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71 5
            new SymfonyStyle($input, $output)
72
        );
73
74 5
        $logger->outputScopingStart(
75 5
            $input->getArgument(self::PREFIX_ARG),
76 5
            $input->getArgument(self::PATH_ARG)
77
        );
78
79
        try {
80 5
            $this->handle->__invoke(
81 5
                $input->getArgument(self::PREFIX_ARG),
82 5
                $input->getArgument(self::PATH_ARG),
83
                $logger
84
            );
85 1
        } catch (\Throwable $throwable) {
86 1
            $logger->outputScopingEndWithFailure();
87
88 1
            throw $throwable;
89
        }
90
91 4
        $logger->outputScopingEnd();
92 4
    }
93
94 10
    private function validatePrefix(InputInterface $input)
95
    {
96 10
        $prefix = trim($input->getArgument(self::PREFIX_ARG));
97
98 10
        if (1 === preg_match('/(?<prefix>.*?)\\\\*$/', $prefix, $matches)) {
99 10
            $prefix = $matches['prefix'];
100
        }
101
102 10
        if ('' === $prefix) {
103 5
            throw new RuntimeException(
104
                sprintf(
105 5
                    'Expected "%s" argument to be a non empty string.',
106 5
                    self::PREFIX_ARG
107
                )
108
            );
109
        }
110
111 5
        $input->setArgument(self::PREFIX_ARG, $prefix);
112 5
    }
113
114 5
    private function validatePaths(InputInterface $input)
115
    {
116 5
        $cwd = getcwd();
117 5
        $fileSystem = $this->fileSystem;
118
119 5
        $paths = array_map(
120 5
            function (string $path) use ($cwd, $fileSystem) {
121 5
                if (false === $fileSystem->isAbsolutePath($path)) {
122 1
                    return $cwd.DIRECTORY_SEPARATOR.$path;
123
                }
124
125 5
                return $path;
126 5
            },
127 5
            $input->getArgument(self::PATH_ARG)
128
        );
129
130 5
        $input->setArgument(self::PATH_ARG, $paths);
131 5
    }
132
}
133