Skip to content

Commit e3f58eb

Browse files
authored
Merge pull request #216 from PHPCSStandards/feature/file-getconditions-add-tests
File::[get|has]Conditions(): add tests
2 parents 8e61fdc + 842b42b commit e3f58eb

File tree

3 files changed

+587
-2
lines changed

3 files changed

+587
-2
lines changed

src/Files/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ public function hasCondition($stackPtr, $types)
27182718
}
27192719

27202720
// Make sure the token has conditions.
2721-
if (isset($this->tokens[$stackPtr]['conditions']) === false) {
2721+
if (empty($this->tokens[$stackPtr]['conditions']) === true) {
27222722
return false;
27232723
}
27242724

@@ -2759,7 +2759,7 @@ public function getCondition($stackPtr, $type, $first=true)
27592759
}
27602760

27612761
// Make sure the token has conditions.
2762-
if (isset($this->tokens[$stackPtr]['conditions']) === false) {
2762+
if (empty($this->tokens[$stackPtr]['conditions']) === true) {
27632763
return false;
27642764
}
27652765

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/* testStartPoint */
4+
/* condition 0: namespace */
5+
namespace Conditions\HorribleCode {
6+
7+
/* condition 1: if */
8+
if (!function_exists('letsGetSerious') ) {
9+
10+
/* condition 2: function */
11+
function letsGetSerious() {
12+
13+
/* condition 3-1: if */
14+
if (isset($loadthis)) {
15+
doing_something();
16+
/* condition 3-2: else */
17+
} else {
18+
19+
/* condition 4: if */
20+
if (!class_exists('SeriouslyNestedClass')) {
21+
22+
/* condition 5: nested class */
23+
class SeriouslyNestedClass extends SomeOtherClass {
24+
25+
/* condition 6: class method */
26+
public function SeriouslyNestedMethod(/* testSeriouslyNestedMethod */ $param) {
27+
28+
/* condition 7: switch */
29+
switch ($param) {
30+
31+
/* condition 8a: case */
32+
case 'testing':
33+
34+
/* condition 9: while */
35+
while ($a < 10 ) {
36+
37+
/* condition 10-1: if */
38+
if ($a === $b) {
39+
40+
/* condition 11-1: nested anonymous class */
41+
return new class() {
42+
43+
/* condition 12: nested anonymous class method */
44+
private function DidSomeoneSayNesting() {
45+
46+
/* condition 13: closure */
47+
$c = function() {
48+
/* testDeepestNested */
49+
return 'closure';
50+
};
51+
}
52+
};
53+
/* condition 10-2: elseif */
54+
} elseif($bool) {
55+
echo 'hello world';
56+
}
57+
58+
/* condition 10-3: foreach */
59+
foreach ($array as $k => $v) {
60+
61+
/* condition 11-2: try */
62+
try {
63+
--$k;
64+
65+
/* condition 11-3: catch */
66+
} catch (Exception $e) {
67+
/* testInException */
68+
echo 'oh darn';
69+
/* condition 11-4: finally */
70+
} finally {
71+
return true;
72+
}
73+
}
74+
75+
$a++;
76+
}
77+
break;
78+
79+
/* condition 8b: default */
80+
default:
81+
/* testInDefault */
82+
$return = 'nada';
83+
return $return;
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)