ChangeableDirectory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A createOption() 0 8 1
A changeWorkingDirectory() 0 23 4
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 Fidry\Console\Input\IO;
18
use InvalidArgumentException;
19
use Symfony\Component\Console\Exception\RuntimeException;
20
use Symfony\Component\Console\Input\InputOption;
21
use function chdir as native_chdir;
22
use function file_exists;
23
use function Safe\getcwd;
24
use function Safe\sprintf;
25
26
/**
27
 * @private
28
 * @codeCoverageIgnore
29
 */
30
final class ChangeableDirectory
31
{
32
    private const WORKING_DIR_OPT = 'working-dir';
33
34
    private function __construct()
35
    {
36
    }
37
38
    public static function createOption(): InputOption
39
    {
40
        return new InputOption(
41
            self::WORKING_DIR_OPT,
42
            'd',
43
            InputOption::VALUE_REQUIRED,
44
            'If specified, use the given directory as working directory.',
45
            null,
46
        );
47
    }
48
49
    public static function changeWorkingDirectory(IO $io): void
50
    {
51
        $workingDir = $io->getOption(self::WORKING_DIR_OPT)->asNullableString();
52
53
        if (null === $workingDir) {
54
            return;
55
        }
56
57
        if (!file_exists($workingDir)) {
58
            throw new InvalidArgumentException(
59
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

59
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
                    'Could not change the working directory to "%s": directory does not exists.',
61
                    $workingDir,
62
                ),
63
            );
64
        }
65
66
        if (!native_chdir($workingDir)) {
67
            throw new RuntimeException(
68
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

68
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
69
                    'Failed to change the working directory to "%s" from "%s".',
70
                    $workingDir,
71
                    getcwd(),
72
                ),
73
            );
74
        }
75
    }
76
}
77