| Conditions | 7 |
| Paths | 995 |
| Total Lines | 84 |
| Code Lines | 59 |
| Lines | 84 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 51 | View Code Duplication | public function parse($uri) |
|
| 52 | { |
||
| 53 | $cfp = clone($this->cfp); |
||
| 54 | try { |
||
| 55 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
| 56 | |||
| 57 | $content = file_Get_contents($uri); |
||
| 58 | $content = mb_convert_encoding($content, 'UTF-8'); |
||
| 59 | $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content); |
||
| 60 | $dom->preserveWhiteSpace = false; |
||
| 61 | |||
| 62 | $timezone = 'UTC'; |
||
| 63 | $xpath = new \DOMXPath($dom); |
||
| 64 | |||
| 65 | $eventLocation = new Location(); |
||
| 66 | $cfp->location = $eventLocation->parse($dom, $xpath); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $location = $this->getLatLonForLocation($cfp->location); |
||
| 70 | $cfp->latitude = $location[0]; |
||
| 71 | $cfp->longitude = $location[1]; |
||
| 72 | $timezone = $this->tzService->getTimezoneForLocation($location[0], $location[1]); |
||
| 73 | } catch (\UnexpectedValueException $e) { |
||
| 74 | error_log($e->getMessage()); |
||
| 75 | } |
||
| 76 | $cfp->timezone = $timezone; |
||
| 77 | |||
| 78 | |||
| 79 | $closingDateParser = new ClosingDate($timezone); |
||
| 80 | $cfp->dateEnd = $closingDateParser->parse($dom, $xpath); |
||
| 81 | |||
| 82 | $eventPageDom = $this->getEventPage($xpath); |
||
| 83 | $eventXpath = new \DOMXPath(($eventPageDom)); |
||
| 84 | |||
| 85 | $descriptionParser = new Description(); |
||
| 86 | $cfp->description = $descriptionParser->parse($dom, $xpath); |
||
| 87 | |||
| 88 | $openingDateParser = new OpeningDate($timezone); |
||
| 89 | try { |
||
| 90 | $cfp->dateStart = $openingDateParser->parse($dom, $xpath); |
||
| 91 | } catch (\Exception $e) {} |
||
| 92 | |||
| 93 | $cfpUriParser = new Uri(); |
||
| 94 | $cfp->uri = $cfpUriParser->parse($dom, $xpath); |
||
| 95 | |||
| 96 | $confNameParser = new EventName(); |
||
| 97 | $cfp->conferenceName = $confNameParser->parse($dom, $xpath); |
||
| 98 | |||
| 99 | $confUriParser = new EventUri(); |
||
| 100 | $cfp->conferenceUri = $confUriParser->parse($eventPageDom, $eventXpath); |
||
| 101 | |||
| 102 | $eventStartDate = new EventStartDate($timezone); |
||
| 103 | $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath); |
||
| 104 | |||
| 105 | try { |
||
| 106 | $eventEndDate = new EventEndDate($timezone); |
||
| 107 | $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath); |
||
| 108 | } catch (\InvalidArgumentException $e) { |
||
| 109 | $cfp->eventEndDate = $cfp->eventStartDate; |
||
| 110 | } |
||
| 111 | |||
| 112 | $eventLocation = new Location(); |
||
| 113 | $cfp->location = $eventLocation->parse($dom, $xpath); |
||
| 114 | |||
| 115 | try { |
||
| 116 | $location = $this->getLatLonForLocation($cfp->location); |
||
| 117 | $cfp->latitude = $location[0]; |
||
| 118 | $cfp->longitude = $location[1]; |
||
| 119 | } catch (\UnexpectedValueException $e) { |
||
| 120 | error_log($e->getMessage()); |
||
| 121 | } |
||
| 122 | |||
| 123 | try { |
||
| 124 | $tags = new Tags(); |
||
| 125 | $cfp->tags = $tags->parse($eventPageDom, $eventXpath); |
||
| 126 | } catch (\InvalidArgumentException $e) { |
||
| 127 | $cfp->tags = []; |
||
| 128 | } |
||
| 129 | |||
| 130 | return $cfp; |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | throw $e; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 171 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: