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) : []; |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.