| Total Complexity | 43 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Event often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Event, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Event implements Resource |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Resource Payload. |
||
| 12 | * |
||
| 13 | * @var object |
||
| 14 | */ |
||
| 15 | private $data; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class Constructor. |
||
| 19 | * |
||
| 20 | * @param string $fullName |
||
| 21 | * @param boolean $init |
||
| 22 | */ |
||
| 23 | public function __construct(string $fullName, bool $init = true) |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Factory Method. |
||
| 33 | * |
||
| 34 | * @param object $object |
||
| 35 | * @return \LiveStream\Resources\Event|null |
||
| 36 | */ |
||
| 37 | public static function fromObject(?object $object): ?Event |
||
| 38 | { |
||
| 39 | if ($object == null) return null; |
||
| 40 | |||
| 41 | $instance = new static(false); |
||
| 42 | $instance->data = $object; |
||
| 43 | return $instance; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Magic Setter Method |
||
| 48 | * |
||
| 49 | * @param string $key |
||
| 50 | * @param mixed $value |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public function __set(string $key, $value): void |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Magic Getter Method |
||
| 60 | * |
||
| 61 | * @param string $key |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function __get(string $key) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Magic Method Isset. |
||
| 71 | * |
||
| 72 | * @param string $key |
||
| 73 | * @return boolean |
||
| 74 | */ |
||
| 75 | public function __isset(string $key) |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set Full Name. |
||
| 82 | * |
||
| 83 | * @param string $fullName |
||
| 84 | * @return \LiveStream\Resources\Event |
||
| 85 | */ |
||
| 86 | public function setFullName(string $fullName): Event |
||
| 87 | { |
||
| 88 | $this->data->fullName = $fullName; |
||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get Full Name. |
||
| 94 | * |
||
| 95 | * @return string|null |
||
| 96 | */ |
||
| 97 | public function getFullName(): ?string |
||
| 98 | { |
||
| 99 | return $this->data->fullName ?? null; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set Event Start Time. |
||
| 104 | * |
||
| 105 | * @param string $strtime |
||
| 106 | * @return \LiveStream\Resources\Event |
||
| 107 | */ |
||
| 108 | public function setStartTime(string $strtime): Event |
||
| 109 | { |
||
| 110 | $this->data->startTime = date('c', strtotime($strtime)); |
||
| 111 | return $this; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get Start Time |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getStartTime(): ?string |
||
| 120 | { |
||
| 121 | return $this->data->startTime ?? null; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set End Time |
||
| 126 | * |
||
| 127 | * @param string $strtime |
||
| 128 | * @return \LiveStream\Resources\Event |
||
| 129 | */ |
||
| 130 | public function setEndTime(string $strtime): Event |
||
| 131 | { |
||
| 132 | $this->data->endTime = date('c', strtotime($strtime)); |
||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get End Time. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getEndTime(): ?string |
||
| 142 | { |
||
| 143 | return $this->data->endTime ?? null; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set Is Draft. |
||
| 148 | * |
||
| 149 | * @param boolean $isDraft |
||
| 150 | * |
||
| 151 | * @return \LiveStream\Resources\Event |
||
| 152 | */ |
||
| 153 | public function setIsDraft(bool $isDraft = true): Event |
||
| 154 | { |
||
| 155 | $this->data->draft = $isDraft; |
||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get Is Draft. |
||
| 161 | * |
||
| 162 | * @return boolean |
||
| 163 | */ |
||
| 164 | public function isDraft(): bool |
||
| 165 | { |
||
| 166 | return $this->data->draft ?? true; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set Event Short Name. |
||
| 171 | * |
||
| 172 | * @param string $shortName |
||
| 173 | * @return Event |
||
| 174 | */ |
||
| 175 | public function setShortName(string $shortName): Event |
||
| 176 | { |
||
| 177 | $this->data->shortName = $shortName; |
||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get Event Short Name. |
||
| 183 | * |
||
| 184 | * @return string|null |
||
| 185 | */ |
||
| 186 | public function getShortName(): ?string |
||
| 187 | { |
||
| 188 | return $this->data->shortName ?? null; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set Event Description |
||
| 193 | * |
||
| 194 | * @param string $description |
||
| 195 | * @return Event |
||
| 196 | */ |
||
| 197 | public function setDescription(string $description): Event |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get Description. |
||
| 205 | * |
||
| 206 | * @return string|null |
||
| 207 | */ |
||
| 208 | public function getDescription(): ?string |
||
| 209 | { |
||
| 210 | return $this->data->description ?? null; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Add Event Tag |
||
| 215 | * |
||
| 216 | * @param string $tag |
||
| 217 | * @return \LiveStream\Resources\Event |
||
| 218 | */ |
||
| 219 | public function addTag(string $tag): Event |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get Tags. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getTags(): string |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Resource Interface Method: Get Resource as JSON String. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getResourceBody(): string |
||
| 244 | { |
||
| 245 | $body = ['fullName' => $this->data->fullName]; |
||
| 246 | |||
| 247 | if ($this->data->shortName ?? null) |
||
| 248 | $body['shortName'] = $this->data->shortName; |
||
| 249 | |||
| 250 | if ($this->data->startTime ?? null) |
||
| 251 | $body['startTime'] = $this->data->startTime; |
||
| 252 | |||
| 253 | if ($this->data->endTime ?? null) |
||
| 254 | $body['endTime'] = $this->data->endTime; |
||
| 255 | |||
| 256 | if ($this->data->draft ?? null) |
||
| 257 | $body['draft'] = $this->data->draft; |
||
| 258 | |||
| 259 | if ($this->data->description ?? null) |
||
| 260 | $body['description'] = $this->data->description; |
||
| 261 | |||
| 262 | if ($this->data->tags ?? null) |
||
| 263 | $body['tags'] = rtrim($this->data->tags, ','); |
||
| 264 | |||
| 265 | /* TODO: Getters & Setters */ |
||
| 266 | if ($this->data->isPublic ?? null) $body['isPublic'] = $this->data->isPublic; |
||
| 267 | if ($this->data->isSearchable ?? null) $body['isSearchable'] = $this->data->isSearchable; |
||
| 268 | if ($this->data->viewerCountVisible ?? null) $body['viewerCountVisible'] = $this->data->viewerCountVisible; |
||
| 269 | if ($this->data->postCommentsEnabled ?? null) $body['postCommentsEnabled'] = $this->data->postCommentsEnabled; |
||
| 270 | if ($this->data->liveChatEnabled ?? null) $body['liveChatEnabled'] = $this->data->liveChatEnabled; |
||
| 271 | if ($this->data->isEmbeddable ?? null) $body['isEmbeddable'] = $this->data->isEmbeddable; |
||
| 272 | if ($this->data->isPasswordProtected ?? null) $body['isPasswordProtected'] = $this->data->isPasswordProtected; |
||
| 273 | if ($this->data->password ?? null) $body['password'] = $this->data->password; |
||
| 274 | if ($this->data->isWhiteLabeled ?? null) $body['isWhiteLabeled'] = $this->data->isWhiteLabeled; |
||
| 275 | if ($this->data->embedRestriction ?? null && in_array($this->data->embedRestriction, ['off', 'whitelist', 'blacklist'])) $body['embedRestriction'] = $this->data->embedRestriction; |
||
| 276 | if ($this->data->embedRestrictionWhitelist ?? null) $body['embedRestrictionWhitelist'] = $this->data->embedRestrictionWhitelist; |
||
| 277 | if ($this->data->embedRestrictionBlacklist ?? null) $body['embedRestrictionBlacklist'] = $this->data->embedRestrictionBlacklist; |
||
| 278 | |||
| 279 | return json_encode($body); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Undocumented function |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getContentType(): string |
||
| 290 | } |
||
| 291 | } |
||
| 292 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: