11<?php
22
3+ declare (strict_types=1 );
4+
35namespace CssLint ;
46
57class Cli
68{
7- private static $ SCRIPT_NAME = 'php-css-lint ' ;
8- private static $ RETURN_CODE_ERROR = 1 ;
9- private static $ RETURN_CODE_SUCCESS = 0 ;
9+ private const SCRIPT_NAME = 'php-css-lint ' ;
10+
11+ private const RETURN_CODE_ERROR = 1 ;
12+
13+ private const RETURN_CODE_SUCCESS = 0 ;
1014
1115 /**
1216 * Entrypoint of the cli, will execute the linter according to the given arguments
13- * @param array $aArguments arguments to be parsed (@see $_SERVER['argv'])
17+ * @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
1418 * @return int the return code related to the execution of the linter
1519 **/
16- public function run (array $ aArguments ): int
20+ public function run (array $ arguments ): int
1721 {
18- $ oCliArgs = $ this ->parseArguments ($ aArguments );
19- if (! $ oCliArgs ->filePathOrCssString ) {
22+ $ cliArgs = $ this ->parseArguments ($ arguments );
23+ if ($ cliArgs ->filePathOrCssString === null || $ cliArgs -> filePathOrCssString === '' || $ cliArgs -> filePathOrCssString === ' 0 ' ) {
2024 $ this ->printUsage ();
21- return self ::$ RETURN_CODE_SUCCESS ;
25+ return self ::RETURN_CODE_SUCCESS ;
2226 }
2327
24- $ oProperties = new \CssLint \Properties ();
25- if ($ oCliArgs ->options ) {
26- $ aOptions = json_decode ($ oCliArgs ->options , true );
28+ $ properties = new \CssLint \Properties ();
29+ if ($ cliArgs ->options !== null && $ cliArgs -> options !== '' && $ cliArgs -> options !== ' 0 ' ) {
30+ $ options = json_decode ($ cliArgs ->options , true );
2731
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 ;
32+ if (json_last_error () !== 0 ) {
33+ $ errorMessage = json_last_error_msg ();
34+ $ this ->printError ('Unable to parse option argument: ' . $ errorMessage );
35+ return self ::RETURN_CODE_ERROR ;
3236 }
3337
34- if (!$ aOptions ) {
38+ if (!$ options ) {
3539 $ this ->printError ('Unable to parse empty option argument ' );
36- return self ::$ RETURN_CODE_ERROR ;
40+ return self ::RETURN_CODE_ERROR ;
3741 }
38- $ oProperties ->setOptions ($ aOptions );
42+
43+ if (!is_array ($ options )) {
44+ $ this ->printError ('Unable to parse option argument: must be a json object ' );
45+ return self ::RETURN_CODE_ERROR ;
46+ }
47+
48+ $ properties ->setOptions ($ options );
3949 }
4050
41- $ oCssLinter = new \CssLint \Linter ($ oProperties );
51+ $ cssLinter = new \CssLint \Linter ($ properties );
4252
43- $ sFilePathOrCssString = $ oCliArgs ->filePathOrCssString ;
44- if (!file_exists ($ sFilePathOrCssString )) {
45- return $ this ->lintString ($ oCssLinter , $ sFilePathOrCssString );
53+ $ filePathOrCssString = $ cliArgs ->filePathOrCssString ;
54+ if (!file_exists ($ filePathOrCssString )) {
55+ return $ this ->lintString ($ cssLinter , $ filePathOrCssString );
4656 }
4757
48- $ sFilePath = $ sFilePathOrCssString ;
49- if (!is_readable ($ sFilePath )) {
50- $ this ->printError ('File " ' . $ sFilePath . '" is not readable ' );
51- return self ::$ RETURN_CODE_ERROR ;
58+ $ filePath = $ filePathOrCssString ;
59+ if (!is_readable ($ filePath )) {
60+ $ this ->printError ('File " ' . $ filePath . '" is not readable ' );
61+ return self ::RETURN_CODE_ERROR ;
5262 }
5363
54- return $ this ->lintFile ($ oCssLinter , $ sFilePath );
64+ return $ this ->lintFile ($ cssLinter , $ filePath );
5565 }
5666
5767 /**
5868 * Retrieve the parsed Cli arguments from given arguments array
69+ * @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
5970 * @return \CssLint\CliArgs an instance of Cli arguments object containing parsed arguments
6071 */
61- private function parseArguments (array $ aArguments ): \CssLint \CliArgs
72+ private function parseArguments (array $ arguments ): \CssLint \CliArgs
6273 {
63- return new \CssLint \CliArgs ($ aArguments );
74+ return new \CssLint \CliArgs ($ arguments );
6475 }
6576
6677 /**
6778 * Display usage of the cli
6879 */
69- private function printUsage ()
80+ private function printUsage (): void
7081 {
7182 $ this ->printLine ('Usage: ' . PHP_EOL .
7283 '------ ' . PHP_EOL .
7384 PHP_EOL .
74- ' ' . self ::$ SCRIPT_NAME . ' [--options= \ '{ } \ '] css_file_or_string_to_lint ' . PHP_EOL .
85+ ' ' . self ::SCRIPT_NAME . " [--options='{ }'] css_file_or_string_to_lint " . PHP_EOL .
7586 PHP_EOL .
7687 'Arguments: ' . PHP_EOL .
7788 '---------- ' . PHP_EOL .
@@ -95,83 +106,84 @@ private function printUsage()
95106 '--------- ' . PHP_EOL .
96107 PHP_EOL .
97108 ' Lint a CSS file: ' . PHP_EOL .
98- ' ' . self ::$ SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css ' . PHP_EOL . PHP_EOL .
109+ ' ' . self ::SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css ' . PHP_EOL . PHP_EOL .
99110 ' Lint a CSS string: ' . PHP_EOL .
100- ' ' . self ::$ SCRIPT_NAME . ' ".test { color: red; }" ' . PHP_EOL . PHP_EOL .
111+ ' ' . self ::SCRIPT_NAME . ' ".test { color: red; }" ' . PHP_EOL . PHP_EOL .
101112 ' Lint with only tabulation as indentation: ' . PHP_EOL .
102- ' ' . self ::$ SCRIPT_NAME .
113+ ' ' . self ::SCRIPT_NAME .
103114 ' --options= \'{ "allowedIndentationChars": ["\t"] } \' ".test { color: red; }" ' . PHP_EOL .
104115 PHP_EOL . PHP_EOL );
105116 }
106117
107118 /**
108119 * 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
120+ * @param \CssLint\Linter $cssLinter the instance of the linter
121+ * @param string $filePath the path of the file to be linted
111122 * @return int the return code related to the execution of the linter
112123 */
113- private function lintFile (\CssLint \Linter $ oCssLinter , string $ sFilePath ): int
124+ private function lintFile (\CssLint \Linter $ cssLinter , string $ filePath ): int
114125 {
115- $ this ->printLine ('# Lint CSS file " ' . $ sFilePath . '"... ' );
126+ $ this ->printLine ('# Lint CSS file " ' . $ filePath . '"... ' );
116127
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 ;
128+ if ($ cssLinter ->lintFile ($ filePath )) {
129+ $ this ->printLine ("\033[32m => CSS file \"" . $ filePath . "\" is valid \033[0m " . PHP_EOL );
130+ return self ::RETURN_CODE_SUCCESS ;
120131 }
121132
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 ;
133+ $ this ->printLine ("\033[31m => CSS file \"" . $ filePath . "\" is not valid: \033[0m " . PHP_EOL );
134+ $ this ->displayLinterErrors ($ cssLinter ->getErrors ());
135+ return self ::RETURN_CODE_ERROR ;
125136 }
126137
127138
128139 /**
129140 * 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
141+ * @param \CssLint\Linter $cssLinter the instance of the linter
142+ * @param string $stringValue the CSS string to be linted
132143 * @return int the return code related to the execution of the linter
133144 */
134- private function lintString (\CssLint \Linter $ oCssLinter , string $ sString ): int
145+ private function lintString (\CssLint \Linter $ cssLinter , string $ stringValue ): int
135146 {
136147 $ this ->printLine ('# Lint CSS string... ' );
137148
138- if ($ oCssLinter ->lintString ($ sString )) {
149+ if ($ cssLinter ->lintString ($ stringValue )) {
139150 $ this ->printLine ("\033[32m => CSS string is valid \033[0m " . PHP_EOL );
140- return self ::$ RETURN_CODE_SUCCESS ;
151+ return self ::RETURN_CODE_SUCCESS ;
141152 }
142153
143154 $ this ->printLine ("\033[31m => CSS string is not valid: \033[0m " . PHP_EOL );
144- $ this ->displayLinterErrors ($ oCssLinter ->getErrors ());
145- return self ::$ RETURN_CODE_ERROR ;
155+ $ this ->displayLinterErrors ($ cssLinter ->getErrors ());
156+ return self ::RETURN_CODE_ERROR ;
146157 }
147158
148159 /**
149160 * Display an error message
150- * @param string $sError the message to be displayed
161+ * @param string $error the message to be displayed
151162 */
152- private function printError (string $ sError )
163+ private function printError (string $ error ): void
153164 {
154- $ this ->printLine ("\033[31m/!\ Error: " . $ sError . "\033[0m " . PHP_EOL );
165+ $ this ->printLine ("\033[31m/!\ Error: " . $ error . "\033[0m " . PHP_EOL );
155166 }
156167
157168 /**
158169 * Display the errors returned by the linter
159- * @param array $aErrors the generated errors to be displayed
170+ * @param array $errors the generated errors to be displayed
160171 */
161- private function displayLinterErrors (array $ aErrors )
172+ private function displayLinterErrors (array $ errors ): void
162173 {
163- foreach ($ aErrors as $ sError ) {
164- $ this ->printLine ("\033[31m - " . $ sError . "\033[0m " );
174+ foreach ($ errors as $ error ) {
175+ $ this ->printLine ("\033[31m - " . $ error . "\033[0m " );
165176 }
177+
166178 $ this ->printLine ("" );
167179 }
168180
169181 /**
170182 * Display the given message in a new line
171- * @param string $sMessage the message to be displayed
183+ * @param string $message the message to be displayed
172184 */
173- private function printLine (string $ sMessage )
185+ private function printLine (string $ message ): void
174186 {
175- echo $ sMessage . PHP_EOL ;
187+ echo $ message . PHP_EOL ;
176188 }
177189}
0 commit comments