Conditions | 5 |
Paths | 8 |
Total Lines | 51 |
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 |
||
43 | public function apply(ClassNode $node) |
||
44 | { |
||
45 | $node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface'); |
||
46 | $node->addProperty('objectProphecyClosure', 'private'); |
||
47 | |||
48 | foreach ($node->getMethods() as $name => $method) { |
||
49 | if ('__construct' === strtolower($name)) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | if ($method->getReturnType() === 'void') { |
||
54 | $method->setCode( |
||
55 | '$this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());' |
||
56 | ); |
||
57 | } else { |
||
58 | $method->setCode( |
||
59 | 'return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());' |
||
60 | ); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $prophecySetter = new MethodNode('setProphecy'); |
||
65 | $prophecyArgument = new ArgumentNode('prophecy'); |
||
66 | $prophecyArgument->setTypeHint('Prophecy\Prophecy\ProphecyInterface'); |
||
67 | $prophecySetter->addArgument($prophecyArgument); |
||
68 | $prophecySetter->setCode('$this->objectProphecyClosure = function () use ($prophecy) { return $prophecy; };'); |
||
69 | |||
70 | $prophecyGetter = new MethodNode('getProphecy'); |
||
71 | $prophecyGetter->setCode('return call_user_func($this->objectProphecyClosure);'); |
||
72 | |||
73 | if ($node->hasMethod('__call')) { |
||
74 | $__call = $node->getMethod('__call'); |
||
75 | } else { |
||
76 | $__call = new MethodNode('__call'); |
||
77 | $__call->addArgument(new ArgumentNode('name')); |
||
78 | $__call->addArgument(new ArgumentNode('arguments')); |
||
79 | |||
80 | $node->addMethod($__call, true); |
||
81 | } |
||
82 | |||
83 | $__call->setCode(<<<PHP |
||
84 | throw new \Prophecy\Exception\Doubler\MethodNotFoundException( |
||
85 | sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)), |
||
86 | \$this->getProphecy(), func_get_arg(0) |
||
87 | ); |
||
88 | PHP |
||
89 | ); |
||
90 | |||
91 | $node->addMethod($prophecySetter, true); |
||
92 | $node->addMethod($prophecyGetter, true); |
||
93 | } |
||
94 | |||
105 |