13
13
14
14
use PHP_CodeSniffer \Exceptions \RuntimeException ;
15
15
use PHP_CodeSniffer \Util ;
16
+ use stdClass ;
16
17
17
18
class Ruleset
18
19
{
@@ -960,6 +961,11 @@ private function processRule($rule, $newSniffs, $depth=0)
960
961
if (isset ($ rule ->properties ) === true
961
962
&& $ this ->shouldProcessElement ($ rule ->properties ) === true
962
963
) {
964
+ $ propertyScope = 'standard ' ;
965
+ if ($ code === $ ref || substr ($ ref , -9 ) === 'Sniff.php ' ) {
966
+ $ propertyScope = 'sniff ' ;
967
+ }
968
+
963
969
foreach ($ rule ->properties ->property as $ prop ) {
964
970
if ($ this ->shouldProcessElement ($ prop ) === false ) {
965
971
continue ;
@@ -980,9 +986,9 @@ private function processRule($rule, $newSniffs, $depth=0)
980
986
$ values = [];
981
987
if (isset ($ prop ['extend ' ]) === true
982
988
&& (string ) $ prop ['extend ' ] === 'true '
983
- && isset ($ this ->ruleset [$ code ]['properties ' ][$ name ]) === true
989
+ && isset ($ this ->ruleset [$ code ]['properties ' ][$ name ][ ' value ' ] ) === true
984
990
) {
985
- $ values = $ this ->ruleset [$ code ]['properties ' ][$ name ];
991
+ $ values = $ this ->ruleset [$ code ]['properties ' ][$ name ][ ' value ' ] ;
986
992
}
987
993
988
994
if (isset ($ prop ->element ) === true ) {
@@ -1017,7 +1023,10 @@ private function processRule($rule, $newSniffs, $depth=0)
1017
1023
}
1018
1024
}//end if
1019
1025
1020
- $ this ->ruleset [$ code ]['properties ' ][$ name ] = $ values ;
1026
+ $ this ->ruleset [$ code ]['properties ' ][$ name ] = [
1027
+ 'value ' => $ values ,
1028
+ 'scope ' => $ propertyScope ,
1029
+ ];
1021
1030
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
1022
1031
echo str_repeat ("\t" , $ depth );
1023
1032
echo "\t\t=> array property \"$ name \" set to \"$ printValue \"" ;
@@ -1028,7 +1037,10 @@ private function processRule($rule, $newSniffs, $depth=0)
1028
1037
echo PHP_EOL ;
1029
1038
}
1030
1039
} else {
1031
- $ this ->ruleset [$ code ]['properties ' ][$ name ] = (string ) $ prop ['value ' ];
1040
+ $ this ->ruleset [$ code ]['properties ' ][$ name ] = [
1041
+ 'value ' => (string ) $ prop ['value ' ],
1042
+ 'scope ' => $ propertyScope ,
1043
+ ];
1032
1044
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
1033
1045
echo str_repeat ("\t" , $ depth );
1034
1046
echo "\t\t=> property \"$ name \" set to \"" .(string ) $ prop ['value ' ].'" ' ;
@@ -1218,8 +1230,8 @@ public function populateTokenListeners()
1218
1230
1219
1231
// Set custom properties.
1220
1232
if (isset ($ this ->ruleset [$ sniffCode ]['properties ' ]) === true ) {
1221
- foreach ($ this ->ruleset [$ sniffCode ]['properties ' ] as $ name => $ value ) {
1222
- $ this ->setSniffProperty ($ sniffClass , $ name , $ value );
1233
+ foreach ($ this ->ruleset [$ sniffCode ]['properties ' ] as $ name => $ settings ) {
1234
+ $ this ->setSniffProperty ($ sniffClass , $ name , $ settings );
1223
1235
}
1224
1236
}
1225
1237
@@ -1286,18 +1298,76 @@ public function populateTokenListeners()
1286
1298
*
1287
1299
* @param string $sniffClass The class name of the sniff.
1288
1300
* @param string $name The name of the property to change.
1289
- * @param string $value The new value of the property.
1301
+ * @param array $settings Array with the new value of the property and the scope of the property being set .
1290
1302
*
1291
1303
* @return void
1304
+ *
1305
+ * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When attempting to set a non-existent property on a sniff
1306
+ * which doesn't declare the property or explicitly supports
1307
+ * dynamic properties.
1292
1308
*/
1293
- public function setSniffProperty ($ sniffClass , $ name , $ value )
1309
+ public function setSniffProperty ($ sniffClass , $ name , $ settings )
1294
1310
{
1295
1311
// Setting a property for a sniff we are not using.
1296
1312
if (isset ($ this ->sniffs [$ sniffClass ]) === false ) {
1297
1313
return ;
1298
1314
}
1299
1315
1300
- $ name = trim ($ name );
1316
+ $ name = trim ($ name );
1317
+ $ propertyName = $ name ;
1318
+ if (substr ($ propertyName , -2 ) === '[] ' ) {
1319
+ $ propertyName = substr ($ propertyName , 0 , -2 );
1320
+ }
1321
+
1322
+ /*
1323
+ * BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format.
1324
+ *
1325
+ * Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_
1326
+ * for the property (which could be an array).
1327
+ * Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope'
1328
+ * and 'value', where 'scope' indicates whether the property should be set to the given 'value'
1329
+ * for one individual sniff or for all sniffs in a standard.
1330
+ *
1331
+ * This BC-layer is only for integrations with PHPCS which may call this method directly
1332
+ * and will be removed in PHPCS 4.0.0.
1333
+ */
1334
+
1335
+ if (is_array ($ settings ) === false
1336
+ || isset ($ settings ['scope ' ], $ settings ['value ' ]) === false
1337
+ ) {
1338
+ // This will be an "old" format value.
1339
+ $ settings = [
1340
+ 'value ' => $ settings ,
1341
+ 'scope ' => 'standard ' ,
1342
+ ];
1343
+
1344
+ trigger_error (
1345
+ __FUNCTION__ .': the format of the $settings parameter has changed from (mixed) $value to array( \'scope \' => \'sniff|standard \', \'value \' => $value). Please update your integration code. See PR #3629 for more information. ' ,
1346
+ E_USER_DEPRECATED
1347
+ );
1348
+ }
1349
+
1350
+ $ isSettable = false ;
1351
+ $ sniffObject = $ this ->sniffs [$ sniffClass ];
1352
+ if (property_exists ($ sniffObject , $ propertyName ) === true
1353
+ || ($ sniffObject instanceof stdClass) === true
1354
+ || method_exists ($ sniffObject , '__set ' ) === true
1355
+ ) {
1356
+ $ isSettable = true ;
1357
+ }
1358
+
1359
+ if ($ isSettable === false ) {
1360
+ if ($ settings ['scope ' ] === 'sniff ' ) {
1361
+ $ notice = "Ruleset invalid. Property \"$ propertyName \" does not exist on sniff " ;
1362
+ $ notice .= array_search ($ sniffClass , $ this ->sniffCodes , true );
1363
+ throw new RuntimeException ($ notice );
1364
+ }
1365
+
1366
+ return ;
1367
+ }
1368
+
1369
+ $ value = $ settings ['value ' ];
1370
+
1301
1371
if (is_string ($ value ) === true ) {
1302
1372
$ value = trim ($ value );
1303
1373
}
@@ -1312,7 +1382,7 @@ public function setSniffProperty($sniffClass, $name, $value)
1312
1382
} else if ($ value === 'false ' ) {
1313
1383
$ value = false ;
1314
1384
} else if (substr ($ name , -2 ) === '[] ' ) {
1315
- $ name = substr ( $ name , 0 , - 2 ) ;
1385
+ $ name = $ propertyName ;
1316
1386
$ values = [];
1317
1387
if ($ value !== null ) {
1318
1388
foreach (explode (', ' , $ value ) as $ val ) {
@@ -1328,7 +1398,7 @@ public function setSniffProperty($sniffClass, $name, $value)
1328
1398
$ value = $ values ;
1329
1399
}
1330
1400
1331
- $ this -> sniffs [ $ sniffClass ] ->$ name = $ value ;
1401
+ $ sniffObject ->$ name = $ value ;
1332
1402
1333
1403
}//end setSniffProperty()
1334
1404
0 commit comments