Conditions | 1 |
Paths | 1 |
Total Lines | 122 |
Code Lines | 91 |
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 |
||
38 | public function testHandle() |
||
39 | { |
||
40 | $this |
||
41 | ->given( |
||
42 | $resolver = new HandlerClassResolver( |
||
43 | new FromClassNameResolver(), |
||
44 | new ChainResolver([ |
||
45 | new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
||
46 | new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
||
47 | new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
||
48 | ]), |
||
49 | new InMemoryLocator([LoginUserMessage::class => new UserMessageValidator()]) |
||
50 | ) |
||
51 | ) |
||
52 | ->and($middleware = new ValidatorMiddleware($resolver)) |
||
53 | ->and($command = new LoginUserMessage('[email protected]', 'plainpassword')) |
||
|
|||
54 | ->and($callable = function (LoginUserMessage $command) { |
||
55 | $command->setEmail('[email protected]'); |
||
56 | }) |
||
57 | ->when($middleware->handle($command, $callable)) |
||
58 | ->then() |
||
59 | ->string($command->email()) |
||
60 | ->isEqualTo('[email protected]') |
||
61 | ->and() |
||
62 | ->when($command = new LoginUserMessage('[email protected]', 'plainpassword')) |
||
63 | ->then() |
||
64 | ->exception(function () use ($middleware, $command, $callable) { |
||
65 | $middleware->handle($command, $callable); |
||
66 | })->isInstanceOf(ValidationException::class) |
||
67 | ; |
||
68 | |||
69 | $this |
||
70 | ->given( |
||
71 | $resolver = new HandlerClassResolver( |
||
72 | new FromClassNameResolver(), |
||
73 | new ChainResolver([ |
||
74 | new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
||
75 | new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
||
76 | new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
||
77 | ]), |
||
78 | new InMemoryLocator([LoginUserMessage::class => new UserMessageValidator()]) |
||
79 | ) |
||
80 | ) |
||
81 | ->and($middleware = new ValidatorMiddleware($resolver)) |
||
82 | ->and($command = new LoginUserMessage('invalid.email.com', 'plainpassword')) |
||
83 | ->and($callable = function () { |
||
84 | }) |
||
85 | ->then() |
||
86 | ->exception(function () use ($middleware, $command, $callable) { |
||
87 | $middleware->handle($command, $callable); |
||
88 | })->isInstanceOf(ValidationException::class) |
||
89 | ; |
||
90 | |||
91 | $this |
||
92 | ->given( |
||
93 | $resolver = new HandlerClassResolver( |
||
94 | new FromClassNameResolver(), |
||
95 | new ChainResolver([ |
||
96 | new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
||
97 | new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
||
98 | new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
||
99 | ]), |
||
100 | new InMemoryLocator([LogoutUserMessage::class => new UserMessageValidator()]) |
||
101 | ) |
||
102 | ) |
||
103 | ->and($middleware = new ValidatorMiddleware($resolver)) |
||
104 | ->and($command = new LogoutUserMessage('invalid.email.com')) |
||
105 | ->and($callable = function (LogoutUserMessage $command) { |
||
106 | $command->setEmail('[email protected]'); |
||
107 | }) |
||
108 | ->then() |
||
109 | ->exception(function () use ($middleware, $command, $callable) { |
||
110 | $middleware->handle($command, $callable); |
||
111 | })->isInstanceOf(ValidationException::class) |
||
112 | ; |
||
113 | |||
114 | $this |
||
115 | ->given( |
||
116 | $resolver = new HandlerClassResolver( |
||
117 | new FromClassNameResolver(), |
||
118 | new ChainResolver([ |
||
119 | new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
||
120 | new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
||
121 | new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
||
122 | ]), |
||
123 | new InMemoryLocator([]) |
||
124 | ) |
||
125 | ) |
||
126 | ->and($middleware = new ValidatorMiddleware($resolver)) |
||
127 | ->and($command = new RemoveUserMessage('[email protected]')) |
||
128 | ->and($callable = function (RemoveUserMessage $command) { |
||
129 | $command->setEmail('[email protected]'); |
||
130 | }) |
||
131 | ->when($middleware->handle($command, $callable)) |
||
132 | ->then() |
||
133 | ->string($command->email()) |
||
134 | ->isEqualTo('[email protected]') |
||
135 | ; |
||
136 | |||
137 | $this |
||
138 | ->given( |
||
139 | $resolver = new HandlerClassResolver( |
||
140 | new FromClassNameResolver(), |
||
141 | new ChainResolver([ |
||
142 | new MethodWithShortObjectNameAndSuffixResolver('Command', 'Validator'), |
||
143 | new MethodWithShortObjectNameAndSuffixResolver('Query', 'Validator'), |
||
144 | new MethodWithShortObjectNameAndSuffixResolver('Message', 'Validator'), |
||
145 | ]), |
||
146 | new InMemoryLocator([RemoveUserMessage::class => new UserMessageValidator()]) |
||
147 | ) |
||
148 | ) |
||
149 | ->and($middleware = new ValidatorMiddleware($resolver)) |
||
150 | ->and($command = new RemoveUserMessage('[email protected]')) |
||
151 | ->and($callable = function (RemoveUserMessage $command) { |
||
152 | $command->setEmail('[email protected]'); |
||
153 | }) |
||
154 | ->then() |
||
155 | ->exception(function () use ($middleware, $command, $callable) { |
||
156 | $middleware->handle($command, $callable); |
||
157 | })->isInstanceOf(NotFoundException::class) |
||
158 | ; |
||
159 | } |
||
160 | } |
||
161 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.