Passed
Push — master ( 161f97...717671 )
by Théo
02:13
created

configureWorkingDirOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Command;
16
17
use Assert\Assertion;
18
use Symfony\Component\Console\Exception\RuntimeException;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
22
trait ChangeableWorkingDirectory
23
{
24
    /**
25
     * @internal using a static property as traits cannot have constants
26
     */
27
    private static $WORKING_DIR_OPT = 'working-dir';
28
29
    final public function changeWorkingDirectory(InputInterface $input): void
30
    {
31
        $workingDir = $input->getOption(self::$WORKING_DIR_OPT);
32
33
        if (null === $workingDir) {
34
            return;
35
        }
36
37
        Assertion::directory(
38
            $workingDir,
39
            'Could not change the working directory to "%s": directory does not exists or file is not a directory.'
40
        );
41
42
        if (false === chdir($workingDir)) {
43
            throw new RuntimeException(
44
                sprintf(
45
                    'Failed to change the working directory to "%s" from "%s".',
46
                    $workingDir,
47
                    getcwd()
48
                )
49
            );
50
        }
51
    }
52
53
    final private function configureWorkingDirOption(): void
54
    {
55
        $this->addOption(
1 ignored issue
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               addOption(
Loading history...
56
            self::$WORKING_DIR_OPT,
57
            'd',
58
            InputOption::VALUE_REQUIRED,
59
            'If specified, use the given directory as working directory.',
60
            null
61
        );
62
    }
63
}
64