Conditions | 21 |
Paths | 24 |
Total Lines | 88 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
44 | public function parseFromMessage(string $messageID, int $relID, int $groupID, NNTP $nntp, int $show): bool |
||
45 | { |
||
46 | if ($messageID === '') { |
||
47 | return false; |
||
48 | } |
||
49 | |||
50 | $query = Release::query() |
||
51 | ->where(['isrenamed' => 0, 'id' => $relID]) |
||
52 | ->select(['id', 'groups_id', 'categories_id', 'name', 'searchname', 'postdate', 'id as releases_id']) |
||
53 | ->first(); |
||
54 | |||
55 | if ($query === null) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | // Only get a new name if the category is OTHER. |
||
60 | $foundName = true; |
||
61 | if (\in_array((int) $query['categories_id'], Category::OTHERS_GROUP, false)) { |
||
62 | $foundName = false; |
||
63 | } |
||
64 | |||
65 | // Get the PAR2 file. |
||
66 | $par2 = $nntp->getMessages(UsenetGroup::getNameByID($groupID), $messageID, $this->alternateNNTP); |
||
67 | if ($nntp->isError($par2)) { |
||
68 | return false; |
||
69 | } |
||
70 | |||
71 | // Put the PAR2 into Par2Info, check if there's an error. |
||
72 | $this->par2Info->setData($par2); |
||
73 | if ($this->par2Info->error) { |
||
74 | return false; |
||
75 | } |
||
76 | |||
77 | // Get the file list from Par2Info. |
||
78 | $files = $this->par2Info->getFileList(); |
||
79 | if ($files !== false && \count($files) > 0) { |
||
80 | $filesAdded = 0; |
||
81 | |||
82 | // Loop through the files. |
||
83 | foreach ($files as $file) { |
||
84 | if (! isset($file['name'])) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | // If we found a name and added 20 files, stop. |
||
89 | if ($foundName === true && $filesAdded > 20) { |
||
90 | break; |
||
91 | } |
||
92 | |||
93 | if ($this->addPar2) { |
||
94 | // Add to release files. |
||
95 | if ($filesAdded < 21 && ReleaseFile::query()->where(['releases_id' => $relID, 'name' => $file['name']])->first() === null) { |
||
96 | // Try to add the files to the DB. |
||
97 | if (ReleaseFile::addReleaseFiles( |
||
98 | $relID, |
||
99 | $file['name'], |
||
100 | $file['size'], |
||
101 | $query['postdate'] !== null ? Carbon::createFromFormat('Y-m-d H:i:s', $query['postdate']) : now(), |
||
102 | 0, |
||
103 | $file['hash_16K'] |
||
104 | )) { |
||
105 | $filesAdded++; |
||
106 | } |
||
107 | } |
||
108 | } else { |
||
109 | $filesAdded++; |
||
110 | } |
||
111 | |||
112 | // Try to get a new name. |
||
113 | if ($foundName === false) { |
||
114 | $query['textstring'] = $file['name']; |
||
115 | if ($this->nameFixer->checkName($query, 1, 'PAR2, ', 1, $show)) { |
||
|
|||
116 | $foundName = true; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // If we found some files. |
||
122 | if ($filesAdded > 0) { |
||
123 | // Update the file count with the new file count + old file count. |
||
124 | Release::whereId($relID)->increment('rarinnerfilecount', $filesAdded); |
||
125 | } |
||
126 | if ($foundName === true) { |
||
127 | return true; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return false; |
||
132 | } |
||
134 |