| Total Complexity | 58 |
| Total Lines | 406 |
| Duplicated Lines | 0 % |
| Changes | 31 | ||
| Bugs | 0 | Features | 0 |
Complex classes like People 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 People, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | #[ORM\Table(name: 'people')] |
||
| 34 | #[ORM\Entity(repositoryClass: PeopleRepository::class)] |
||
| 35 | #[ApiResource( |
||
| 36 | formats: ['jsonld', 'json', 'html', 'jsonhal', 'csv' => 'text/csv'], |
||
| 37 | normalizationContext: ['groups' => ['people:read']], |
||
| 38 | denormalizationContext: ['groups' => ['people:write']], |
||
| 39 | security: "is_granted('ROLE_CLIENT')", |
||
| 40 | operations: [ |
||
| 41 | new GetCollection(securityPostDenormalize: "is_granted('ROLE_CLIENT')"), |
||
| 42 | new GetCollection( |
||
| 43 | uriTemplate: '/people/company/default', |
||
| 44 | controller: \ControleOnline\Controller\GetDefaultCompanyAction::class, |
||
| 45 | security: "is_granted('PUBLIC_ACCESS')" |
||
| 46 | ), |
||
| 47 | new GetCollection( |
||
| 48 | uriTemplate: '/people/companies/my', |
||
| 49 | controller: \ControleOnline\Controller\GetMyCompaniesAction::class, |
||
| 50 | security: "is_granted('ROLE_CLIENT')" |
||
| 51 | ), |
||
| 52 | new Get(security: "is_granted('PUBLIC_ACCESS')"), |
||
| 53 | new Post(securityPostDenormalize: "is_granted('ROLE_CLIENT')"), |
||
| 54 | new Put( |
||
| 55 | security: "is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')", |
||
| 56 | validationContext: ['groups' => ['people:write']], |
||
| 57 | denormalizationContext: ['groups' => ['people:write']] |
||
| 58 | ), |
||
| 59 | new Delete(security: "is_granted('ROLE_CLIENT')") |
||
| 60 | ] |
||
| 61 | )] |
||
| 62 | #[ApiFilter(CustomOrFilter::class, properties: ['name', 'id', 'alias'])] |
||
| 63 | #[ApiFilter(SearchFilter::class, properties: [ |
||
| 64 | 'id' => 'exact', |
||
| 65 | 'enable' => 'exact', |
||
| 66 | 'name' => 'partial', |
||
| 67 | 'alias' => 'partial', |
||
| 68 | 'peopleType' => 'exact', |
||
| 69 | 'link.link_type' => 'exact', |
||
| 70 | 'link.company' => 'exact', |
||
| 71 | 'link.people' => 'exact', |
||
| 72 | 'user' => 'exact', |
||
| 73 | 'document' => 'exact', |
||
| 74 | 'address' => 'exact', |
||
| 75 | 'phone' => 'exact', |
||
| 76 | 'email' => 'exact' |
||
| 77 | ])] |
||
| 78 | class People |
||
| 79 | { |
||
| 80 | #[ORM\Column(type: 'integer')] |
||
| 81 | #[ORM\Id] |
||
| 82 | #[ORM\GeneratedValue(strategy: 'IDENTITY')] |
||
| 83 | #[Groups(['people:read', 'people:write'])] |
||
| 84 | private $id; |
||
| 85 | |||
| 86 | #[ORM\Column(type: 'boolean')] |
||
| 87 | #[Groups(['people:read', 'people:write'])] |
||
| 88 | private $enable = 0; |
||
| 89 | |||
| 90 | #[ORM\Column(type: 'string', length: 50)] |
||
| 91 | #[Groups(['people:read', 'people:write'])] |
||
| 92 | private $name = ''; |
||
| 93 | |||
| 94 | #[ORM\Column(type: 'datetime', columnDefinition: 'DATETIME')] |
||
| 95 | private $registerDate; |
||
| 96 | |||
| 97 | #[ORM\Column(type: 'string', length: 50)] |
||
| 98 | #[Groups(['people:read', 'people:write'])] |
||
| 99 | private $alias = ''; |
||
| 100 | |||
| 101 | #[ORM\Column(name: 'other_informations', type: 'json', nullable: true)] |
||
| 102 | #[Groups(['people:read', 'people:write'])] |
||
| 103 | private $otherInformations; |
||
| 104 | |||
| 105 | #[ORM\Column(type: 'string', length: 1)] |
||
| 106 | #[Groups(['people:read', 'people:write'])] |
||
| 107 | private $peopleType = 'F'; |
||
| 108 | |||
| 109 | #[ORM\ManyToOne(targetEntity: File::class, inversedBy: 'people')] |
||
| 110 | #[ORM\JoinColumn(name: 'image_id', referencedColumnName: 'id')] |
||
| 111 | #[Groups(['people:read', 'people:write'])] |
||
| 112 | private $image; |
||
| 113 | |||
| 114 | #[ORM\OneToMany(targetEntity: Config::class, mappedBy: 'people')] |
||
| 115 | private $config; |
||
| 116 | |||
| 117 | #[ORM\ManyToOne(targetEntity: File::class)] |
||
| 118 | #[ORM\JoinColumn(name: 'alternative_image', referencedColumnName: 'id')] |
||
| 119 | #[Groups(['people:read', 'people:write'])] |
||
| 120 | private $alternative_image; |
||
| 121 | |||
| 122 | #[ORM\ManyToOne(targetEntity: File::class)] |
||
| 123 | #[ORM\JoinColumn(name: 'background_image', referencedColumnName: 'id')] |
||
| 124 | #[Groups(['people:read', 'people:write'])] |
||
| 125 | private $background; |
||
| 126 | |||
| 127 | #[ORM\ManyToOne(targetEntity: Language::class, inversedBy: 'people')] |
||
| 128 | #[ORM\JoinColumn(name: 'language_id', referencedColumnName: 'id')] |
||
| 129 | private $language; |
||
| 130 | |||
| 131 | #[ORM\OneToMany(targetEntity: PeopleLink::class, mappedBy: 'company')] |
||
| 132 | private $company; |
||
| 133 | |||
| 134 | #[ORM\OneToMany(targetEntity: PeopleLink::class, mappedBy: 'people')] |
||
| 135 | private $link; |
||
| 136 | |||
| 137 | #[ORM\OneToMany(targetEntity: User::class, mappedBy: 'people')] |
||
| 138 | #[Groups(['people:read', 'people:write'])] |
||
| 139 | private $user; |
||
| 140 | |||
| 141 | #[ORM\OneToMany(targetEntity: Document::class, mappedBy: 'people')] |
||
| 142 | #[Groups(['people:read', 'people:write'])] |
||
| 143 | private $document; |
||
| 144 | |||
| 145 | #[ORM\OneToMany(targetEntity: CompanyDocument::class, mappedBy: 'people')] |
||
| 146 | #[Groups(['people:read', 'people:write'])] |
||
| 147 | private $company_document; |
||
| 148 | |||
| 149 | #[ORM\OneToMany(targetEntity: Address::class, mappedBy: 'people')] |
||
| 150 | #[Groups(['people:read', 'people:write'])] |
||
| 151 | private $address; |
||
| 152 | |||
| 153 | #[ORM\OneToMany(targetEntity: Phone::class, mappedBy: 'people')] |
||
| 154 | #[Groups(['people:read', 'people:write'])] |
||
| 155 | private $phone; |
||
| 156 | |||
| 157 | #[ORM\OneToMany(targetEntity: Email::class, mappedBy: 'people')] |
||
| 158 | #[Groups(['people:read', 'people:write'])] |
||
| 159 | private $email; |
||
| 160 | |||
| 161 | #[ORM\Column(type: 'datetime', columnDefinition: 'DATETIME', nullable: false)] |
||
| 162 | #[Groups(['people:read', 'people:write'])] |
||
| 163 | private $foundationDate = null; |
||
| 164 | |||
| 165 | public function __construct() |
||
| 166 | { |
||
| 167 | $this->enable = 0; |
||
| 168 | $this->registerDate = new DateTime('now'); |
||
| 169 | $this->company = new ArrayCollection(); |
||
| 170 | $this->config = new ArrayCollection(); |
||
| 171 | $this->link = new ArrayCollection(); |
||
| 172 | $this->user = new ArrayCollection(); |
||
| 173 | $this->document = new ArrayCollection(); |
||
| 174 | $this->address = new ArrayCollection(); |
||
| 175 | $this->email = new ArrayCollection(); |
||
| 176 | $this->phone = new ArrayCollection(); |
||
| 177 | $this->otherInformations = json_encode(new stdClass()); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getId() |
||
| 181 | { |
||
| 182 | return $this->id; |
||
| 183 | } |
||
| 184 | public function getEnabled() |
||
| 185 | { |
||
| 186 | return $this->enable; |
||
| 187 | } |
||
| 188 | public function setEnabled($enable) |
||
| 189 | { |
||
| 190 | $this->enable = $enable ?: 0; |
||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function setPeopleType($people_type) |
||
| 195 | { |
||
| 196 | $this->peopleType = $people_type; |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | public function getPeopleType() |
||
| 200 | { |
||
| 201 | return $this->peopleType; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function setName(string $name): self |
||
| 205 | { |
||
| 206 | $this->name = $name; |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | public function getName(): string |
||
| 210 | { |
||
| 211 | return strtoupper((string) $this->name); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function setAlias($alias) |
||
| 215 | { |
||
| 216 | $this->alias = $alias; |
||
| 217 | return $this; |
||
| 218 | } |
||
| 219 | public function getAlias() |
||
| 220 | { |
||
| 221 | return strtoupper((string) $this->alias); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function setLanguage(Language $language = null) |
||
| 225 | { |
||
| 226 | $this->language = $language; |
||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | public function getLanguage() |
||
| 230 | { |
||
| 231 | return $this->language; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function getRegisterDate(): DateTimeInterface |
||
| 235 | { |
||
| 236 | return $this->registerDate; |
||
| 237 | } |
||
| 238 | public function setRegisterDate(DateTimeInterface $registerDate): self |
||
| 239 | { |
||
| 240 | $this->registerDate = $registerDate; |
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function addDocument(Document $document) |
||
| 245 | { |
||
| 246 | $this->document[] = $document; |
||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | public function getDocument() |
||
| 250 | { |
||
| 251 | return $this->document; |
||
| 252 | } |
||
| 253 | |||
| 254 | public function addCompany(People $company) |
||
| 255 | { |
||
| 256 | $this->company[] = $company; |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | public function removeCompany(People $company) |
||
| 260 | { |
||
| 261 | $this->company->removeElement($company); |
||
| 262 | } |
||
| 263 | public function getCompany() |
||
| 264 | { |
||
| 265 | return $this->company; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function addLink(People $link) |
||
| 269 | { |
||
| 270 | $this->link[] = $link; |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | public function removeLink(People $link) |
||
| 274 | { |
||
| 275 | $this->link->removeElement($link); |
||
| 276 | } |
||
| 277 | public function getLink() |
||
| 278 | { |
||
| 279 | return $this->link; |
||
| 280 | } |
||
| 281 | |||
| 282 | public function addUser(User $user) |
||
| 283 | { |
||
| 284 | $this->user[] = $user; |
||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | public function removeUser(User $user) |
||
| 288 | { |
||
| 289 | $this->user->removeElement($user); |
||
| 290 | } |
||
| 291 | public function getUser() |
||
| 292 | { |
||
| 293 | return $this->user; |
||
| 294 | } |
||
| 295 | |||
| 296 | public function getAddress() |
||
| 297 | { |
||
| 298 | return $this->address; |
||
| 299 | } |
||
| 300 | public function getPhone() |
||
| 301 | { |
||
| 302 | return $this->phone; |
||
| 303 | } |
||
| 304 | public function getEmail() |
||
| 305 | { |
||
| 306 | return $this->email; |
||
| 307 | } |
||
| 308 | |||
| 309 | public function getFoundationDate(): ?DateTime |
||
| 310 | { |
||
| 311 | return $this->foundationDate; |
||
| 312 | } |
||
| 313 | public function setFoundationDate(DateTimeInterface $date): self |
||
| 314 | { |
||
| 315 | $this->foundationDate = $date; |
||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | public function getFullName(): string |
||
| 320 | { |
||
| 321 | if ($this->getPeopleType() == 'F') { |
||
| 322 | return trim((string) preg_replace('/[^A-Za-z\s]/', '', sprintf('%s %s', $this->getName(), $this->getAlias()))); |
||
| 323 | } |
||
| 324 | return trim((string) preg_replace('/[^A-Za-z\s]/', '', $this->getName())); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function isPerson(): bool |
||
| 328 | { |
||
| 329 | return $this->getPeopleType() == 'F'; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function getOneEmail(): ?Email |
||
| 333 | { |
||
| 334 | if (($email = $this->getEmail()->first()) === false) { |
||
| 335 | return null; |
||
| 336 | } |
||
| 337 | return $email; |
||
| 338 | } |
||
| 339 | |||
| 340 | public function getOneDocument(): ?Document |
||
| 341 | { |
||
| 342 | $documents = $this->getDocument()->filter(function ($peopleDocument) { |
||
| 343 | if ($peopleDocument->getPeople()->getPeopleType() == 'F') { |
||
| 344 | return $peopleDocument->getDocumentType()->getDocumentType() == 'CPF'; |
||
| 345 | } |
||
| 346 | return $peopleDocument->getDocumentType()->getDocumentType() == 'CNPJ'; |
||
| 347 | }); |
||
| 348 | return ($document = $documents->first()) === false ? null : $document; |
||
| 349 | } |
||
| 350 | |||
| 351 | public function getBirthdayAsString(): ?string |
||
| 352 | { |
||
| 353 | if ($this->getFoundationDate() instanceof DateTimeInterface) { |
||
| 354 | return $this->getFoundationDate()->format('Y-m-d'); |
||
| 355 | } |
||
| 356 | return null; |
||
| 357 | } |
||
| 358 | |||
| 359 | public function getOtherInformations($decode = false) |
||
| 364 | } |
||
| 365 | |||
| 366 | public function addOtherInformations($key, $value) |
||
| 367 | { |
||
| 368 | $otherInformations = $this->getOtherInformations(true); |
||
| 369 | $otherInformations->{$key} = $value; |
||
| 370 | $this->otherInformations = json_encode($otherInformations); |
||
| 371 | return $this; |
||
| 372 | } |
||
| 373 | |||
| 374 | public function setOtherInformations(stdClass $otherInformations) |
||
| 375 | { |
||
| 376 | $this->otherInformations = json_encode($otherInformations); |
||
| 377 | return $this; |
||
| 378 | } |
||
| 379 | |||
| 380 | public function addConfig(Config $config) |
||
| 381 | { |
||
| 382 | $this->config[] = $config; |
||
| 383 | return $this; |
||
| 384 | } |
||
| 385 | public function removeConfig(Config $config) |
||
| 386 | { |
||
| 387 | $this->config->removeElement($config); |
||
| 388 | } |
||
| 389 | public function getConfig() |
||
| 390 | { |
||
| 391 | return $this->config; |
||
| 392 | } |
||
| 393 | |||
| 394 | public function getBackground() |
||
| 397 | } |
||
| 398 | public function setBackground($background): self |
||
| 399 | { |
||
| 400 | $this->background = $background; |
||
| 401 | return $this; |
||
| 402 | } |
||
| 403 | |||
| 404 | public function getImage() |
||
| 405 | { |
||
| 406 | return $this->image; |
||
| 407 | } |
||
| 408 | public function setImage($image): self |
||
| 409 | { |
||
| 410 | $this->image = $image; |
||
| 411 | return $this; |
||
| 412 | } |
||
| 413 | |||
| 414 | public function getAlternativeImage() |
||
| 415 | { |
||
| 416 | return $this->alternative_image; |
||
| 417 | } |
||
| 418 | public function setAlternativeImage($alternative_image): self |
||
| 422 | } |
||
| 423 | |||
| 424 | public function getCompanyDocument() |
||
| 425 | { |
||
| 426 | return $this->company_document; |
||
| 427 | } |
||
| 428 | |||
| 429 | public function addCompanyDocument(CompanyDocument $doc) |
||
| 433 | } |
||
| 434 | |||
| 435 | public function removeCompanyDocument(CompanyDocument $doc) |
||
| 436 | { |
||
| 437 | $this->company_document->removeElement($doc); |
||
| 438 | return $this; |
||
| 439 | } |
||
| 440 | } |
||
| 441 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths