fix: SA: Split critical issues in its own file.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera
2021-11-16 13:55:55 +01:00
parent c68bda5c9b
commit 8ede116cf5
12 changed files with 333 additions and 380 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Household\Position;
@@ -33,14 +35,14 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
// household)
if (NULL === $data['destination']) {
return $this->denormalizeLeave($data, $type, $format, $context);
} else {
return $this->denormalizeMove($data, $type, $format, $context);
}
return $this->denormalizeMove($data, $type, $format, $context);
}
private function performChecks($data): void
{
if (NULL == $data['concerned'] ?? NULL
if (NULL == $data['concerned'] ?? NULL
&& FALSE === ·\is_array('concerned')) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
}
@@ -55,8 +57,8 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
$editor = $this->factory->createEditor(null);
foreach ($data['concerned'] as $key => $concerned) {
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
$format, $context);
@@ -80,18 +82,18 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
$householdContext = $context;
$householdContext['groups'][] = 'create';
$household = $this->denormalizer->denormalize($data['destination'], Household::class,
$household = $this->denormalizer->denormalize($data['destination'], Household::class,
$format, $householdContext);
if (NULL === $household) {
throw new Exception\InvalidArgumentException("household could not be denormalized. Impossible to process");
throw new Exception\InvalidArgumentException("household could not be denormalized. Impossible to process");
}
$editor = $this->factory->createEditor($household);
foreach ($data['concerned'] as $key => $concerned) {
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$person = $this->denormalizer->denormalize($concerned['person'] ?? null, Person::class,
$format, $context);
$position = $this->denormalizer->denormalize($concerned['position'] ?? null, Position::class,
$format, $context);
$startDate = $this->denormalizer->denormalize($concerned['start_date'] ?? null, \DateTimeImmutable::class,
@@ -110,10 +112,10 @@ class MembersEditorNormalizer implements DenormalizerInterface, DenormalizerAwar
"person, position or start_date.");
}
$editor->addMovement($startDate, $person, $position, $holder,
$editor->addMovement($startDate, $person, $position, $holder,
$comment);
}
return $editor;
}

View File

@@ -67,7 +67,7 @@ class PersonNormalizer implements
$this->centerResolverDispatcher = $centerResolverDispatcher;
}
public function normalize($person, string $format = null, array $context = array())
public function normalize($person, string $format = null, array $context = [])
{
/** @var Household $household */
$household = $person->getCurrentHousehold();
@@ -128,22 +128,44 @@ class PersonNormalizer implements
$person = new Person();
}
foreach (['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender']
as $item) {
if (\array_key_exists($item, $data)) {
$person->{'set'.\ucfirst($item)}($data[$item]);
$properties = ['firstName', 'lastName', 'phonenumber', 'mobilenumber', 'gender'];
$properties = array_filter(
$properties,
static fn (string $property): bool => array_key_exists($property, $data)
);
foreach ($properties as $item) {
$callable = [$person, sprintf('set%s', ucfirst($item))];
if (is_callable($callable)) {
$closure = \Closure::fromCallable($callable);
$closure($data[$item]);
}
}
foreach ([
$propertyToClassMapping = [
'birthdate' => \DateTime::class,
'deathdate' => \DateTime::class,
'center' => Center::class
] as $item => $class) {
if (\array_key_exists($item, $data)) {
$object = $this->denormalizer->denormalize($data[$item], $class, $format, $context);
if ($object instanceof $class) {
$person->{'set'.\ucfirst($item)}($object);
'center' => Center::class,
];
$propertyToClassMapping = array_filter(
$propertyToClassMapping,
static fn (string $item): bool => array_key_exists($item, $data)
);
foreach ($propertyToClassMapping as $item => $class) {
$object = $this->denormalizer->denormalize($data[$item], $class, $format, $context);
if ($object instanceof $class) {
$callable = [$object, sprintf('set%s', ucfirst($item))];
if (is_callable($callable)) {
$closure = \Closure::fromCallable($callable);
$closure($object);
}
}
}