Passed
Pull Request — master (#33)
by Melech
04:17 queued 54s
created

Suite::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Test\Suites;
15
16
use Valkyrja\Test\Enums\Argument;
17
use Valkyrja\Test\Output\Output;
18
use Valkyrja\Test\Output\Outputters\EchoOutput;
19
use Valkyrja\Test\Output\Results;
20
use Valkyrja\Test\Suite as Contract;
21
22
/**
23
 * Class Suite.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
class Suite implements Contract
28
{
29
    public function __construct(
30
        protected Output $output = new EchoOutput(),
31
        protected Results $results = new Results\Results(),
32
    ) {
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function run(array $args = null): void
39
    {
40
        $args ??= $this->getServerArgs();
41
        $results = $this->results;
0 ignored issues
show
Unused Code introduced by
The assignment to $results is dead and can be removed.
Loading history...
42
        $files   = $this->getFilesFromArgs($args);
0 ignored issues
show
Unused Code introduced by
The assignment to $files is dead and can be removed.
Loading history...
43
44
        $this->output->title();
45
        $this->output->sectionSpacing();
46
    }
47
48
    /**
49
     * Get the server arguments.
50
     */
51
    protected function getServerArgs(): array
52
    {
53
        global $argv;
54
55
        return $argv;
56
    }
57
58
    /**
59
     * Get the files from an array of arguments.
60
     *
61
     * @param string[] $args The arguments
62
     *
63
     * @return string[]
64
     */
65
    protected function getFilesFromArgs(array $args): array
66
    {
67
        return $this->getFilteredArgs(Argument::file->name, $args);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Valkyrja\Test\Enums\Argument.
Loading history...
68
    }
69
70
    /**
71
     * Get a filtered array of arguments from an array of arguments given a specific name.
72
     *
73
     * @param string[] $args The arguments
74
     *
75
     * @return string[]
76
     */
77
    protected function getFilteredArgs(string $argName, array $args): array
78
    {
79
        $filteredArgs = [];
80
81
        foreach ($args as $arg) {
82
            if (str_contains($arg, "--{$argName}")) {
83
                $filteredArgs[] = str_replace("--{$argName}=", '', $arg);
84
            }
85
        }
86
87
        return $filteredArgs;
88
    }
89
}
90