| 1 |
1
|
mapi |
<?php |
|
| 2 |
↓
|
mapi |
/** |
|
| 3 |
↓
|
mapi |
* This file is part of PHP_PMD. |
|
| 4 |
↓
|
mapi |
* |
|
| 5 |
↓
|
mapi |
* PHP Version 5 |
|
| 6 |
↓
|
mapi |
* |
|
| 7 |
174
|
mapi |
* Copyright (c) 2009-2010, Manuel Pichler <mapi@phpmd.org>. |
|
| 8 |
1
|
mapi |
* All rights reserved. |
|
| 9 |
↓
|
mapi |
* |
|
| 10 |
↓
|
mapi |
* Redistribution and use in source and binary forms, with or without |
|
| 11 |
↓
|
mapi |
* modification, are permitted provided that the following conditions |
|
| 12 |
↓
|
mapi |
* are met: |
|
| 13 |
↓
|
mapi |
* |
|
| 14 |
↓
|
mapi |
* * Redistributions of source code must retain the above copyright |
|
| 15 |
↓
|
mapi |
* notice, this list of conditions and the following disclaimer. |
|
| 16 |
↓
|
mapi |
* |
|
| 17 |
↓
|
mapi |
* * Redistributions in binary form must reproduce the above copyright |
|
| 18 |
↓
|
mapi |
* notice, this list of conditions and the following disclaimer in |
|
| 19 |
↓
|
mapi |
* the documentation and/or other materials provided with the |
|
| 20 |
↓
|
mapi |
* distribution. |
|
| 21 |
↓
|
mapi |
* |
|
| 22 |
↓
|
mapi |
* * Neither the name of Manuel Pichler nor the names of his |
|
| 23 |
↓
|
mapi |
* contributors may be used to endorse or promote products derived |
|
| 24 |
↓
|
mapi |
* from this software without specific prior written permission. |
|
| 25 |
↓
|
mapi |
* |
|
| 26 |
↓
|
mapi |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
| 27 |
↓
|
mapi |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
| 28 |
↓
|
mapi |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
| 29 |
↓
|
mapi |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
| 30 |
↓
|
mapi |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
| 31 |
↓
|
mapi |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
| 32 |
↓
|
mapi |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
| 33 |
↓
|
mapi |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
| 34 |
↓
|
mapi |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
| 35 |
↓
|
mapi |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
| 36 |
↓
|
mapi |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
| 37 |
↓
|
mapi |
* POSSIBILITY OF SUCH DAMAGE. |
|
| 38 |
↓
|
mapi |
* |
|
| 39 |
37
|
mapi |
* @category PHP |
|
| 40 |
↓
|
mapi |
* @package PHP_PMD |
|
| 41 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 42 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 43 |
37
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 44 |
↓
|
mapi |
* @version SVN: $Id$ |
|
| 45 |
174
|
mapi |
* @link http://phpmd.org |
|
| 46 |
1
|
mapi |
*/ |
|
| 47 |
↓
|
mapi |
|
|
| 48 |
↓
|
mapi |
require_once 'PHP/PMD/AbstractRule.php'; |
|
| 49 |
↓
|
mapi |
require_once 'PHP/PMD/RuleSet.php'; |
|
| 50 |
7
|
mapi |
require_once 'PHP/PMD/RuleClassFileNotFoundException.php'; |
|
| 51 |
↓
|
mapi |
require_once 'PHP/PMD/RuleClassNotFoundException.php'; |
|
| 52 |
1
|
mapi |
require_once 'PHP/PMD/RuleSetNotFoundException.php'; |
|
| 53 |
↓
|
mapi |
|
|
| 54 |
↓
|
mapi |
/** |
|
| 55 |
↓
|
mapi |
* This factory class is used to create the {@link PHP_PMD_RuleSet} instance |
|
| 56 |
↓
|
mapi |
* that PHP_PMD will use to analyze the source code. |
|
| 57 |
↓
|
mapi |
* |
|
| 58 |
37
|
mapi |
* @category PHP |
|
| 59 |
↓
|
mapi |
* @package PHP_PMD |
|
| 60 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 61 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 62 |
37
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 63 |
↓
|
mapi |
* @version Release: @package_version@ |
|
| 64 |
174
|
mapi |
* @link http://phpmd.org |
|
| 65 |
1
|
mapi |
*/ |
|
| 66 |
↓
|
mapi |
class PHP_PMD_RuleSetFactory |
|
| 67 |
↓
|
mapi |
{ |
|
| 68 |
76
|
mapi |
/** |
|
| 69 |
↓
|
mapi |
* The data directory set by PEAR or a dynamic property set within the class |
|
| 70 |
↓
|
mapi |
* constructor. |
|
| 71 |
↓
|
mapi |
* |
|
| 72 |
↓
|
mapi |
* @var string |
|
| 73 |
↓
|
mapi |
*/ |
|
| 74 |
1
|
mapi |
private $_location = '@data_dir@'; |
|
| 75 |
↓
|
mapi |
|
|
| 76 |
↓
|
mapi |
/** |
|
| 77 |
↓
|
mapi |
* The minimum priority for rules to load. |
|
| 78 |
↓
|
mapi |
* |
|
| 79 |
76
|
mapi |
* @var integer |
|
| 80 |
1
|
mapi |
*/ |
|
| 81 |
↓
|
mapi |
private $_minimumPriority = PHP_PMD_AbstractRule::LOWEST_PRIORITY; |
|
| 82 |
↓
|
mapi |
|
|
| 83 |
37
|
mapi |
/** |
|
| 84 |
↓
|
mapi |
* Constructs a new default rule-set factory instance. |
|
| 85 |
↓
|
mapi |
*/ |
|
| 86 |
1
|
mapi |
public function __construct() |
|
| 87 |
↓
|
mapi |
{ |
|
| 88 |
37
|
mapi |
// PEAR installer workaround |
|
| 89 |
1
|
mapi |
if (strpos($this->_location, '@data_dir') === 0) { |
|
| 90 |
94
|
mapi |
$this->_location = dirname(__FILE__) . '/../../../'; |
|
| 91 |
106
|
mapi |
} else { |
|
| 92 |
↓
|
mapi |
$this->_location .= '/PHP_PMD'; |
|
| 93 |
1
|
mapi |
} |
|
| 94 |
↓
|
mapi |
} |
|
| 95 |
↓
|
mapi |
|
|
| 96 |
↓
|
mapi |
/** |
|
| 97 |
↓
|
mapi |
* Sets the minimum priority that a rule must have. |
|
| 98 |
↓
|
mapi |
* |
|
| 99 |
↓
|
mapi |
* @param integer $minimumPriority The minimum priority value. |
|
| 100 |
↓
|
mapi |
* |
|
| 101 |
↓
|
mapi |
* @return void |
|
| 102 |
↓
|
mapi |
*/ |
|
| 103 |
↓
|
mapi |
public function setMinimumPriority($minimumPriority) |
|
| 104 |
↓
|
mapi |
{ |
|
| 105 |
↓
|
mapi |
$this->_minimumPriority = $minimumPriority; |
|
| 106 |
↓
|
mapi |
} |
|
| 107 |
↓
|
mapi |
|
|
| 108 |
37
|
mapi |
/** |
|
| 109 |
↓
|
mapi |
* Creates an array of rule-set instances for the given argument. |
|
| 110 |
↓
|
mapi |
* |
|
| 111 |
↓
|
mapi |
* @param string $ruleSetFileNames Comma-separated string of rule-set filenames |
|
| 112 |
↓
|
mapi |
* or identifier. |
|
| 113 |
↓
|
mapi |
* |
|
| 114 |
↓
|
mapi |
* @return array(PHP_PMD_RuleSet) |
|
| 115 |
↓
|
mapi |
*/ |
|
| 116 |
1
|
mapi |
public function createRuleSets($ruleSetFileNames) |
|
| 117 |
↓
|
mapi |
{ |
|
| 118 |
↓
|
mapi |
$ruleSets = array(); |
|
| 119 |
↓
|
mapi |
|
|
| 120 |
↓
|
mapi |
$ruleSetFileName = strtok($ruleSetFileNames, ','); |
|
| 121 |
↓
|
mapi |
while ($ruleSetFileName !== false) { |
|
| 122 |
↓
|
mapi |
$ruleSets[] = $this->createSingleRuleSet($ruleSetFileName); |
|
| 123 |
↓
|
mapi |
|
|
| 124 |
↓
|
mapi |
$ruleSetFileName = strtok(','); |
|
| 125 |
↓
|
mapi |
} |
|
| 126 |
↓
|
mapi |
return $ruleSets; |
|
| 127 |
↓
|
mapi |
} |
|
| 128 |
↓
|
mapi |
|
|
| 129 |
37
|
mapi |
/** |
|
| 130 |
↓
|
mapi |
* Creates a single rule-set instance for the given filename or identifier. |
|
| 131 |
↓
|
mapi |
* |
|
| 132 |
↓
|
mapi |
* @param string $ruleSetOrFileName The rule-set filename or identifier. |
|
| 133 |
↓
|
mapi |
* |
|
| 134 |
↓
|
mapi |
* @return PHP_PMD_RuleSet |
|
| 135 |
↓
|
mapi |
*/ |
|
| 136 |
1
|
mapi |
public function createSingleRuleSet($ruleSetOrFileName) |
|
| 137 |
↓
|
mapi |
{ |
|
| 138 |
↓
|
mapi |
$fileName = $this->_createRuleSetFileName($ruleSetOrFileName); |
|
| 139 |
↓
|
mapi |
return $this->_parseRuleSetNode($fileName); |
|
| 140 |
↓
|
mapi |
} |
|
| 141 |
↓
|
mapi |
|
|
| 142 |
37
|
mapi |
/** |
|
| 143 |
↓
|
mapi |
* This method creates the filename for a rule-set identifier or it returns |
|
| 144 |
↓
|
mapi |
* the input when it is already a filename. |
|
| 145 |
↓
|
mapi |
* |
|
| 146 |
↓
|
mapi |
* @param string $ruleSetOrFileName The rule-set filename or identifier. |
|
| 147 |
↓
|
mapi |
* |
|
| 148 |
↓
|
mapi |
* @return string |
|
| 149 |
↓
|
mapi |
*/ |
|
| 150 |
1
|
mapi |
private function _createRuleSetFileName($ruleSetOrFileName) |
|
| 151 |
↓
|
mapi |
{ |
|
| 152 |
↓
|
mapi |
if (file_exists($ruleSetOrFileName) === true) { |
|
| 153 |
↓
|
mapi |
return $ruleSetOrFileName; |
|
| 154 |
↓
|
mapi |
} |
|
| 155 |
↓
|
mapi |
|
|
| 156 |
↓
|
mapi |
$fileName = $this->_location . '/' . $ruleSetOrFileName; |
|
| 157 |
↓
|
mapi |
if (file_exists($fileName) === true) { |
|
| 158 |
↓
|
mapi |
return $fileName; |
|
| 159 |
↓
|
mapi |
} |
|
| 160 |
↓
|
mapi |
|
|
| 161 |
↓
|
mapi |
$fileName = $this->_location . '/rulesets/' . $ruleSetOrFileName . '.xml'; |
|
| 162 |
↓
|
mapi |
if (file_exists($fileName) === true) { |
|
| 163 |
↓
|
mapi |
return $fileName; |
|
| 164 |
↓
|
mapi |
} |
|
| 165 |
↓
|
mapi |
|
|
| 166 |
↓
|
mapi |
$fileName = getcwd() . '/rulesets/' . $ruleSetOrFileName . '.xml'; |
|
| 167 |
↓
|
mapi |
if (file_exists($fileName) === true) { |
|
| 168 |
↓
|
mapi |
return $fileName; |
|
| 169 |
↓
|
mapi |
} |
|
| 170 |
↓
|
mapi |
|
|
| 171 |
↓
|
mapi |
throw new PHP_PMD_RuleSetNotFoundException($ruleSetOrFileName); |
|
| 172 |
↓
|
mapi |
} |
|
| 173 |
↓
|
mapi |
|
|
| 174 |
37
|
mapi |
/** |
|
| 175 |
↓
|
mapi |
* This method parses the rule-set definition in the given file. |
|
| 176 |
↓
|
mapi |
* |
|
| 177 |
↓
|
mapi |
* @param string $fileName The filename of a rule-set definition. |
|
| 178 |
↓
|
mapi |
* |
|
| 179 |
↓
|
mapi |
* @return PHP_PMD_RuleSet |
|
| 180 |
↓
|
mapi |
*/ |
|
| 181 |
1
|
mapi |
private function _parseRuleSetNode($fileName) |
|
| 182 |
↓
|
mapi |
{ |
|
| 183 |
↓
|
mapi |
// Hide error messages |
|
| 184 |
↓
|
mapi |
$libxml = libxml_use_internal_errors(true); |
|
| 185 |
↓
|
mapi |
|
|
| 186 |
5
|
mapi |
$xml = simplexml_load_file($fileName); |
|
| 187 |
↓
|
mapi |
if ($xml === false) { |
|
| 188 |
1
|
mapi |
// Reset error handling to previous setting |
|
| 189 |
↓
|
mapi |
libxml_use_internal_errors($libxml); |
|
| 190 |
↓
|
mapi |
|
|
| 191 |
↓
|
mapi |
throw new RuntimeException(trim(libxml_get_last_error()->message)); |
|
| 192 |
↓
|
mapi |
} |
|
| 193 |
↓
|
mapi |
|
|
| 194 |
↓
|
mapi |
$ruleSet = new PHP_PMD_RuleSet(); |
|
| 195 |
↓
|
mapi |
$ruleSet->setFileName($fileName); |
|
| 196 |
↓
|
mapi |
$ruleSet->setName((string) $xml['name']); |
|
| 197 |
↓
|
mapi |
|
|
| 198 |
↓
|
mapi |
foreach ($xml->children() as $node) { |
|
| 199 |
↓
|
mapi |
if ($node->getName() === 'description') { |
|
| 200 |
↓
|
mapi |
$ruleSet->setDescription((string) $node); |
|
| 201 |
↓
|
mapi |
} else if ($node->getName() === 'rule') { |
|
| 202 |
↓
|
mapi |
$this->_parseRuleNode($ruleSet, $node); |
|
| 203 |
↓
|
mapi |
} |
|
| 204 |
↓
|
mapi |
} |
|
| 205 |
↓
|
mapi |
|
|
| 206 |
↓
|
mapi |
return $ruleSet; |
|
| 207 |
↓
|
mapi |
} |
|
| 208 |
↓
|
mapi |
|
|
| 209 |
37
|
mapi |
/** |
|
| 210 |
↓
|
mapi |
* This method parses a single rule xml node. Bases on the structure of the |
|
| 211 |
↓
|
mapi |
* xml node this method delegates the parsing process to another method in |
|
| 212 |
↓
|
mapi |
* this class. |
|
| 213 |
↓
|
mapi |
* |
|
| 214 |
↓
|
mapi |
* @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. |
|
| 215 |
↓
|
mapi |
* @param SimpleXMLElement $node The unparsed rule xml node. |
|
| 216 |
↓
|
mapi |
* |
|
| 217 |
↓
|
mapi |
* @return void |
|
| 218 |
↓
|
mapi |
*/ |
|
| 219 |
76
|
mapi |
private function _parseRuleNode( |
|
| 220 |
↓
|
mapi |
PHP_PMD_RuleSet $ruleSet, |
|
| 221 |
↓
|
mapi |
SimpleXMLElement $node |
|
| 222 |
↓
|
mapi |
) { |
|
| 223 |
1
|
mapi |
if (substr($node['ref'], -3, 3) === 'xml') { |
|
| 224 |
↓
|
mapi |
$this->_parseRuleSetReferenceNode($ruleSet, $node); |
|
| 225 |
↓
|
mapi |
} else if ('' === (string) $node['ref']) { |
|
| 226 |
↓
|
mapi |
$this->_parseSingleRuleNode($ruleSet, $node); |
|
| 227 |
↓
|
mapi |
} else { |
|
| 228 |
↓
|
mapi |
$this->_parseRuleReferenceNode($ruleSet, $node); |
|
| 229 |
↓
|
mapi |
} |
|
| 230 |
↓
|
mapi |
} |
|
| 231 |
↓
|
mapi |
|
|
| 232 |
37
|
mapi |
/** |
|
| 233 |
↓
|
mapi |
* This method parses a complete rule set that was includes a reference in |
|
| 234 |
↓
|
mapi |
* the currently parsed ruleset. |
|
| 235 |
↓
|
mapi |
* |
|
| 236 |
↓
|
mapi |
* @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. |
|
| 237 |
↓
|
mapi |
* @param SimpleXMLElement $ruleSetNode The unparsed rule xml node. |
|
| 238 |
↓
|
mapi |
* |
|
| 239 |
↓
|
mapi |
* @return void |
|
| 240 |
↓
|
mapi |
*/ |
|
| 241 |
76
|
mapi |
private function _parseRuleSetReferenceNode( |
|
| 242 |
↓
|
mapi |
PHP_PMD_RuleSet $ruleSet, |
|
| 243 |
↓
|
mapi |
SimpleXMLElement $ruleSetNode |
|
| 244 |
↓
|
mapi |
) { |
|
| 245 |
189
|
mapi |
$rules = $this->_parseRuleSetReference($ruleSetNode); |
|
| 246 |
↓
|
mapi |
foreach ($rules as $rule) { |
|
| 247 |
↓
|
mapi |
if ($this->_isIncluded($rule, $ruleSetNode)) { |
|
| 248 |
↓
|
mapi |
$ruleSet->addRule($rule); |
|
| 249 |
↓
|
mapi |
} |
|
| 250 |
↓
|
mapi |
} |
|
| 251 |
↓
|
mapi |
} |
|
| 252 |
↓
|
mapi |
|
|
| 253 |
↓
|
mapi |
/** |
|
| 254 |
↓
|
mapi |
* Parses a rule-set xml file referenced by the given rule-set xml element. |
|
| 255 |
↓
|
mapi |
* |
|
| 256 |
↓
|
mapi |
* @param SimpleXMLElement $ruleSetNode The context rule-set xml element. |
|
| 257 |
↓
|
mapi |
* |
|
| 258 |
↓
|
mapi |
* @return PHP_PMD_RuleSet |
|
| 259 |
↓
|
mapi |
* @since 0.2.3 |
|
| 260 |
↓
|
mapi |
*/ |
|
| 261 |
↓
|
mapi |
private function _parseRuleSetReference(SimpleXMLElement $ruleSetNode) |
|
| 262 |
↓
|
mapi |
{ |
|
| 263 |
1
|
mapi |
$ruleSetFactory = new PHP_PMD_RuleSetFactory(); |
|
| 264 |
↓
|
mapi |
$ruleSetFactory->setMinimumPriority($this->_minimumPriority); |
|
| 265 |
↓
|
mapi |
|
|
| 266 |
189
|
mapi |
return $ruleSetFactory->createSingleRuleSet((string) $ruleSetNode['ref']); |
|
| 267 |
↓
|
mapi |
} |
|
| 268 |
↓
|
mapi |
|
|
| 269 |
↓
|
mapi |
/** |
|
| 270 |
↓
|
mapi |
* Checks if the given rule is included/not excluded by the given rule-set |
|
| 271 |
↓
|
mapi |
* reference node. |
|
| 272 |
↓
|
mapi |
* |
|
| 273 |
↓
|
mapi |
* @param PHP_PMD_AbstractRule $rule The currently processed rule. |
|
| 274 |
↓
|
mapi |
* @param SimpleXMLElement $ruleSetNode The context rule-set xml element. |
|
| 275 |
↓
|
mapi |
* |
|
| 276 |
↓
|
mapi |
* @return boolean |
|
| 277 |
↓
|
mapi |
* @since 0.2.3 |
|
| 278 |
↓
|
mapi |
*/ |
|
| 279 |
↓
|
mapi |
private function _isIncluded( |
|
| 280 |
↓
|
mapi |
PHP_PMD_AbstractRule $rule, |
|
| 281 |
↓
|
mapi |
SimpleXMLElement $ruleSetNode |
|
| 282 |
↓
|
mapi |
) { |
|
| 283 |
↓
|
mapi |
foreach ($ruleSetNode->exclude as $exclude) { |
|
| 284 |
↓
|
mapi |
if ($rule->getName() === (string) $exclude['name']) { |
|
| 285 |
↓
|
mapi |
return false; |
|
| 286 |
↓
|
mapi |
} |
|
| 287 |
1
|
mapi |
} |
|
| 288 |
189
|
mapi |
return true; |
|
| 289 |
1
|
mapi |
} |
|
| 290 |
↓
|
mapi |
|
|
| 291 |
7
|
mapi |
/** |
|
| 292 |
↓
|
mapi |
* This method will create a single rule instance and add it to the given |
|
| 293 |
↓
|
mapi |
* {@link PHP_PMD_RuleSet} object. |
|
| 294 |
↓
|
mapi |
* |
|
| 295 |
↓
|
mapi |
* @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. |
|
| 296 |
↓
|
mapi |
* @param SimpleXMLElement $ruleNode The context rule-set xml node. |
|
| 297 |
↓
|
mapi |
* |
|
| 298 |
↓
|
mapi |
* @return void |
|
| 299 |
↓
|
mapi |
* @throws PHP_PMD_RuleClassFileNotFoundException When a class file does not |
|
| 300 |
↓
|
mapi |
* exist. |
|
| 301 |
↓
|
mapi |
* @throws PHP_PMD_RuleClassNotFoundException When a configured rule class |
|
| 302 |
↓
|
mapi |
* does not exist. |
|
| 303 |
↓
|
mapi |
*/ |
|
| 304 |
76
|
mapi |
private function _parseSingleRuleNode( |
|
| 305 |
↓
|
mapi |
PHP_PMD_RuleSet $ruleSet, |
|
| 306 |
↓
|
mapi |
SimpleXMLElement $ruleNode |
|
| 307 |
↓
|
mapi |
) { |
|
| 308 |
1
|
mapi |
$className = (string) $ruleNode['class']; |
|
| 309 |
↓
|
mapi |
$fileName = strtr($className, '_', '/') . '.php'; |
|
| 310 |
↓
|
mapi |
|
|
| 311 |
7
|
mapi |
if (class_exists($className) === false) { |
|
| 312 |
↓
|
mapi |
$fp = @fopen($fileName, 'r', true); |
|
| 313 |
↓
|
mapi |
if ($fp === false) { |
|
| 314 |
↓
|
mapi |
throw new PHP_PMD_RuleClassFileNotFoundException($className); |
|
| 315 |
↓
|
mapi |
} |
|
| 316 |
↓
|
mapi |
fclose($fp); |
|
| 317 |
1
|
mapi |
|
|
| 318 |
7
|
mapi |
include_once $fileName; |
|
| 319 |
1
|
mapi |
|
|
| 320 |
7
|
mapi |
if (class_exists($className) === false) { |
|
| 321 |
↓
|
mapi |
throw new PHP_PMD_RuleClassNotFoundException($className); |
|
| 322 |
↓
|
mapi |
} |
|
| 323 |
1
|
mapi |
} |
|
| 324 |
↓
|
mapi |
|
|
| 325 |
↓
|
mapi |
/* @var $rule PHP_PMD_AbstractRule */ |
|
| 326 |
↓
|
mapi |
$rule = new $className(); |
|
| 327 |
↓
|
mapi |
$rule->setName((string) $ruleNode['name']); |
|
| 328 |
↓
|
mapi |
$rule->setMessage((string) $ruleNode['message']); |
|
| 329 |
↓
|
mapi |
$rule->setExternalInfoUrl((string) $ruleNode['externalInfoUrl']); |
|
| 330 |
↓
|
mapi |
|
|
| 331 |
↓
|
mapi |
$rule->setRuleSetName($ruleSet->getName()); |
|
| 332 |
↓
|
mapi |
|
|
| 333 |
↓
|
mapi |
if (trim($ruleNode['since']) !== '') { |
|
| 334 |
↓
|
mapi |
$rule->setSince((string) $ruleNode['since']); |
|
| 335 |
↓
|
mapi |
} |
|
| 336 |
↓
|
mapi |
|
|
| 337 |
↓
|
mapi |
foreach ($ruleNode->children() as $node) { |
|
| 338 |
↓
|
mapi |
if ($node->getName() === 'description') { |
|
| 339 |
↓
|
mapi |
$rule->setDescription((string) $node); |
|
| 340 |
↓
|
mapi |
} else if ($node->getName() === 'example') { |
|
| 341 |
↓
|
mapi |
$rule->addExample((string) $node); |
|
| 342 |
↓
|
mapi |
} else if ($node->getName() === 'priority') { |
|
| 343 |
↓
|
mapi |
$rule->setPriority((integer) $node); |
|
| 344 |
↓
|
mapi |
} else if ($node->getName() === 'properties') { |
|
| 345 |
↓
|
mapi |
$this->_parsePropertiesNode($rule, $node); |
|
| 346 |
↓
|
mapi |
} |
|
| 347 |
↓
|
mapi |
} |
|
| 348 |
↓
|
mapi |
|
|
| 349 |
↓
|
mapi |
if ($rule->getPriority() <= $this->_minimumPriority) { |
|
| 350 |
↓
|
mapi |
$ruleSet->addRule($rule); |
|
| 351 |
↓
|
mapi |
} |
|
| 352 |
↓
|
mapi |
} |
|
| 353 |
↓
|
mapi |
|
|
| 354 |
37
|
mapi |
/** |
|
| 355 |
↓
|
mapi |
* This method parses a single rule that was included from a different |
|
| 356 |
↓
|
mapi |
* rule-set. |
|
| 357 |
↓
|
mapi |
* |
|
| 358 |
↓
|
mapi |
* @param PHP_PMD_RuleSet $ruleSet The parent rule-set instance. |
|
| 359 |
↓
|
mapi |
* @param SimpleXMLElement $ruleNode The unparsed rule xml node. |
|
| 360 |
↓
|
mapi |
* |
|
| 361 |
↓
|
mapi |
* @return void |
|
| 362 |
↓
|
mapi |
*/ |
|
| 363 |
76
|
mapi |
private function _parseRuleReferenceNode( |
|
| 364 |
↓
|
mapi |
PHP_PMD_RuleSet $ruleSet, |
|
| 365 |
↓
|
mapi |
SimpleXMLElement $ruleNode |
|
| 366 |
↓
|
mapi |
) { |
|
| 367 |
1
|
mapi |
$ref = (string) $ruleNode['ref']; |
|
| 368 |
↓
|
mapi |
|
|
| 369 |
↓
|
mapi |
$fileName = substr($ref, 0, strpos($ref, '.xml/') + 4); |
|
| 370 |
↓
|
mapi |
$fileName = $this->_createRuleSetFileName($fileName); |
|
| 371 |
↓
|
mapi |
|
|
| 372 |
↓
|
mapi |
$ruleName = substr($ref, strpos($ref, '.xml/') + 5); |
|
| 373 |
↓
|
mapi |
|
|
| 374 |
↓
|
mapi |
$ruleSetFactory = new PHP_PMD_RuleSetFactory(); |
|
| 375 |
↓
|
mapi |
|
|
| 376 |
43
|
mapi |
$ruleSetRef = $ruleSetFactory->createSingleRuleSet($fileName); |
|
| 377 |
↓
|
mapi |
$rule = $ruleSetRef->getRuleByName($ruleName); |
|
| 378 |
1
|
mapi |
|
|
| 379 |
↓
|
mapi |
if (trim($ruleNode['name']) !== '') { |
|
| 380 |
↓
|
mapi |
$rule->setName((string) $ruleNode['name']); |
|
| 381 |
↓
|
mapi |
} |
|
| 382 |
↓
|
mapi |
if (trim($ruleNode['message']) !== '') { |
|
| 383 |
↓
|
mapi |
$rule->setMessage((string) $ruleNode['message']); |
|
| 384 |
↓
|
mapi |
} |
|
| 385 |
↓
|
mapi |
if (trim($ruleNode['externalInfoUrl']) !== '') { |
|
| 386 |
↓
|
mapi |
$rule->setExternalInfoUrl((string) $ruleNode['externalInfoUrl']); |
|
| 387 |
↓
|
mapi |
} |
|
| 388 |
↓
|
mapi |
|
|
| 389 |
↓
|
mapi |
foreach ($ruleNode->children() as $node) { |
|
| 390 |
↓
|
mapi |
if ($node->getName() === 'description') { |
|
| 391 |
↓
|
mapi |
$rule->setDescription((string) $node); |
|
| 392 |
↓
|
mapi |
} else if ($node->getName() === 'example') { |
|
| 393 |
↓
|
mapi |
$rule->addExample((string) $node); |
|
| 394 |
↓
|
mapi |
} else if ($node->getName() === 'priority') { |
|
| 395 |
↓
|
mapi |
$rule->setPriority((integer) $node); |
|
| 396 |
↓
|
mapi |
} else if ($node->getName() === 'properties') { |
|
| 397 |
↓
|
mapi |
$this->_parsePropertiesNode($rule, $node); |
|
| 398 |
↓
|
mapi |
} |
|
| 399 |
↓
|
mapi |
} |
|
| 400 |
↓
|
mapi |
|
|
| 401 |
↓
|
mapi |
if ($rule->getPriority() <= $this->_minimumPriority) { |
|
| 402 |
↓
|
mapi |
$ruleSet->addRule($rule); |
|
| 403 |
↓
|
mapi |
} |
|
| 404 |
↓
|
mapi |
} |
|
| 405 |
↓
|
mapi |
|
|
| 406 |
↓
|
mapi |
/** |
|
| 407 |
↓
|
mapi |
* This method parses a xml properties structure and adds all found properties |
|
| 408 |
↓
|
mapi |
* to the given <b>$rule</b> object. |
|
| 409 |
↓
|
mapi |
* |
|
| 410 |
↓
|
mapi |
* <code> |
|
| 411 |
↓
|
mapi |
* ... |
|
| 412 |
↓
|
mapi |
* <properties> |
|
| 413 |
↓
|
mapi |
* <property name="foo" value="42" /> |
|
| 414 |
↓
|
mapi |
* <property name="bar" value="23" /> |
|
| 415 |
↓
|
mapi |
* ... |
|
| 416 |
↓
|
mapi |
* </properties> |
|
| 417 |
↓
|
mapi |
* ... |
|
| 418 |
↓
|
mapi |
* </code> |
|
| 419 |
↓
|
mapi |
* |
|
| 420 |
↓
|
mapi |
* @param PHP_PMD_AbstractRule $rule The context rule object. |
|
| 421 |
↓
|
mapi |
* @param SimpleXMLElement $propertiesNode The raw properties xml node. |
|
| 422 |
↓
|
mapi |
* |
|
| 423 |
↓
|
mapi |
* @return void |
|
| 424 |
↓
|
mapi |
*/ |
|
| 425 |
76
|
mapi |
private function _parsePropertiesNode( |
|
| 426 |
↓
|
mapi |
PHP_PMD_AbstractRule $rule, |
|
| 427 |
↓
|
mapi |
SimpleXMLElement $propertiesNode |
|
| 428 |
↓
|
mapi |
) { |
|
| 429 |
1
|
mapi |
foreach ($propertiesNode->children() as $node) { |
|
| 430 |
83
|
mapi |
if ($node->getName() === 'property') { |
|
| 431 |
↓
|
mapi |
$this->_addProperty($rule, $node); |
|
| 432 |
1
|
mapi |
} |
|
| 433 |
83
|
mapi |
} |
|
| 434 |
↓
|
mapi |
} |
|
| 435 |
1
|
mapi |
|
|
| 436 |
83
|
mapi |
/** |
|
| 437 |
↓
|
mapi |
* Adds an additional propery to the given <b>$rule</b> instance. |
|
| 438 |
↓
|
mapi |
* |
|
| 439 |
↓
|
mapi |
* @param PHP_PMD_AbstractRule $rule The context rule object. |
|
| 440 |
↓
|
mapi |
* @param SimpleXMLElement $node The raw xml property node. |
|
| 441 |
↓
|
mapi |
* |
|
| 442 |
↓
|
mapi |
* @return void |
|
| 443 |
↓
|
mapi |
*/ |
|
| 444 |
↓
|
mapi |
private function _addProperty( |
|
| 445 |
↓
|
mapi |
PHP_PMD_AbstractRule $rule, |
|
| 446 |
↓
|
mapi |
SimpleXMLElement $node |
|
| 447 |
↓
|
mapi |
) { |
|
| 448 |
↓
|
mapi |
$name = trim($node['name']); |
|
| 449 |
220
|
mapi |
$value = trim($this->_getPropertyValue($node)); |
|
| 450 |
83
|
mapi |
if ($name !== '' && $value !== '') { |
|
| 451 |
↓
|
mapi |
$rule->addProperty($name, $value); |
|
| 452 |
1
|
mapi |
} |
|
| 453 |
↓
|
mapi |
} |
|
| 454 |
220
|
mapi |
|
|
| 455 |
↓
|
mapi |
/** |
|
| 456 |
↓
|
mapi |
* Returns the value of a property node. This value can be expressed in |
|
| 457 |
↓
|
mapi |
* two different notations. First version is an attribute named <b>value</b> |
|
| 458 |
↓
|
mapi |
* and the second valid notation is a child element named <b>value</b> that |
|
| 459 |
↓
|
mapi |
* contains the value as character data. |
|
| 460 |
↓
|
mapi |
* |
|
| 461 |
↓
|
mapi |
* @param SimpleXMLElement $propertyNode The raw xml property node. |
|
| 462 |
↓
|
mapi |
* |
|
| 463 |
↓
|
mapi |
* @return string |
|
| 464 |
↓
|
mapi |
* @since 0.2.5 |
|
| 465 |
↓
|
mapi |
*/ |
|
| 466 |
↓
|
mapi |
private function _getPropertyValue(SimpleXMLElement $propertyNode) |
|
| 467 |
↓
|
mapi |
{ |
|
| 468 |
↓
|
mapi |
if (isset($propertyNode->value)) { |
|
| 469 |
↓
|
mapi |
return (string) $propertyNode->value; |
|
| 470 |
↓
|
mapi |
} |
|
| 471 |
↓
|
mapi |
return (string) $propertyNode['value']; |
|
| 472 |
↓
|
mapi |
} |
|
| 473 |
78
|
mapi |
} |
|