Arbit - project tracking

PHPMD

History

Diff

188 189 /test/PHP/PMD/RuleSetFactoryTest.php
531 531 }
532 532
533 533 /**
534 + * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule
535 + *
536 + * @return void
537 + * @covers PHP_PMD_RuleSetFactory
538 + * @group phpmd
539 + * @group unittest
540 + */
541 + public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule()
542 + {
543 + self::changeWorkingDirectory();
544 +
545 + $factory = new PHP_PMD_RuleSetFactory();
546 + $ruleSets = $factory->createRuleSets('refset-exclude-one');
547 +
548 + $rules = $ruleSets[0]->getRules();
549 + $this->assertEquals(1, iterator_count($rules));
550 + }
551 +
552 + /**
553 + * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules
554 + *
555 + * @return void
556 + * @covers PHP_PMD_RuleSetFactory
557 + * @group phpmd
558 + * @group unittest
559 + */
560 + public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules()
561 + {
562 + self::changeWorkingDirectory();
563 +
564 + $factory = new PHP_PMD_RuleSetFactory();
565 + $ruleSets = $factory->createRuleSets('refset-exclude-all');
566 +
567 + $rules = $ruleSets[0]->getRules();
568 + $this->assertEquals(0, iterator_count($rules));
569 + }
570 +
571 + /**
534 572 * Tests that the factory throws the expected exception for an invalid ruleset
535 573 * identifier.
536 574 *
188 189 /test/PHP/PMD/_files/rulesets/refset-exclude-all.xml
2 +<?xml version="1.0" encoding="UTF-8"?>
3 +
4 +<ruleset name="Fourth Test RuleSet"
5 + xmlns="http://pmd.sf.net/ruleset/1.0.0"
6 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 + xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
8 + xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
9 +
10 + <description>First description...</description>
11 +
12 + <rule name="Exclude all rules"
13 + message="Exclude all rules"
14 + externalInfoUrl="http://example.com/exclude-all-rules"
15 + ref="rulesets/set1.xml">
16 + <exclude name="RuleOneInFirstRuleSet" />
17 + <exclude name="RuleTwoInFirstRuleSet" />
18 + </rule>
19 +</ruleset>
188 189 /test/PHP/PMD/_files/rulesets/refset-exclude-one.xml
2 +<?xml version="1.0" encoding="UTF-8"?>
3 +
4 +<ruleset name="Fourth Test RuleSet"
5 + xmlns="http://pmd.sf.net/ruleset/1.0.0"
6 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 + xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
8 + xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
9 +
10 + <description>First description...</description>
11 +
12 + <rule name="Exclude one rule"
13 + message="Exclude one rule"
14 + externalInfoUrl="http://example.com/exclude-one-rule"
15 + ref="rulesets/set1.xml">
16 + <exclude name="RuleTwoInFirstRuleSet" />
17 + </rule>
18 +</ruleset>
188 189 /source/PHP/PMD/RuleSetFactory.php
243 243 PHP_PMD_RuleSet $ruleSet,
244 244 SimpleXMLElement $ruleSetNode
245 245 ) {
246 + $rules = $this->_parseRuleSetReference($ruleSetNode);
247 + foreach ($rules as $rule) {
248 + if ($this->_isIncluded($rule, $ruleSetNode)) {
249 + $ruleSet->addRule($rule);
250 + }
251 + }
252 + }
253 +
254 + /**
255 + * Parses a rule-set xml file referenced by the given rule-set xml element.
256 + *
257 + * @param SimpleXMLElement $ruleSetNode The context rule-set xml element.
258 + *
259 + * @return PHP_PMD_RuleSet
260 + * @since 0.2.3
261 + */
262 + private function _parseRuleSetReference(SimpleXMLElement $ruleSetNode)
263 + {
246 264 $ruleSetFactory = new PHP_PMD_RuleSetFactory();
247 265 $ruleSetFactory->setMinimumPriority($this->_minimumPriority);
248 266
249 - $rules = $ruleSetFactory->createSingleRuleSet((string) $ruleSetNode['ref']);
250 - foreach ($rules as $rule) {
251 - $ruleSet->addRule($rule);
267 + return $ruleSetFactory->createSingleRuleSet((string) $ruleSetNode['ref']);
268 + }
269 +
270 + /**
271 + * Checks if the given rule is included/not excluded by the given rule-set
272 + * reference node.
273 + *
274 + * @param PHP_PMD_AbstractRule $rule The currently processed rule.
275 + * @param SimpleXMLElement $ruleSetNode The context rule-set xml element.
276 + *
277 + * @return boolean
278 + * @since 0.2.3
279 + */
280 + private function _isIncluded(
281 + PHP_PMD_AbstractRule $rule,
282 + SimpleXMLElement $ruleSetNode
283 + ) {
284 + foreach ($ruleSetNode->exclude as $exclude) {
285 + if ($rule->getName() === (string) $exclude['name']) {
286 + return false;
287 + }
252 288 }
289 + return true;
253 290 }
254 291
255 292 /**