FileChanged::getRestriction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Condition;
13
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Git\ChangedFiles\Detector\Factory;
16
use CaptainHook\App\Hook\Restriction;
17
use CaptainHook\App\Hooks;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Class FileChange
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhook-git/captainhook
26
 * @since   Class available since Release 4.2.0
27
 */
28
abstract class FileChanged extends File
29
{
30
    /**
31
     * List of file to watch
32
     *
33
     * @var array<string>
34
     */
35
    protected array $filesToWatch;
36
37
    /**
38
     * Git filter options
39
     *
40
     * @var array<string>
41
     */
42
    private array $filter;
43
44
    /**
45
     * FileChange constructor
46
     *
47
     * @param array<string> $files
48
     * @param string $filter
49
     */
50 13
    public function __construct(array $files, string $filter = 'ACMR')
51
    {
52 13
        $this->filesToWatch = $files;
53 13
        $this->filter       = !empty($filter) ? str_split($filter) : [];
0 ignored issues
show
Documentation Bug introduced by
It seems like ! empty($filter) ? str_split($filter) : array() can also be of type true. However, the property $filter is declared as type string[]. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
    }
55
56
    /**
57
     * Return the hook restriction information
58
     *
59
     * @return \CaptainHook\App\Hook\Restriction
60
     */
61 4
    public static function getRestriction(): Restriction
62
    {
63 4
        return Restriction::fromArray([Hooks::PRE_PUSH, Hooks::POST_CHECKOUT, Hooks::POST_MERGE, Hooks::POST_REWRITE]);
64
    }
65
66
    /**
67
     * Evaluates a condition
68
     *
69
     * @param  \CaptainHook\App\Console\IO       $io
70
     * @param  \SebastianFeldmann\Git\Repository $repository
71
     * @return bool
72
     */
73
    abstract public function isTrue(IO $io, Repository $repository): bool;
74
75
    /**
76
     * Use 'diff-tree' to find the changed files after this merge or checkout
77
     *
78
     * In case of a checkout it is easy because the arguments 'previousHead' and 'newHead' exist.
79
     * In case of a merge determining this hashes is more difficult, so we are using the 'ref-log'
80
     * to do it and using 'HEAD@{1}' as the last position before the merge and 'HEAD' as the
81
     * current position after the merge.
82
     *
83
     * @param  \CaptainHook\App\Console\IO       $io
84
     * @param  \SebastianFeldmann\Git\Repository $repository
85
     * @return array<string>
86
     */
87 12
    protected function getChangedFiles(IO $io, Repository $repository): array
88
    {
89 12
        $factory  = new Factory();
90 12
        $detector = $factory->getDetector($io, $repository);
91
92 12
        return $detector->getChangedFiles($this->filter);
93
    }
94
}
95