/** * @param RuleInterface $rule * @param ContextInterface $context */ private function applyRule(RuleInterface $rule, ContextInterface $context) { if (!$rule->appliesToContext($context)) { return; } $rule->apply($context); }
function it_can_apply_to_multiple_other_rules(RuleInterface $rule1, RuleInterface $rule2, ContextInterface $context) { $rule1->appliesToContext($context)->willReturn(false); $rule1->apply($context)->shouldNotBeCalled(); $rule2->appliesToContext($context)->willReturn(true); $rule2->apply($context)->shouldBeCalled(); $this->apply($context); }
/** * @param ContextInterface $context * * @return bool */ public function appliesToContext(ContextInterface $context) { if (!$context instanceof TypeContext && !$context instanceof PropertyContext) { return false; } $type = $context->getType(); $typeExists = array_key_exists($type->getName(), $this->typeMap); // The default rule will be used here: if (!$typeExists) { return $this->defaultRule->appliesToContext($context); } // It's possible to define a null rule, which means that no code will be generated. $rule = $this->typeMap[$type->getName()]; if ($rule === null) { return false; } return $rule->appliesToContext($context); }
function it_appies_subrule_when_applied(RuleInterface $subRule, ContextInterface $context) { $subRule->apply($context)->shouldBeCalled(); $this->apply($context); }
function it_applies_the_default_rule_to_unknown_types(RuleInterface $defaultRule, TypeContext $context) { $context->getType()->willReturn(new Type('MyNamespace', 'UnknownType', [])); $defaultRule->apply($context)->shouldBeCalled(); $this->apply($context); }
function it_can_skip_rules(RuleInterface $rule, ContextInterface $context) { $rule->appliesToContext($context)->willReturn(false); $rule->apply($context)->shouldNotBeCalled(); $this->applyRules($context); }
/** * @param ContextInterface $context */ public function apply(ContextInterface $context) { $this->subRule->apply($context); }