|
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
|
|
|
final class ProjectRootHelper |
|
14
|
|
|
{ |
|
15
|
|
|
private const PROJECT_ROOT = 'project-root'; |
|
16
|
|
|
private const RELATIVE_PATH_TO_CODE = 'relative-path-to-code'; |
|
17
|
|
|
|
|
18
|
|
|
public static function configureProjectRootOption(Command $command): void |
|
19
|
|
|
{ |
|
20
|
|
|
$command->addOption( |
|
21
|
|
|
self::PROJECT_ROOT, |
|
22
|
|
|
null, |
|
23
|
|
|
InputOption::VALUE_REQUIRED, |
|
24
|
|
|
'Path to the root of the project you are creating baseline for', |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
$command->addOption( |
|
28
|
|
|
self::RELATIVE_PATH_TO_CODE, |
|
29
|
|
|
null, |
|
30
|
|
|
InputOption::VALUE_REQUIRED, |
|
31
|
|
|
"Relative path between project root and code being analysed. (Only needed for static analysis tools that don't provide full path to files containing issues)", |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws SarbException |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getProjectRoot(InputInterface $input): ProjectRoot |
|
39
|
|
|
{ |
|
40
|
|
|
$projectRootAsString = CliConfigReader::getOption($input, self::PROJECT_ROOT); |
|
41
|
|
|
$cwd = self::getCwd(); |
|
42
|
|
|
|
|
43
|
|
|
if (null === $projectRootAsString) { |
|
44
|
|
|
return ProjectRoot::fromCurrentWorkingDirectory($cwd); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$projectRoot = ProjectRoot::fromProjectRoot($projectRootAsString, $cwd); |
|
48
|
|
|
|
|
49
|
|
|
$relativePathAsString = CliConfigReader::getOption($input, self::RELATIVE_PATH_TO_CODE); |
|
50
|
|
|
if (null !== $relativePathAsString) { |
|
51
|
|
|
$projectRoot = $projectRoot->withRelativePath($relativePathAsString); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $projectRoot; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @codeCoverageIgnore |
|
59
|
|
|
* |
|
60
|
|
|
* @throws SarbException |
|
61
|
|
|
*/ |
|
62
|
|
|
private static function getCwd(): string |
|
63
|
|
|
{ |
|
64
|
|
|
$cwd = getcwd(); |
|
65
|
|
|
if (false === $cwd) { |
|
66
|
|
|
throw new SarbException('Can not get current working directory. Specify project root with option: '.self::PROJECT_ROOT); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $cwd; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|