11<?php
22
3+ declare (strict_types=1 );
4+
35namespace CssLint ;
46
7+ /**
8+ * @phpstan-import-type Errors from \CssLint\Linter
9+ * @package CssLint
10+ */
511class Cli
612{
7- private static $ SCRIPT_NAME = 'php-css-lint ' ;
8- private static $ RETURN_CODE_ERROR = 1 ;
9- private static $ RETURN_CODE_SUCCESS = 0 ;
13+ private const SCRIPT_NAME = 'php-css-lint ' ;
14+
15+ private const RETURN_CODE_ERROR = 1 ;
16+
17+ private const RETURN_CODE_SUCCESS = 0 ;
1018
1119 /**
1220 * Entrypoint of the cli, will execute the linter according to the given arguments
13- * @param array $aArguments arguments to be parsed (@see $_SERVER['argv'])
21+ * @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
1422 * @return int the return code related to the execution of the linter
1523 **/
16- public function run (array $ aArguments ): int
24+ public function run (array $ arguments ): int
1725 {
18- $ oCliArgs = $ this ->parseArguments ($ aArguments );
19- if (! $ oCliArgs ->filePathOrCssString ) {
26+ $ cliArgs = $ this ->parseArguments ($ arguments );
27+ if ($ cliArgs ->filePathOrCssString === null || $ cliArgs -> filePathOrCssString === '' || $ cliArgs -> filePathOrCssString === ' 0 ' ) {
2028 $ this ->printUsage ();
21- return self ::$ RETURN_CODE_SUCCESS ;
29+ return self ::RETURN_CODE_SUCCESS ;
2230 }
2331
24- $ oProperties = new \CssLint \Properties ();
25- if ($ oCliArgs ->options ) {
26- $ aOptions = json_decode ($ oCliArgs ->options , true );
32+ $ properties = new \CssLint \Properties ();
33+ if ($ cliArgs ->options !== null && $ cliArgs -> options !== '' && $ cliArgs -> options !== ' 0 ' ) {
34+ $ options = json_decode ($ cliArgs ->options , true );
2735
28- if (json_last_error ()) {
29- $ sErrorMessage = json_last_error_msg ();
30- $ this ->printError ('Unable to parse option argument: ' . $ sErrorMessage );
31- return self ::$ RETURN_CODE_ERROR ;
36+ if (json_last_error () !== 0 ) {
37+ $ errorMessage = json_last_error_msg ();
38+ $ this ->printError ('Unable to parse option argument: ' . $ errorMessage );
39+ return self ::RETURN_CODE_ERROR ;
3240 }
3341
34- if (!$ aOptions ) {
42+ if (!$ options ) {
3543 $ this ->printError ('Unable to parse empty option argument ' );
36- return self ::$ RETURN_CODE_ERROR ;
44+ return self ::RETURN_CODE_ERROR ;
3745 }
38- $ oProperties ->setOptions ($ aOptions );
46+
47+ if (!is_array ($ options )) {
48+ $ this ->printError ('Unable to parse option argument: must be a json object ' );
49+ return self ::RETURN_CODE_ERROR ;
50+ }
51+
52+ $ properties ->setOptions ($ options );
3953 }
4054
41- $ oCssLinter = new \CssLint \Linter ($ oProperties );
55+ $ cssLinter = new \CssLint \Linter ($ properties );
4256
43- $ sFilePathOrCssString = $ oCliArgs ->filePathOrCssString ;
44- if (!file_exists ($ sFilePathOrCssString )) {
45- return $ this ->lintString ($ oCssLinter , $ sFilePathOrCssString );
57+ $ filePathOrCssString = $ cliArgs ->filePathOrCssString ;
58+ if (!file_exists ($ filePathOrCssString )) {
59+ return $ this ->lintString ($ cssLinter , $ filePathOrCssString );
4660 }
4761
48- $ sFilePath = $ sFilePathOrCssString ;
49- if (!is_readable ($ sFilePath )) {
50- $ this ->printError ('File " ' . $ sFilePath . '" is not readable ' );
51- return self ::$ RETURN_CODE_ERROR ;
62+ $ filePath = $ filePathOrCssString ;
63+ if (!is_readable ($ filePath )) {
64+ $ this ->printError ('File " ' . $ filePath . '" is not readable ' );
65+ return self ::RETURN_CODE_ERROR ;
5266 }
5367
54- return $ this ->lintFile ($ oCssLinter , $ sFilePath );
68+ return $ this ->lintFile ($ cssLinter , $ filePath );
5569 }
5670
5771 /**
5872 * Retrieve the parsed Cli arguments from given arguments array
73+ * @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
5974 * @return \CssLint\CliArgs an instance of Cli arguments object containing parsed arguments
6075 */
61- private function parseArguments (array $ aArguments ): \CssLint \CliArgs
76+ private function parseArguments (array $ arguments ): \CssLint \CliArgs
6277 {
63- return new \CssLint \CliArgs ($ aArguments );
78+ return new \CssLint \CliArgs ($ arguments );
6479 }
6580
6681 /**
6782 * Display usage of the cli
6883 */
69- private function printUsage ()
84+ private function printUsage (): void
7085 {
7186 $ this ->printLine ('Usage: ' . PHP_EOL .
7287 '------ ' . PHP_EOL .
7388 PHP_EOL .
74- ' ' . self ::$ SCRIPT_NAME . ' [--options= \ '{ } \ '] css_file_or_string_to_lint ' . PHP_EOL .
89+ ' ' . self ::SCRIPT_NAME . " [--options='{ }'] css_file_or_string_to_lint " . PHP_EOL .
7590 PHP_EOL .
7691 'Arguments: ' . PHP_EOL .
7792 '---------- ' . PHP_EOL .
@@ -95,83 +110,84 @@ private function printUsage()
95110 '--------- ' . PHP_EOL .
96111 PHP_EOL .
97112 ' Lint a CSS file: ' . PHP_EOL .
98- ' ' . self ::$ SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css ' . PHP_EOL . PHP_EOL .
113+ ' ' . self ::SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css ' . PHP_EOL . PHP_EOL .
99114 ' Lint a CSS string: ' . PHP_EOL .
100- ' ' . self ::$ SCRIPT_NAME . ' ".test { color: red; }" ' . PHP_EOL . PHP_EOL .
115+ ' ' . self ::SCRIPT_NAME . ' ".test { color: red; }" ' . PHP_EOL . PHP_EOL .
101116 ' Lint with only tabulation as indentation: ' . PHP_EOL .
102- ' ' . self ::$ SCRIPT_NAME .
117+ ' ' . self ::SCRIPT_NAME .
103118 ' --options= \'{ "allowedIndentationChars": ["\t"] } \' ".test { color: red; }" ' . PHP_EOL .
104119 PHP_EOL . PHP_EOL );
105120 }
106121
107122 /**
108123 * Performs lint on a given file path
109- * @param \CssLint\Linter $oCssLinter the instance of the linter
110- * @param string $sFilePath the path of the file to be linted
124+ * @param \CssLint\Linter $cssLinter the instance of the linter
125+ * @param string $filePath the path of the file to be linted
111126 * @return int the return code related to the execution of the linter
112127 */
113- private function lintFile (\CssLint \Linter $ oCssLinter , string $ sFilePath ): int
128+ private function lintFile (\CssLint \Linter $ cssLinter , string $ filePath ): int
114129 {
115- $ this ->printLine ('# Lint CSS file " ' . $ sFilePath . '"... ' );
130+ $ this ->printLine ('# Lint CSS file " ' . $ filePath . '"... ' );
116131
117- if ($ oCssLinter ->lintFile ($ sFilePath )) {
118- $ this ->printLine ("\033[32m => CSS file \"" . $ sFilePath . "\" is valid \033[0m " . PHP_EOL );
119- return self ::$ RETURN_CODE_SUCCESS ;
132+ if ($ cssLinter ->lintFile ($ filePath )) {
133+ $ this ->printLine ("\033[32m => CSS file \"" . $ filePath . "\" is valid \033[0m " . PHP_EOL );
134+ return self ::RETURN_CODE_SUCCESS ;
120135 }
121136
122- $ this ->printLine ("\033[31m => CSS file \"" . $ sFilePath . "\" is not valid: \033[0m " . PHP_EOL );
123- $ this ->displayLinterErrors ($ oCssLinter ->getErrors ());
124- return self ::$ RETURN_CODE_ERROR ;
137+ $ this ->printLine ("\033[31m => CSS file \"" . $ filePath . "\" is not valid: \033[0m " . PHP_EOL );
138+ $ this ->displayLinterErrors ($ cssLinter ->getErrors ());
139+ return self ::RETURN_CODE_ERROR ;
125140 }
126141
127142
128143 /**
129144 * Performs lint on a given string
130- * @param \CssLint\Linter $oCssLinter the instance of the linter
131- * @param string $sString the CSS string to be linted
145+ * @param \CssLint\Linter $cssLinter the instance of the linter
146+ * @param string $stringValue the CSS string to be linted
132147 * @return int the return code related to the execution of the linter
133148 */
134- private function lintString (\CssLint \Linter $ oCssLinter , string $ sString ): int
149+ private function lintString (\CssLint \Linter $ cssLinter , string $ stringValue ): int
135150 {
136151 $ this ->printLine ('# Lint CSS string... ' );
137152
138- if ($ oCssLinter ->lintString ($ sString )) {
153+ if ($ cssLinter ->lintString ($ stringValue )) {
139154 $ this ->printLine ("\033[32m => CSS string is valid \033[0m " . PHP_EOL );
140- return self ::$ RETURN_CODE_SUCCESS ;
155+ return self ::RETURN_CODE_SUCCESS ;
141156 }
142157
143158 $ this ->printLine ("\033[31m => CSS string is not valid: \033[0m " . PHP_EOL );
144- $ this ->displayLinterErrors ($ oCssLinter ->getErrors ());
145- return self ::$ RETURN_CODE_ERROR ;
159+ $ this ->displayLinterErrors ($ cssLinter ->getErrors ());
160+ return self ::RETURN_CODE_ERROR ;
146161 }
147162
148163 /**
149164 * Display an error message
150- * @param string $sError the message to be displayed
165+ * @param string $error the message to be displayed
151166 */
152- private function printError (string $ sError )
167+ private function printError (string $ error ): void
153168 {
154- $ this ->printLine ("\033[31m/!\ Error: " . $ sError . "\033[0m " . PHP_EOL );
169+ $ this ->printLine ("\033[31m/!\ Error: " . $ error . "\033[0m " . PHP_EOL );
155170 }
156171
157172 /**
158173 * Display the errors returned by the linter
159- * @param array $aErrors the generated errors to be displayed
174+ * @param Errors $errors the generated errors to be displayed
160175 */
161- private function displayLinterErrors (array $ aErrors )
176+ private function displayLinterErrors (array $ errors ): void
162177 {
163- foreach ($ aErrors as $ sError ) {
164- $ this ->printLine ("\033[31m - " . $ sError . "\033[0m " );
178+ foreach ($ errors as $ error ) {
179+ $ this ->printLine ("\033[31m - " . $ error . "\033[0m " );
165180 }
181+
166182 $ this ->printLine ("" );
167183 }
168184
169185 /**
170186 * Display the given message in a new line
171- * @param string $sMessage the message to be displayed
187+ * @param string $message the message to be displayed
172188 */
173- private function printLine (string $ sMessage )
189+ private function printLine (string $ message ): void
174190 {
175- echo $ sMessage . PHP_EOL ;
191+ echo $ message . PHP_EOL ;
176192 }
177193}
0 commit comments