Conditions | 74 |
Paths | 74 |
Total Lines | 147 |
Code Lines | 113 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | public function elementMatches($pseudoclass, $node, $scope, $value = NULL) |
||
45 | { |
||
46 | $name = strtolower($pseudoclass); |
||
47 | // Need to handle known pseudoclasses. |
||
48 | switch ($name) { |
||
49 | case 'current': |
||
50 | case 'past': |
||
51 | case 'future': |
||
52 | case 'visited': |
||
53 | case 'hover': |
||
54 | case 'active': |
||
55 | case 'focus': |
||
56 | case 'animated': // Last 3 are from jQuery |
||
57 | case 'visible': |
||
58 | case 'hidden': |
||
59 | // These require a UA, which we don't have. |
||
60 | case 'valid': |
||
61 | case 'invalid': |
||
62 | case 'required': |
||
63 | case 'optional': |
||
64 | case 'read-only': |
||
65 | case 'read-write': |
||
66 | // Since we don't know how to validate elements, |
||
67 | // we can't supply these. |
||
68 | case 'dir': |
||
69 | // FIXME: I don't know how to get directionality info. |
||
70 | case 'nth-column': |
||
71 | case 'nth-last-column': |
||
72 | // We don't know what a column is in most documents. |
||
73 | // FIXME: Can we do this for HTML? |
||
74 | case 'target': |
||
75 | // This requires a location URL, which we don't have. |
||
76 | return false; |
||
77 | case 'indeterminate': |
||
78 | // Because sometimes screwing with people is fun. |
||
79 | return (boolean)mt_rand(0, 1); |
||
80 | case 'lang': |
||
81 | // No value = exception. |
||
82 | if (!isset($value)) { |
||
83 | throw new NotImplementedException(':lang() requires a value.'); |
||
84 | } |
||
85 | |||
86 | return $this->lang($node, $value); |
||
87 | case 'any-link': |
||
88 | return Util::matchesAttribute($node, 'href') |
||
89 | || Util::matchesAttribute($node, 'src') |
||
90 | || Util::matchesAttribute($node, 'link'); |
||
91 | case 'link': |
||
92 | return Util::matchesAttribute($node, 'href'); |
||
93 | case 'local-link': |
||
94 | return $this->isLocalLink($node); |
||
95 | case 'root': |
||
96 | return $node->isSameNode($node->ownerDocument->documentElement); |
||
97 | |||
98 | // CSS 4 declares the :scope pseudo-class, which describes what was |
||
99 | // the :x-root QueryPath extension. |
||
100 | case 'x-root': |
||
101 | case 'x-reset': |
||
102 | case 'scope': |
||
103 | return $node->isSameNode($scope); |
||
104 | // NON-STANDARD extensions for simple support of even and odd. These |
||
105 | // are supported by jQuery, FF, and other user agents. |
||
106 | case 'even': |
||
107 | return $this->isNthChild($node, 'even'); |
||
108 | case 'odd': |
||
109 | return $this->isNthChild($node, 'odd'); |
||
110 | case 'nth-child': |
||
111 | return $this->isNthChild($node, $value); |
||
112 | case 'nth-last-child': |
||
113 | return $this->isNthChild($node, $value, true); |
||
114 | case 'nth-of-type': |
||
115 | return $this->isNthChild($node, $value, false, true); |
||
116 | case 'nth-last-of-type': |
||
117 | return $this->isNthChild($node, $value, true, true); |
||
118 | case 'first-of-type': |
||
119 | return $this->isFirstOfType($node); |
||
120 | case 'last-of-type': |
||
121 | return $this->isLastOfType($node); |
||
122 | case 'only-of-type': |
||
123 | return $this->isFirstOfType($node) && $this->isLastOfType($node); |
||
124 | |||
125 | // Additional pseudo-classes defined in jQuery: |
||
126 | case 'lt': |
||
127 | // I'm treating this as "less than or equal to". |
||
128 | $rule = sprintf('-n + %d', (int)$value); |
||
129 | |||
130 | // $rule = '-n+15'; |
||
131 | return $this->isNthChild($node, $rule); |
||
132 | case 'gt': |
||
133 | // I'm treating this as "greater than" |
||
134 | // return $this->nodePositionFromEnd($node) > (int) $value; |
||
135 | return $this->nodePositionFromStart($node) > (int)$value; |
||
136 | case 'nth': |
||
137 | case 'eq': |
||
138 | $rule = (int)$value; |
||
139 | |||
140 | return $this->isNthChild($node, $rule); |
||
141 | case 'first': |
||
142 | return $this->isNthChild($node, 1); |
||
143 | case 'first-child': |
||
144 | return $this->isFirst($node); |
||
145 | case 'last': |
||
146 | case 'last-child': |
||
147 | return $this->isLast($node); |
||
148 | case 'only-child': |
||
149 | return $this->isFirst($node) && $this->isLast($node); |
||
150 | case 'empty': |
||
151 | return $this->isEmpty($node); |
||
152 | case 'parent': |
||
153 | return !$this->isEmpty($node); |
||
154 | |||
155 | case 'enabled': |
||
156 | case 'disabled': |
||
157 | case 'checked': |
||
158 | return Util::matchesAttribute($node, $name); |
||
159 | case 'text': |
||
160 | case 'radio': |
||
161 | case 'checkbox': |
||
162 | case 'file': |
||
163 | case 'password': |
||
164 | case 'submit': |
||
165 | case 'image': |
||
166 | case 'reset': |
||
167 | case 'button': |
||
168 | return Util::matchesAttribute($node, 'type', $name); |
||
169 | |||
170 | case 'header': |
||
171 | return $this->header($node); |
||
172 | case 'has': |
||
173 | case 'matches': |
||
174 | return $this->has($node, $value); |
||
175 | break; |
||
|
|||
176 | case 'not': |
||
177 | if (empty($value)) { |
||
178 | throw new ParseException(':not() requires a value.'); |
||
179 | } |
||
180 | |||
181 | return $this->isNot($node, $value); |
||
182 | // Contains == text matches. |
||
183 | // In QP 2.1, this was changed. |
||
184 | case 'contains': |
||
185 | return $this->contains($node, $value); |
||
186 | // Since QP 2.1 |
||
187 | case 'contains-exactly': |
||
188 | return $this->containsExactly($node, $value); |
||
189 | default: |
||
190 | throw new ParseException('Unknown Pseudo-Class: ' . $name); |
||
191 | } |
||
477 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.