Conditions | 10 |
Paths | 8 |
Total Lines | 74 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Tests | 43 |
CRAP Score | 10.001 |
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 |
||
26 | 52 | public function __invoke(Readable $stream, Protocol $protocol): Frame |
|
27 | { |
||
28 | 52 | $octet = UnsignedOctet::fromStream($stream); |
|
29 | |||
30 | try { |
||
31 | 52 | $type = Type::fromInt($octet->original()->value()); |
|
1 ignored issue
–
show
|
|||
32 | 2 | } catch (UnknownFrameType $e) { |
|
33 | 2 | throw new NoFrameDetected(new StringStream( |
|
34 | 2 | (string) $stream->read()->prepend((string) $octet) |
|
35 | )); |
||
36 | } |
||
37 | |||
38 | 51 | $channel = new Channel( |
|
39 | 51 | UnsignedShortInteger::fromStream($stream)->original()->value() |
|
1 ignored issue
–
show
|
|||
40 | ); |
||
41 | $payload = $stream |
||
42 | 51 | ->read(UnsignedLongInteger::fromStream($stream)->original()->value()) |
|
1 ignored issue
–
show
|
|||
43 | 51 | ->toEncoding('ASCII'); |
|
44 | |||
45 | if ( |
||
46 | ( |
||
47 | 51 | $type === Type::method() || |
|
48 | 51 | $type === Type::header() |
|
49 | ) && |
||
50 | 51 | $payload->length() < 4 |
|
51 | ) { |
||
52 | 1 | throw new PayloadTooShort; |
|
53 | } |
||
54 | |||
55 | 50 | $end = UnsignedOctet::fromStream($stream)->original()->value(); |
|
56 | |||
57 | 50 | if ($end !== Frame::end()) { |
|
58 | 1 | throw new ReceivedFrameNotDelimitedCorrectly; |
|
59 | } |
||
60 | |||
61 | 49 | $payload = new StringStream((string) $payload); |
|
62 | |||
63 | switch ($type) { |
||
64 | 49 | case Type::method(): |
|
65 | 46 | $method = new Method( |
|
66 | 46 | UnsignedShortInteger::fromStream($payload) |
|
1 ignored issue
–
show
|
|||
67 | 46 | ->original() |
|
68 | 46 | ->value(), |
|
69 | 46 | UnsignedShortInteger::fromStream($payload) |
|
1 ignored issue
–
show
|
|||
70 | 46 | ->original() |
|
71 | 46 | ->value() |
|
72 | ); |
||
73 | |||
74 | 46 | return Frame::method( |
|
75 | 46 | $channel, |
|
76 | 46 | $method, |
|
77 | 46 | ...$protocol->read($method, $payload) |
|
78 | ); |
||
79 | |||
80 | 19 | case Type::header(): |
|
81 | 17 | $class = UnsignedShortInteger::fromStream($payload) |
|
82 | 17 | ->original() |
|
83 | 17 | ->value(); |
|
84 | 17 | $payload->read(2); // walk over the weight definition |
|
85 | |||
86 | 17 | return Frame::header( |
|
87 | 17 | $channel, |
|
88 | 17 | $class, |
|
89 | 17 | ...$protocol->readHeader($payload) |
|
90 | ); |
||
91 | |||
92 | 18 | case Type::body(): |
|
93 | 17 | return Frame::body($channel, $payload->read()); |
|
94 | |||
95 | 1 | case Type::heartbeat(): |
|
96 | 1 | return Frame::heartbeat(); |
|
97 | |||
98 | default: |
||
99 | throw new LogicException; //if reached then there's an implementation error |
||
100 | } |
||
103 |