Passed
Pull Request — master (#69)
by Dave
02:17
created

ProjectRootHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureProjectRootOption() 0 7 1
A getProjectRoot() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class ProjectRootHelper
14
{
15
    private const PROJECT_ROOT = 'project-root';
16
17
    public static function configureProjectRootOption(Command $command): void
18
    {
19
        $command->addOption(
20
            self::PROJECT_ROOT,
21
            null,
22
            InputOption::VALUE_REQUIRED,
23
            'Path to the root of the project you are creating baseline for'
24
        );
25
    }
26
27
    /**
28
     * @throws InvalidConfigException
29
     * @throws SarbException
30
     */
31
    public static function getProjectRoot(InputInterface $input): ProjectRoot
32
    {
33
        $projectRootAsString = CliConfigReader::getOption($input, self::PROJECT_ROOT);
34
35
        $cwd = getcwd();
36
        if (false === $cwd) {
37
            // Impossible to easily test `cwd` failing, to exclude from tests.
38
            throw new SarbException('Can not get current working directory. Specify project root with options: '.self::PROJECT_ROOT); // @codeCoverageIgnore
39
        }
40
41
        if (null === $projectRootAsString) {
42
            $projectRootAsString = $cwd;
43
        }
44
45
        return new ProjectRoot($projectRootAsString, $cwd);
46
    }
47
}
48