Conditions | 13 |
Paths | 9 |
Total Lines | 50 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
32 | public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self |
||
33 | { |
||
34 | if ($recordType->isA(DNSRecordType::TYPE_TXT)) { |
||
35 | return new TXTData(trim($data, '"')); |
||
36 | } |
||
37 | |||
38 | if ($recordType->isA(DNSRecordType::TYPE_NS)) { |
||
39 | return new NSData(new Hostname($data)); |
||
40 | } |
||
41 | |||
42 | if ($recordType->isA(DNSRecordType::TYPE_CNAME)) { |
||
43 | return new CNAMEData(new Hostname($data)); |
||
44 | } |
||
45 | |||
46 | $parsed = self::parseDataToArray($data); |
||
47 | |||
48 | if ($recordType->isA(DNSRecordType::TYPE_MX)) { |
||
49 | return new MXData(new Hostname($parsed[1]), (int)$parsed[0]); |
||
50 | } |
||
51 | |||
52 | if ($recordType->isA(DNSRecordType::TYPE_SOA)) { |
||
53 | return new SOAData( |
||
54 | new Hostname($parsed[0]), |
||
55 | new Hostname($parsed[1]), |
||
56 | (int)($parsed[2] ?? 0), |
||
57 | (int)($parsed[3] ?? 0), |
||
58 | (int)($parsed[4] ?? 0), |
||
59 | (int)($parsed[5] ?? 0), |
||
60 | (int)($parsed[6] ?? 0) |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | if ($recordType->isA(DNSRecordType::TYPE_CAA) && count($parsed) === 3) { |
||
65 | return new CAAData((int)$parsed[0], (string)$parsed[1], $parsed[2]); |
||
66 | } |
||
67 | |||
68 | if ($recordType->isA(DNSRecordType::TYPE_SRV)) { |
||
69 | return new SRVData( |
||
70 | (int)$parsed[0] ?: 0, |
||
71 | (int) $parsed[1] ?: 0, |
||
72 | (int) $parsed[2] ?: 0, |
||
73 | new Hostname($parsed[3]) |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | if ($recordType->isA(DNSRecordType::TYPE_PTR)) { |
||
78 | return new PTRData(new Hostname($data)); |
||
79 | } |
||
80 | |||
81 | throw new InvalidArgumentException("{$data} could not be created with type {$recordType}"); |
||
82 | } |
||
97 |