1 | <?php |
||
54 | class BuildTypeFromEtcIssue implements BuildTypeFromFile |
||
55 | { |
||
56 | /** |
||
57 | * use /etc/issue (if it exists) to work out what operating system we |
||
58 | * are looking at |
||
59 | * |
||
60 | * @param string $path |
||
61 | * path to the file to parse |
||
62 | * @return null|OsType |
||
|
|||
63 | * OsType if we can determine the operating system |
||
64 | * null if we cannot |
||
65 | */ |
||
66 | public function __invoke($path = '/etc/issue') |
||
70 | |||
71 | /** |
||
72 | * use /etc/issue (if it exists) to work out what operating system we |
||
73 | * are looking at |
||
74 | * |
||
75 | * @return null|OsType |
||
76 | * OsType if we can determine the operating system |
||
77 | * null if we cannot |
||
78 | */ |
||
79 | public static function usingDefaultPath() |
||
80 | { |
||
81 | return self::usingPath('/etc/issue'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * use /etc/issue (if it exists) to work out what operating system we |
||
86 | * are looking at |
||
87 | * |
||
88 | * @param string $path |
||
89 | * path to the file to parse |
||
90 | * @return null|OsType |
||
91 | * OsType if we can determine the operating system |
||
92 | * null if we cannot |
||
93 | */ |
||
94 | public static function usingPath($path) |
||
95 | { |
||
96 | // make sure we have the file! |
||
97 | if (!HasEtcIssue::check($path)) { |
||
98 | return null; |
||
99 | } |
||
100 | |||
101 | // make sure the file is readable |
||
102 | RequireReadableFile::check($path); |
||
103 | |||
104 | // what do we have? |
||
105 | $fileContents = file_get_contents($path); |
||
106 | |||
107 | // do we have a match? |
||
108 | return self::matchContentsToType($fileContents); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * do we have a regex that matches the contents of our file? |
||
113 | * |
||
114 | * @param string $fileContents |
||
115 | * the contents of the file to check |
||
116 | * @return null|OsType |
||
117 | * OsType if we can determine the operating system |
||
118 | * null if we cannot |
||
119 | */ |
||
120 | private static function matchContentsToType($fileContents) |
||
130 | |||
131 | /** |
||
132 | * does the given regex match our file |
||
133 | * |
||
134 | * @param string $type |
||
135 | * the OsType class to return if the regex matches |
||
136 | * @param string $regex |
||
137 | * the regex to try |
||
138 | * @param string $fileContents |
||
139 | * the text that we apply the regex to |
||
140 | * @return null|OsType |
||
141 | * OsType if the regex matches |
||
142 | * null otherwise |
||
143 | */ |
||
144 | private static function matchTypeToRegex($type, $regex, $fileContents) |
||
157 | |||
158 | /** |
||
159 | * a map of regexes to OsType classes |
||
160 | * |
||
161 | * @var array |
||
162 | */ |
||
163 | private static $osTypes = [ |
||
164 | "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class, |
||
165 | "|^Debian GNU/Linux (?<version>\d+)|" => Debian::class, |
||
166 | "|^Linux Mint (?<version>\d+\.\d+)|" => LinuxMint::class, |
||
167 | "|^Ubuntu (?<version>\d+\.\d+)|" => Ubuntu::class, |
||
168 | ]; |
||
169 | } |
||
170 |
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.