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