1 | <?php |
||
51 | class BuildTypeFromEtcRedhatRelease implements BuildTypeFromFile |
||
52 | { |
||
53 | /** |
||
54 | * use /etc/redhat-release (if it exists) to work out what flavour of |
||
55 | * RedHat Linux we are looking at |
||
56 | * |
||
57 | * @param string $path |
||
58 | * path to the file to parse |
||
59 | * @return null|OsType |
||
|
|||
60 | * OsType if we can determine the operating system |
||
61 | * null if we cannot |
||
62 | */ |
||
63 | public function __invoke($path = '/etc/redhat-release') |
||
67 | |||
68 | /** |
||
69 | * use /etc/redhat-release (if it exists) to work out what flavour of |
||
70 | * RedHat Linux we are looking at |
||
71 | * |
||
72 | * @return null|OsType |
||
73 | * OsType if we can determine the operating system |
||
74 | * null if we cannot |
||
75 | */ |
||
76 | public static function usingDefaultPath() |
||
77 | { |
||
78 | return self::usingPath('/etc/redhat-release'); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * use /etc/redhat-release (if it exists) to work out what flavour of |
||
83 | * RedHat Linux we are looking at |
||
84 | * |
||
85 | * @param string $path |
||
86 | * path to the file to parse |
||
87 | * @return null|OsType |
||
88 | * OsType if we can determine the operating system |
||
89 | * null if we cannot |
||
90 | */ |
||
91 | public static function usingPath($path) |
||
92 | { |
||
93 | // make sure we have the file! |
||
94 | if (!HasEtcRedhatRelease::check($path)) { |
||
95 | return null; |
||
96 | } |
||
97 | |||
98 | // make sure the file is readable |
||
99 | RequireReadableFile::check($path); |
||
100 | |||
101 | // what do we have? |
||
102 | $fileContents = file_get_contents($path); |
||
103 | |||
104 | // do we have a match? |
||
105 | return self::matchContentsToType($fileContents); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * do we have a regex that matches the contents of our file? |
||
110 | * |
||
111 | * @param string $fileContents |
||
112 | * the contents of the file to check |
||
113 | * @return null|OsType |
||
114 | * OsType if we can determine the operating system |
||
115 | * null if we cannot |
||
116 | */ |
||
117 | private static function matchContentsToType($fileContents) |
||
127 | |||
128 | /** |
||
129 | * does the given regex match our file |
||
130 | * |
||
131 | * @param string $type |
||
132 | * the OsType class to return if the regex matches |
||
133 | * @param string $regex |
||
134 | * the regex to try |
||
135 | * @param string $fileContents |
||
136 | * the text that we apply the regex to |
||
137 | * @return null|OsType |
||
138 | * OsType if the regex matches |
||
139 | * null otherwise |
||
140 | */ |
||
141 | private static function matchTypeToRegex($type, $regex, $fileContents) |
||
154 | |||
155 | /** |
||
156 | * a map of regexes to OsType classes |
||
157 | * |
||
158 | * @var array |
||
159 | */ |
||
160 | private static $osTypes = [ |
||
161 | "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class, |
||
162 | "|^CentOS Linux release (?<version>\d+\.\d+)|" => CentOS::class, |
||
163 | ]; |
||
164 | } |
||
165 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.