| Conditions | 13 |
| Paths | 2 |
| Total Lines | 118 |
| 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 |
||
| 14 | public function mapMainArticle($singleJsonArticle) { |
||
| 15 | |||
| 16 | $decoded_article = $singleJsonArticle; |
||
| 17 | |||
| 18 | if (!is_null($this->jsonToArray($singleJsonArticle))) { |
||
| 19 | $decoded_article = $this->jsonToArray($singleJsonArticle); |
||
| 20 | } |
||
| 21 | |||
| 22 | $data_article = $decoded_article['data']; |
||
| 23 | |||
| 24 | $title = function () use ($data_article) { |
||
| 25 | if (isset($data_article['title'])) { |
||
| 26 | return $data_article['title']; |
||
| 27 | } |
||
| 28 | return null; |
||
| 29 | }; |
||
| 30 | |||
| 31 | $body = function () use ($data_article) { |
||
| 32 | if (isset($data_article['body'])) { |
||
| 33 | return $data_article['body']; |
||
| 34 | } |
||
| 35 | return null; |
||
| 36 | }; |
||
| 37 | |||
| 38 | $source = function () use ($data_article) { |
||
| 39 | if (isset($data_article['source'])) { |
||
| 40 | return $data_article['source']; |
||
| 41 | } |
||
| 42 | return null; |
||
| 43 | }; |
||
| 44 | |||
| 45 | $uniqueId = function () use ($data_article) { |
||
| 46 | if (isset($data_article['uniqueId'])) { |
||
| 47 | return $data_article['uniqueId']; |
||
| 48 | } |
||
| 49 | return null; |
||
| 50 | }; |
||
| 51 | |||
| 52 | $typeId = function () use ($data_article) { |
||
| 53 | if (isset($data_article['type']['type_id'])) { |
||
| 54 | return $data_article['type']['type_id']; |
||
| 55 | } |
||
| 56 | return null; |
||
| 57 | }; |
||
| 58 | |||
| 59 | $categoryId = function () use ($data_article) { |
||
| 60 | if (isset($data_article['category']['category_id'])) { |
||
| 61 | return $data_article['category']['category_id']; |
||
| 62 | } |
||
| 63 | return null; |
||
| 64 | }; |
||
| 65 | |||
| 66 | $reporter = function () use ($data_article) { |
||
| 67 | if (isset($data_article['reporter'])) { |
||
| 68 | return $data_article['reporter']; |
||
| 69 | } |
||
| 70 | return null; |
||
| 71 | }; |
||
| 72 | |||
| 73 | $lead = function () use ($data_article) { |
||
| 74 | if (isset($data_article['lead'])) { |
||
| 75 | return $data_article['lead']; |
||
| 76 | } |
||
| 77 | return null; |
||
| 78 | }; |
||
| 79 | |||
| 80 | $tags = function () use ($data_article) { |
||
| 81 | if (isset($data_article['tags']['tag_name'])) { |
||
| 82 | return $data_article['tags']['tag_name']; |
||
| 83 | } |
||
| 84 | return null; |
||
| 85 | }; |
||
| 86 | |||
| 87 | $publishedAt = function () use ($data_article) { |
||
| 88 | if (isset($data_article['published_at'])) { |
||
| 89 | return $data_article['published_at']; |
||
| 90 | } |
||
| 91 | return null; |
||
| 92 | }; |
||
| 93 | |||
| 94 | $identifier = function () use ($data_article) { |
||
| 95 | if (isset($data_article['id'])) { |
||
| 96 | return $data_article['id']; |
||
| 97 | } |
||
| 98 | return null; |
||
| 99 | }; |
||
| 100 | |||
| 101 | $article = array(); |
||
|
|
|||
| 102 | |||
| 103 | $article = new Article( |
||
| 104 | |||
| 105 | $title(), |
||
| 106 | |||
| 107 | $body(), |
||
| 108 | |||
| 109 | $source(), |
||
| 110 | |||
| 111 | $uniqueId(), |
||
| 112 | |||
| 113 | $typeId(), |
||
| 114 | |||
| 115 | $categoryId(), |
||
| 116 | |||
| 117 | $reporter(), |
||
| 118 | |||
| 119 | $lead(), |
||
| 120 | |||
| 121 | $tags(), |
||
| 122 | |||
| 123 | $publishedAt(), |
||
| 124 | |||
| 125 | $identifier() |
||
| 126 | |||
| 127 | ); |
||
| 128 | |||
| 129 | return $article; |
||
| 130 | |||
| 131 | } |
||
| 132 | |||
| 150 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.