22/**
33 * Class to manage a list of sniffs to ignore.
44 *
5- * @author Brad Jorsch <brad.jorsch@automattic.com>
6- * @copyright 2023 Brad Jorsch
7- * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
5+ * @copyright 2025 PHPCSStandards and contributors
6+ * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
87 */
98
109namespace PHP_CodeSniffer \Util ;
1110
12- class IgnoreList
11+ /**
12+ * Class to manage a list of sniffs to ignore.
13+ *
14+ * ---------------------------------------------------------------------------------------------
15+ * This class is intended for internal use only and is not part of the public API.
16+ * This also means that it has no promise of backward compatibility. Use at your own risk.
17+ * ---------------------------------------------------------------------------------------------
18+ *
19+ * @internal
20+ */
21+ final class IgnoreList
1322{
1423
1524 /**
@@ -19,35 +28,55 @@ class IgnoreList
1928 * Each level may be a boolean indicating that everything underneath the branch is or is not ignored, or
2029 * may have a `.default' key indicating the default status for any branches not in the tree.
2130 *
22- * @var array|boolean
31+ * @var array<string, bool|array<string, bool|array<string, bool|array<string, bool>>>>
2332 */
2433 private $ data = [ '.default ' => false ];
2534
2635
2736 /**
2837 * Get an instance set to ignore nothing.
2938 *
30- * @return static
39+ * @return IgnoreList
3140 */
32- public static function ignoringNone ()
41+ public static function getInstanceIgnoringNothing ()
3342 {
3443 return new self ();
3544
36- }//end ignoringNone ()
45+ }//end getInstanceIgnoringNothing ()
3746
3847
3948 /**
4049 * Get an instance set to ignore everything.
4150 *
42- * @return static
51+ * @return IgnoreList
52+ */
53+ public static function getInstanceIgnoringAll ()
54+ {
55+ $ instance = new self ();
56+ $ instance ->data ['.default ' ] = true ;
57+ return $ instance ;
58+
59+ }//end getInstanceIgnoringAll()
60+
61+
62+ /**
63+ * Get a new instance based on an existing instance.
64+ *
65+ * If passed null, creates a new instance that ignores nothing.
66+ *
67+ * @param IgnoreList|null $ignoreList List to clone.
68+ *
69+ * @return IgnoreList
4370 */
44- public static function ignoringAll ( )
71+ public static function getNewInstanceFrom (? IgnoreList $ ignoreList )
4572 {
46- $ ret = new self ();
47- $ ret ->data ['.default ' ] = true ;
48- return $ ret ;
73+ if ($ ignoreList === null ) {
74+ return self ::getInstanceIgnoringNothing ();
75+ }
76+
77+ return clone $ ignoreList ;
4978
50- }//end ignoringAll ()
79+ }//end getNewInstanceFrom ()
5180
5281
5382 /**
@@ -57,29 +86,29 @@ public static function ignoringAll()
5786 *
5887 * @return bool
5988 */
60- public function check ($ code )
89+ public function isIgnored ($ code )
6190 {
62- $ data = $ this ->data ;
63- $ ret = $ data ['.default ' ];
91+ $ data = $ this ->data ;
92+ $ returnValue = $ data ['.default ' ];
6493 foreach (explode ('. ' , $ code ) as $ part ) {
6594 if (isset ($ data [$ part ]) === false ) {
6695 break ;
6796 }
6897
6998 $ data = $ data [$ part ];
7099 if (is_bool ($ data ) === true ) {
71- $ ret = $ data ;
100+ $ returnValue = $ data ;
72101 break ;
73102 }
74103
75104 if (isset ($ data ['.default ' ]) === true ) {
76- $ ret = $ data ['.default ' ];
105+ $ returnValue = $ data ['.default ' ];
77106 }
78107 }
79108
80- return $ ret ;
109+ return $ returnValue ;
81110
82- }//end check ()
111+ }//end isIgnored ()
83112
84113
85114 /**
@@ -88,7 +117,7 @@ public function check($code)
88117 * @param string $code Partial or complete sniff code.
89118 * @param bool $ignore Whether the specified sniff should be ignored.
90119 *
91- * @return this
120+ * @return IgnoreList $ this for chaining.
92121 */
93122 public function set ($ code , $ ignore )
94123 {
@@ -114,55 +143,55 @@ public function set($code, $ignore)
114143
115144
116145 /**
117- * Check if the list is empty .
146+ * Check if the list ignores nothing .
118147 *
119148 * @return bool
120149 */
121- public function isEmpty ()
150+ public function ignoresNothing ()
122151 {
123- $ arrs = [ $ this ->data ];
124- while ($ arrs !== []) {
125- $ arr = array_pop ($ arrs );
126- foreach ($ arr as $ v ) {
127- if ($ v === true ) {
152+ $ arraysToProcess = [ $ this ->data ];
153+ while ($ arraysToProcess !== []) {
154+ $ arrayBeingProcessed = array_pop ($ arraysToProcess );
155+ foreach ($ arrayBeingProcessed as $ valueBeingProcessed ) {
156+ if ($ valueBeingProcessed === true ) {
128157 return false ;
129158 }
130159
131- if (is_array ($ v ) === true ) {
132- $ arrs [] = $ v ;
160+ if (is_array ($ valueBeingProcessed ) === true ) {
161+ $ arraysToProcess [] = $ valueBeingProcessed ;
133162 }
134163 }
135164 }
136165
137166 return true ;
138167
139- }//end isEmpty ()
168+ }//end ignoresNothing ()
140169
141170
142171 /**
143172 * Check if the list ignores everything.
144173 *
145174 * @return bool
146175 */
147- public function isAll ()
176+ public function ignoresEverything ()
148177 {
149- $ arrs = [ $ this ->data ];
150- while ($ arrs !== []) {
151- $ arr = array_pop ($ arrs );
152- foreach ($ arr as $ v ) {
153- if ($ v === false ) {
178+ $ arraysToProcess = [ $ this ->data ];
179+ while ($ arraysToProcess !== []) {
180+ $ arrayBeingProcessed = array_pop ($ arraysToProcess );
181+ foreach ($ arrayBeingProcessed as $ valueBeingProcessed ) {
182+ if ($ valueBeingProcessed === false ) {
154183 return false ;
155184 }
156185
157- if (is_array ($ v ) === true ) {
158- $ arrs [] = $ v ;
186+ if (is_array ($ valueBeingProcessed ) === true ) {
187+ $ arraysToProcess [] = $ valueBeingProcessed ;
159188 }
160189 }
161190 }
162191
163192 return true ;
164193
165- }//end isAll ()
194+ }//end ignoresEverything ()
166195
167196
168197}//end class
0 commit comments