|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SnifferReport\Service; |
|
4
|
|
|
|
|
5
|
|
|
use \PHP_CodeSniffer; |
|
6
|
|
|
use SnifferReport\Exception\UnableToParseExclusionListException; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Validator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Checks if a given url is a valid Git url. |
|
12
|
|
|
* |
|
13
|
|
|
* @param $url |
|
14
|
|
|
* The URL to be checked. |
|
15
|
|
|
* |
|
16
|
|
|
* @return bool |
|
17
|
|
|
* Whether the given URL is valid or not. |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function isGitUrl($url) |
|
20
|
|
|
{ |
|
21
|
|
|
if (!$url) { |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// @todo: change validation to accept any git url. |
|
26
|
|
|
$patterns = array( |
|
27
|
|
|
'#git.drupal.org/project#', |
|
28
|
|
|
'#git.drupal.org/sandbox#', |
|
29
|
|
|
'#github.com#', |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$valid = false; |
|
33
|
|
|
foreach ($patterns as $pattern) { |
|
34
|
|
|
if (preg_match($pattern, $url)) { |
|
35
|
|
|
$valid = true; |
|
36
|
|
|
break; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $valid; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Checks if given standards are supported by the application. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $standards |
|
47
|
|
|
* The standards to be validated |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
* Whether the given standards are valid or not. |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function areAllStandardsValid($standards) |
|
53
|
|
|
{ |
|
54
|
|
|
$standards = explode(',', $standards); |
|
55
|
|
|
$installed_standards = PHP_CodeSniffer::getInstalledStandards(); |
|
56
|
|
|
foreach ($standards as $standard) { |
|
57
|
|
|
if (!in_array($standard, $installed_standards)) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Validates if given file should be sniffed based on sent options. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $file |
|
69
|
|
|
* @param string $exclusion_list |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
* |
|
73
|
|
|
* @throws UnableToParseExclusionListException |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function fileShouldBeSniffed($file, $exclusion_list) |
|
76
|
|
|
{ |
|
77
|
|
|
$file_list = json_decode($exclusion_list); |
|
78
|
|
|
|
|
79
|
|
|
if (!empty($exclusion_list) && empty($file_list)) { |
|
80
|
|
|
throw new UnableToParseExclusionListException(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
foreach ($file_list as $item) { |
|
84
|
|
|
if (strpos($item, $file)) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|