fix: SA: Fix "...Access to an undefined property..." rule.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera 2021-11-16 16:37:45 +01:00
parent 6cfcf91757
commit db2010082a
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
2 changed files with 18 additions and 32 deletions

View File

@ -195,11 +195,6 @@ parameters:
count: 2 count: 2
path: src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCalendarRange.php path: src/Bundle/ChillCalendarBundle/DataFixtures/ORM/LoadCalendarRange.php
-
message: "#^Access to an undefined property Chill\\\\CustomFieldsBundle\\\\Form\\\\DataTransformer\\\\JsonCustomFieldToArrayTransformer\\:\\:\\$customField\\.$#"
count: 3
path: src/Bundle/ChillCustomFieldsBundle/Form/DataTransformer/JsonCustomFieldToArrayTransformer.php
- -
message: "#^Call to an undefined method Chill\\\\ThirdPartyBundle\\\\Form\\\\Type\\\\PickThirdPartyTypeCategoryType\\:\\:transform\\(\\)\\.$#" message: "#^Call to an undefined method Chill\\\\ThirdPartyBundle\\\\Form\\\\Type\\\\PickThirdPartyTypeCategoryType\\:\\:transform\\(\\)\\.$#"
count: 1 count: 1

View File

@ -1,28 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace Chill\CustomFieldsBundle\Form\DataTransformer; namespace Chill\CustomFieldsBundle\Form\DataTransformer;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface { class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
/** private ObjectManager $om;
* @var ObjectManager
*/ private array $customField;
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om) public function __construct(ObjectManager $om)
{ {
$this->om = $om; $this->om = $om;
$customFields = $this->om $customFields = $this->om
->getRepository('ChillCustomFieldsBundle:CustomField') ->getRepository(CustomField::class)
->findAll(); ->findAll();
// @TODO: in the array_map callback, CustomField::getLabel() does not exist. What do we do here?
$customFieldsLablels = array_map( $customFieldsLablels = array_map(
function($e) { return $e->getLabel(); }, function($e) { return $e->getLabel(); },
$customFields); $customFields);
@ -36,20 +35,12 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
{ {
echo $customFieldsJSON; echo $customFieldsJSON;
if($customFieldsJSON === null) { // lors de la creation if($customFieldsJSON === null) {
$customFieldsArray = array(); $customFieldsArray = [];
} else { } else {
$customFieldsArray = json_decode($customFieldsJSON,true); $customFieldsArray = json_decode($customFieldsJSON, true, 512, JSON_THROW_ON_ERROR);
} }
/*
echo "<br> - 4 - <br>";
var_dump($customFieldsArray);
echo "<br> - 5 - <br>";
*/
$customFieldsArrayRet = array(); $customFieldsArrayRet = array();
foreach ($customFieldsArray as $key => $value) { foreach ($customFieldsArray as $key => $value) {
@ -62,7 +53,7 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
} else { } else {
$entityClass = substr($type, 10, -1); $entityClass = substr($type, 10, -1);
} }
$customFieldsArrayRet[$key] = $this->om $customFieldsArrayRet[$key] = $this->om
->getRepository('ChillCustomFieldsBundle:' . $entityClass) ->getRepository('ChillCustomFieldsBundle:' . $entityClass)
->findOneById($value); ->findOneById($value);
@ -86,10 +77,10 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
{ {
/* /*
echo "<br> - - 7 - <br>"; echo "<br> - - 7 - <br>";
var_dump(array_keys($customFieldsArray)); var_dump(array_keys($customFieldsArray));
echo "<br> - - 8 - <br>"; echo "<br> - - 8 - <br>";
var_dump(array_keys($this->customField)); var_dump(array_keys($this->customField));
@ -112,7 +103,7 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
//$entityClass = substr($type, 10, -1); //$entityClass = substr($type, 10, -1);
//echo $entityClasss; //echo $entityClasss;
if(strpos($type, 'ManyToOnePersist') === 0) { if(strpos($type, 'ManyToOnePersist') === 0) {
// PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien... // PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien...
// //
// //
$this->om->persist($value); // pas bon ici $this->om->persist($value); // pas bon ici
@ -121,7 +112,7 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
// et faire le persist qd fait sur l'obj parent // et faire le persist qd fait sur l'obj parent
// regarder : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html // regarder : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
// ou : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html // ou : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
// dans yml : // dans yml :
// lifecycleCallbacks: // lifecycleCallbacks:
// prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ] // prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ]
$this->om->flush(); // sinon l'id pose pbm $this->om->flush(); // sinon l'id pose pbm
@ -142,4 +133,4 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
return json_encode($customFieldsArrayRet); return json_encode($customFieldsArrayRet);
} }
} }