Refactored code to use PHP8 attributes instead of annotations

In this change, Doctrine and validation annotations have been replaced with PHP8 Attributes. The Rector tool has been configured with a list of annotations to convert to attributes. As a consequence, the PHPStan's rules have been updated to reflect these changes. The PHP8's nullable operator (?) has been added where required, and comments in field declaration have been replaced with #[Attribute] syntax.
This commit is contained in:
2024-04-08 12:11:29 +02:00
parent 0ff4593863
commit 5be85a4fc6
35 changed files with 164 additions and 531 deletions

View File

@@ -25,7 +25,6 @@ use Chill\MainBundle\Entity\HasScopesInterface;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Validator\Constraints\Entity\UserCircleConsistency;
use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodLinkedWithSocialIssuesEntityInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
@@ -42,19 +41,12 @@ use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Activity.
*
* @ActivityValidator\ActivityValidity
*
* TODO see if necessary
* UserCircleConsistency(
* "CHILL_ACTIVITY_SEE_DETAILS",
* getUserFunction="getUser",
* path="scope")
*/
#[DiscriminatorMap(typeProperty: 'type', mapping: ['activity' => Activity::class])]
#[ORM\Entity(repositoryClass: \Chill\ActivityBundle\Repository\ActivityRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[ORM\Table(name: 'activity')]
#[ActivityValidator\ActivityValidity] // TODO see if necessary
class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterface, HasCentersInterface, HasScopesInterface, TrackCreationInterface, TrackUpdateInterface
{
use TrackCreationTrait;

View File

@@ -13,9 +13,7 @@ namespace Chill\ActivityBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ActivityValidity extends Constraint
{
final public const IS_REQUIRED_MESSAGE = ' is required';

View File

@@ -13,7 +13,6 @@ namespace Chill\CustomFieldsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\Mapping as ORM;
/**
@@ -36,7 +35,7 @@ class CustomFieldsGroup
* @var Collection<CustomField>
*/
#[ORM\OneToMany(targetEntity: CustomField::class, mappedBy: 'customFieldGroup')]
#[ORM\OrderBy(['ordering' => 'ASC'])]
#[ORM\OrderBy(['ordering' => \Doctrine\Common\Collections\Criteria::ASC])]
private Collection $customFields;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255)]

View File

@@ -28,13 +28,10 @@ use Symfony\Component\Serializer\Annotation as Serializer;
*
* The property `$deleteAt` allow a deletion of the document after the given date. But this property should
* be set before the document is actually written by the StoredObjectManager.
*
* @AsyncFileExists(
* message="The file is not stored properly"
* )
*/
#[ORM\Entity]
#[ORM\Table('chill_doc.stored_object')]
#[AsyncFileExists(message: 'The file is not stored properly')]
class StoredObject implements Document, TrackCreationInterface
{
use TrackCreationTrait;

View File

@@ -13,13 +13,17 @@ namespace Chill\DocStoreBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class AsyncFileExists extends Constraint
{
public string $message = "The file '{{ filename }}' is not stored properly.";
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, $payload = null)
{
parent::__construct($options ?? [], $groups, $payload);
$this->message = $message;
}
public function validatedBy()
{
return AsyncFileExistsValidator::class;

View File

@@ -65,18 +65,14 @@ class Location implements TrackCreationInterface, TrackUpdateInterface
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255, nullable: true)]
private ?string $name = null;
/**
* @PhonenumberConstraint(type="any")
*/
#[Serializer\Groups(['read', 'write', 'docgen:read'])]
#[ORM\Column(type: 'phone_number', nullable: true)]
#[PhonenumberConstraint(type: 'any')]
private ?PhoneNumber $phonenumber1 = null;
/**
* @PhonenumberConstraint(type="any")
*/
#[Serializer\Groups(['read', 'write', 'docgen:read'])]
#[ORM\Column(type: 'phone_number', nullable: true)]
#[PhonenumberConstraint(type: 'any')]
private ?PhoneNumber $phonenumber2 = null;
#[Serializer\Groups(['read'])]

View File

@@ -14,7 +14,6 @@ namespace Chill\MainBundle\Entity;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -58,7 +57,7 @@ class Notification implements TrackUpdateInterface
* @var Collection<NotificationComment>
*/
#[ORM\OneToMany(targetEntity: NotificationComment::class, mappedBy: 'notification', orphanRemoval: true)]
#[ORM\OrderBy(['createdAt' => 'ASC'])]
#[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::ASC])]
private Collection $comments;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE)]

View File

@@ -111,10 +111,9 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter
/**
* The user's mobile phone number.
*
* @PhonenumberConstraint()
*/
#[ORM\Column(type: 'phone_number', nullable: true)]
#[PhonenumberConstraint]
private ?PhoneNumber $phonenumber = null;
/**

View File

@@ -24,12 +24,10 @@ use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @EntityWorkflowCreation(groups={"creation"})
*/
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['entity_workflow' => EntityWorkflow::class])]
#[ORM\Entity]
#[ORM\Table('chill_main_workflow_entity')]
#[EntityWorkflowCreation(groups: ['creation'])]
class EntityWorkflow implements TrackCreationInterface, TrackUpdateInterface
{
use TrackCreationTrait;

View File

@@ -102,7 +102,7 @@ class SortExportElementTest extends KernelTestCase
private function makeTranslator(): TranslatorInterface
{
return new class () implements TranslatorInterface {
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null)
{
return $id;
}

View File

@@ -13,9 +13,7 @@ namespace Chill\MainBundle\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
class PhonenumberConstraint extends Constraint
{
public $notLandlineMessage = 'This is not a landline phonenumber';
@@ -29,7 +27,13 @@ class PhonenumberConstraint extends Constraint
*
* @var string 'landline', 'mobile' or 'any'
*/
public $type;
public string $type;
public function __construct(?array $options = null, ?string $type = null, ?array $groups = null, $payload = null)
{
parent::__construct($options ?? [], $groups, $payload);
$this->type = $type ?? 'any';
}
public function validatedBy()
{

View File

@@ -13,9 +13,7 @@ namespace Chill\MainBundle\Validator\Constraints\Entity;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class UserCircleConsistency extends Constraint
{
public $getUserFunction = 'getUser';

View File

@@ -17,9 +17,8 @@ namespace Chill\MainBundle\Workflow\Validator;
* * a handler exists;
* * a related entity does exists;
* * a workflow can be associated with this entity.
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class EntityWorkflowCreation extends \Symfony\Component\Validator\Constraint
{
public string $messageEntityNotFound = 'Related entity is not found';

View File

@@ -53,17 +53,14 @@ use UnexpectedValueException;
/**
* AccompanyingPeriod Class.
*
* @AccompanyingPeriodValidity(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*
* @LocationValidity(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*
* @ConfidentialCourseMustHaveReferrer(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*/
#[DiscriminatorMap(typeProperty: 'type', mapping: ['accompanying_period' => AccompanyingPeriod::class])]
#[Assert\GroupSequenceProvider]
#[ORM\Entity]
#[ORM\Table(name: 'chill_person_accompanying_period')]
#[AccompanyingPeriodValidity(groups: [AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED])]
#[LocationValidity(groups: [AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED])]
#[ConfidentialCourseMustHaveReferrer(groups: [AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED])]
class AccompanyingPeriod implements
GroupSequenceProviderInterface,
HasCentersInterface,
@@ -215,12 +212,11 @@ class AccompanyingPeriod implements
private ?Origin $origin = null;
/**
* @ParticipationOverlap(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*
* @var Collection<AccompanyingPeriodParticipation>
*/
#[Groups(['read', 'docgen:read'])]
#[ORM\OneToMany(targetEntity: AccompanyingPeriodParticipation::class, mappedBy: 'accompanyingPeriod', orphanRemoval: true, cascade: ['persist', 'refresh', 'remove', 'merge', 'detach'])]
#[ParticipationOverlap(groups: [AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED])]
private Collection $participations;
#[ORM\ManyToOne(targetEntity: Person::class, inversedBy: 'periodLocatedOn')]
@@ -251,11 +247,10 @@ class AccompanyingPeriod implements
/**
* @var Collection<resource>
*
* @ResourceDuplicateCheck(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED, "Default", "default"})
*/
#[Groups(['read', 'docgen:read'])]
#[ORM\OneToMany(targetEntity: AccompanyingPeriod\Resource::class, mappedBy: 'accompanyingPeriod', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ResourceDuplicateCheck(groups: [AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED, 'Default', 'default'])]
private Collection $resources;
/**

View File

@@ -24,7 +24,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Order;
use Doctrine\Common\Collections\ReadableCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
@@ -47,7 +46,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues
*/
#[Serializer\Groups(['read', 'docgen:read'])]
#[ORM\OneToMany(targetEntity: AccompanyingPeriodWorkEvaluation::class, mappedBy: 'accompanyingPeriodWork', cascade: ['remove', 'persist'], orphanRemoval: true)]
#[ORM\OrderBy(['startDate' => 'DESC', 'id' => 'DESC'])]
#[ORM\OrderBy(['startDate' => \Doctrine\Common\Collections\Criteria::DESC, 'id' => 'DESC'])]
private Collection $accompanyingPeriodWorkEvaluations;
#[Serializer\Groups(['read', 'docgen:read', 'read:accompanyingPeriodWork:light'])]

View File

@@ -25,12 +25,10 @@ use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @MaxHolder(groups={"household_memberships"})
*/
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['household' => Household::class])]
#[ORM\Entity]
#[ORM\Table(name: 'chill_person_household')]
#[MaxHolder(groups: ['household_memberships'])]
class Household
{
/**

View File

@@ -47,12 +47,6 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Person Class.
*
* @PersonHasCenter
*
* @HouseholdMembershipSequential(
* groups={"household_memberships"}
* )
*/
#[DiscriminatorMap(typeProperty: 'type', mapping: ['person' => Person::class])]
#[ORM\Entity]
@@ -60,6 +54,8 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Index(name: 'person_birthdate', columns: ['birthdate'])] // @ORM\HasLifecycleCallbacks
#[ORM\Table(name: 'chill_person_person')]
#[ORM\HasLifecycleCallbacks]
#[PersonHasCenter]
#[HouseholdMembershipSequential(groups: ['household_memberships'])]
class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateInterface, \Stringable
{
final public const BOTH_GENDER = 'both';
@@ -122,10 +118,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/**
* The person's birthdate.
*
* @Birthdate
*/
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATE_MUTABLE, nullable: true)]
#[Birthdate]
private ?\DateTime $birthdate = null;
/**
@@ -316,10 +311,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/**
* The person's mobile phone number.
*
* @PhonenumberConstraint(type="mobile")
*/
#[ORM\Column(type: 'phone_number', nullable: true)]
#[PhonenumberConstraint(type: 'mobile')]
private ?PhoneNumber $mobilenumber = null;
/**
@@ -350,12 +344,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/**
* The person's phonenumber.
*
* @PhonenumberConstraint(
* type="landline",
* )
*/
#[ORM\Column(type: 'phone_number', nullable: true)]
#[PhonenumberConstraint(type: 'landline')]
private ?PhoneNumber $phonenumber = null;
/**

View File

@@ -23,13 +23,11 @@ use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @RelationshipNoDuplicate
*/
#[DiscriminatorMap(typeProperty: 'type', mapping: ['relationship' => Relationship::class])]
#[ORM\Entity]
#[DiscriminatorColumn(name: 'relation_id', type: 'integer')]
#[ORM\Table(name: 'chill_person_relationships')]
#[RelationshipNoDuplicate]
class Relationship implements TrackCreationInterface, TrackUpdateInterface
{
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE)]

View File

@@ -27,7 +27,7 @@ class ByStepAggregatorTest extends AbstractAggregatorTest
public function getAggregator()
{
$translator = new class () implements TranslatorInterface {
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null)
{
return $id;
}

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class AccompanyingPeriodValidity extends Constraint
{
public $messageSocialIssueCannotBeDeleted = 'The social %name% issue cannot be deleted because it is associated with an activity or an action';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ConfidentialCourseMustHaveReferrer extends Constraint
{
public string $message = 'A confidential parcours must have a referrer';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class LocationValidity extends Constraint
{
public $messagePeriodMustRemainsLocated = 'The period must remain located';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ParticipationOverlap extends Constraint
{
public $message = '{{ name }} is already associated to this accompanying course.';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ResourceDuplicateCheck extends Constraint
{
public $message = '{{ name }} is already associated to this accompanying course.';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\Household;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class HouseholdMembershipSequential extends Constraint
{
public $message = 'household_membership.Person with membership covering';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\Household;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class MaxHolder extends Constraint
{
public $message = 'household.max_holder_overflowed';

View File

@@ -20,9 +20,8 @@ use Symfony\Component\Validator\Constraint;
* interval_spec : http://php.net/manual/en/dateinterval.construct.php
* (this interval_spec itself is based on ISO8601 :
* https://en.wikipedia.org/wiki/ISO_8601#Durations)
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Birthdate extends Constraint
{
final public const BIRTHDATE_INVALID_CODE = '3f42fd96-0b2d-11ec-8cf3-0f3b1b1ca1c4';

View File

@@ -11,9 +11,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\Person;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class PersonHasCenter extends \Symfony\Component\Validator\Constraint
{
public string $message = 'A center is required';

View File

@@ -13,9 +13,7 @@ namespace Chill\PersonBundle\Validator\Constraints\Relationship;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class RelationshipNoDuplicate extends Constraint
{
public $message = 'relationship.duplicate';

View File

@@ -206,11 +206,9 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface, \Strin
#[Context(normalizationContext: ['groups' => 'docgen:read'], groups: ['docgen:read:3party:parent'])]
private string $profession = '';
/**
* @PhonenumberConstraint(type="any")
*/
#[Groups(['read', 'write', 'docgen:read', 'docgen:read:3party:parent'])]
#[ORM\Column(name: 'telephone', type: 'phone_number', nullable: true)]
#[PhonenumberConstraint(type: 'any')]
private ?PhoneNumber $telephone = null;
#[ORM\Column(name: 'types', type: \Doctrine\DBAL\Types\Types::JSON, nullable: true)]