Conditions | 12 |
Paths | 2 |
Total Lines | 78 |
Code Lines | 58 |
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 |
||
45 | public function testProcess() |
||
46 | { |
||
47 | $this->container |
||
|
|||
48 | ->expects($this->once()) |
||
49 | ->method('findTaggedServiceIds') |
||
50 | ->with($this->identicalTo($tag = 'lug.registry')) |
||
51 | ->will($this->returnValue([$service = 'my.registry' => []])); |
||
52 | |||
53 | $this->container |
||
54 | ->expects($this->once()) |
||
55 | ->method('getDefinition') |
||
56 | ->with($this->identicalTo($service)) |
||
57 | ->will($this->returnValue($definition = $this->createDefinitionMock())); |
||
58 | |||
59 | $definition |
||
60 | ->expects($this->once()) |
||
61 | ->method('getClass') |
||
62 | ->will($this->returnValue(Registry::class)); |
||
63 | |||
64 | $definition |
||
65 | ->expects($this->once()) |
||
66 | ->method('clearTag') |
||
67 | ->with($this->identicalTo($tag)) |
||
68 | ->will($this->returnSelf()); |
||
69 | |||
70 | $types = [ |
||
71 | ['type1', new Reference('my.id1')], |
||
72 | ['type2', new Reference('my.id2')], |
||
73 | ]; |
||
74 | |||
75 | $map = []; |
||
76 | foreach ($types as $type) { |
||
77 | $map[] = ['offsetSet', $type]; |
||
78 | } |
||
79 | |||
80 | $definition |
||
81 | ->expects($this->once()) |
||
82 | ->method('getMethodCalls') |
||
83 | ->will($this->returnValue($map)); |
||
84 | |||
85 | $definition |
||
86 | ->expects($this->exactly(3)) |
||
87 | ->method('hasMethodCall') |
||
88 | ->with($this->identicalTo('offsetSet')) |
||
89 | ->willReturnOnConsecutiveCalls(true, true, false); |
||
90 | |||
91 | $definition |
||
92 | ->expects($this->exactly(2)) |
||
93 | ->method('removeMethodCall') |
||
94 | ->with('offsetSet'); |
||
95 | |||
96 | $this->container |
||
97 | ->expects($this->exactly(2)) |
||
98 | ->method('setDefinition') |
||
99 | ->withConsecutive( |
||
100 | [$service.'.internal', $definition], |
||
101 | [$service, $this->callback(function ($definition) use ($service, $types) { |
||
102 | $result = $definition instanceof Definition |
||
103 | && $definition->getClass() === LazyRegistry::class |
||
104 | && count($definition->getArguments()) === 2 |
||
105 | && $definition->getArgument(0) instanceof Reference |
||
106 | && (string) $definition->getArgument(0) === 'service_container' |
||
107 | && $definition->getArgument(1) instanceof Reference |
||
108 | && (string) $definition->getArgument(1) === $service.'.internal'; |
||
109 | |||
110 | $result = $result && count($methodCalls = $definition->getMethodCalls()) === count($types); |
||
111 | |||
112 | foreach ($types as $type => $value) { |
||
113 | $value[1] = (string) $value[1]; |
||
114 | $result = $result && isset($methodCalls[$type]) && $methodCalls[$type] === ['setLazy', $value]; |
||
115 | } |
||
116 | |||
117 | return $result; |
||
118 | })] |
||
119 | ); |
||
120 | |||
121 | $this->compiler->process($this->container); |
||
122 | } |
||
123 | |||
177 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: