Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sources/AppBundle/Accounting/Entity/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace AppBundle\Accounting\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'compta_evenement')]
class Event
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public ?int $id = null;

#[ORM\Column(name: 'evenement', length: 50, nullable: false)]
public string $name;

#[ORM\Column(type: 'datetime_immutable', nullable: true)]
public ?\DateTimeImmutable $hideInAccountingJournalAt = null;
}
31 changes: 31 additions & 0 deletions sources/AppBundle/Accounting/Entity/Repository/EventRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace AppBundle\Accounting\Entity\Repository;

use AppBundle\Accounting\Entity\Event;
use AppBundle\Doctrine\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends EntityRepository<Event>
*/
final class EventRepository extends EntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Event::class);
}

/**
* @return array<Event>
*/
public function getAllSortedByName(): array
{
return $this->createQueryBuilder('e')
->orderBy('e.name', 'asc')
->getQuery()
->execute();
}
}
5 changes: 3 additions & 2 deletions sources/AppBundle/Accounting/Entity/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class Rule
#[ORM\JoinColumn(nullable: true)]
public ?Category $category = null;

#[ORM\Column(nullable: true)]
public ?int $eventId = null;
#[ORM\OneToOne()]
#[ORM\JoinColumn(nullable: true)]
public ?Event $event = null;

#[ORM\Column(name: 'mode_regl_id', nullable: true)]
public ?int $paymentTypeId = null;
Expand Down
18 changes: 9 additions & 9 deletions sources/AppBundle/Accounting/Form/RuleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace AppBundle\Accounting\Form;

use AppBundle\Accounting\Entity\Category;
use AppBundle\Accounting\Model\Repository\EventRepository;
use AppBundle\Accounting\Entity\Event;
use AppBundle\Accounting\Entity\Repository\CategoryRepository;
use AppBundle\Accounting\Entity\Repository\EventRepository;
use AppBundle\Model\ComptaModeReglement;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
Expand All @@ -17,17 +19,12 @@
class RuleType extends AbstractType
{
public function __construct(
private readonly CategoryRepository $categoryRepository,
private readonly EventRepository $eventRepository,
) {}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$events = [];
$events[''] = null;
foreach ($this->eventRepository->getAllSortedByName() as $event) {
$events[$event->getName()] = $event->getId();
}

$builder->add('label', TextType::class, [
'label' => 'Nom de la régle',
'required' => true,
Expand Down Expand Up @@ -65,11 +62,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])->add('category', EntityType::class, [
'label' => 'Catégorie',
'class' => Category::class,
'choices' => $this->categoryRepository->getAllSortedByName(),
'choice_label' => 'name',
'required' => false,
])->add('eventId', ChoiceType::class, [
])->add('event', EntityType::class, [
'label' => 'Évènement',
'choices' => $events,
'class' => Event::class,
'choices' => $this->eventRepository->getAllSortedByName(),
'choice_label' => 'name',
'required' => false,
])->add('attachmentRequired', ChoiceType::class, [
'label' => ' Justificatif obligatoire ? ',
Expand Down
55 changes: 0 additions & 55 deletions sources/AppBundle/Accounting/Model/Event.php

This file was deleted.

60 changes: 0 additions & 60 deletions sources/AppBundle/Accounting/Model/Repository/EventRepository.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace AppBundle\Controller\Admin\Accounting\Configuration;

use AppBundle\Accounting\Form\EventType;
use AppBundle\Accounting\Model\Event;
use AppBundle\Accounting\Model\Repository\EventRepository;
use AppBundle\Accounting\Entity\Event;
use AppBundle\Accounting\Entity\Repository\EventRepository;
use AppBundle\AuditLog\Audit;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -26,7 +26,7 @@ public function __invoke(Request $request): Response
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->eventRepository->save($event);
$this->audit->log('Ajout de l\'évènement ' . $event->getName());
$this->audit->log('Ajout de l\'évènement ' . $event->name);
$this->addFlash('notice', 'L\'évènement a été ajouté');
return $this->redirectToRoute('admin_accounting_events_list');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace AppBundle\Controller\Admin\Accounting\Configuration;

use AppBundle\Accounting\Form\EventType;
use AppBundle\Accounting\Model\Repository\EventRepository;
use AppBundle\Accounting\Entity\Repository\EventRepository;
use AppBundle\AuditLog\Audit;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -20,12 +20,12 @@ public function __construct(

public function __invoke(int $id,Request $request): Response
{
$event = $this->eventRepository->get($id);
$event = $this->eventRepository->find($id);
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->eventRepository->save($event);
$this->audit->log('Modification de l\'évènement ' . $event->getName());
$this->audit->log('Modification de l\'évènement ' . $event->name);
$this->addFlash('notice', 'L\'évènement a été modifié');
return $this->redirectToRoute('admin_accounting_events_list');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AppBundle\Controller\Admin\Accounting\Configuration;

use AppBundle\Accounting\Model\Repository\EventRepository;
use AppBundle\Accounting\Entity\Repository\EventRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
Expand Down
27 changes: 27 additions & 0 deletions sources/AppBundle/StaticAnalysis/Rule/DoctrineRepositoryRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Type;
use Symfony\Component\Form\AbstractType;

/**
* @implements Rule<MethodCall>
Expand All @@ -28,11 +29,13 @@
];

private ClassReflection $repositoryClassReflection;
private ClassReflection $formClassReflection;

public function __construct(
private ReflectionProvider $reflectionProvider,
) {
$this->repositoryClassReflection = $this->reflectionProvider->getClass(EntityRepository::class);
$this->formClassReflection = $this->reflectionProvider->getClass(AbstractType::class);
}

public function getNodeType(): string
Expand All @@ -57,6 +60,8 @@ public function processNode(Node $node, Scope $scope): array

// Si l'appel est fait depuis l'intérieur d'un repository, c'est autorisé
|| $this->isRepositoryClass($scope->getClassReflection())
// Si l'appel est fait depuis l'intérieur d'un formulaire, c'est autorisé
|| $this->isFormClass($scope->getClassReflection())
) {
return [];
}
Expand Down Expand Up @@ -102,6 +107,28 @@ private function isRepositoryClass(ClassReflection|Type $target): bool
|| $target->isSubclassOfClass($this->repositoryClassReflection);
}

private function isFormClass(ClassReflection|Type $target): bool
{
if ($target instanceof Type) {
$classes = $target->getObjectClassNames();

foreach ($classes as $class) {
$classReflection = $this->reflectionProvider->getClass($class);

if ($classReflection->getName() === $this->formClassReflection->getName()
|| $classReflection->isSubclassOfClass($this->formClassReflection)
) {
return true;
}
}

return false;
}

return $target->getName() === $this->formClassReflection->getName()
|| $target->isSubclassOfClass($this->formClassReflection);
}

private function isMethodOverridden(Type $type, string $methodName): bool
{
if ($type->isObject()->no()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{ form_row(form.paymentTypeId) }}
{{ form_row(form.vat) }}
{{ form_row(form.category) }}
{{ form_row(form.eventId) }}
{{ form_row(form.event) }}
{{ form_row(form.attachmentRequired) }}
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions tests/behat/features/Admin/Tresorerie/Configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Feature: Administration - Trésorerie - Configuration
# À determiner
When I fill in "rule[category]" with "26"
# Association AFUP
When I fill in "rule[eventId]" with "27"
When I fill in "rule[event]" with "27"
# Justification obligatoire
When I fill in "rule[attachmentRequired]" with "1"
And I press "Ajouter"
Expand All @@ -120,5 +120,5 @@ Feature: Administration - Trésorerie - Configuration
And The "rule[paymentTypeId]" field should have the following selected value "2"
And The "rule[vat]" field should have the following selected value "5_5"
And The "rule[category]" field should have the following selected value "26"
And The "rule[eventId]" field should have the following selected value "27"
And The "rule[event]" field should have the following selected value "27"
And The "rule[attachmentRequired]" field should have the following selected value "1"
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../../../../stubs/phpstan/Doctrine/SubClassRepository.php'], []);
$this->analyse([__DIR__ . '/../../../../stubs/phpstan/Doctrine/NotRepository.php'], []);
$this->analyse([__DIR__ . '/../../../../stubs/phpstan/Doctrine/SubNotRepository.php'], []);
$this->analyse([__DIR__ . '/../../../../stubs/phpstan/Doctrine/FormType.php'], []);
}
}
20 changes: 20 additions & 0 deletions tests/stubs/phpstan/Doctrine/FormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Afup\Tests\Stubs\PHPStan\Doctrine;

use AppBundle\Doctrine\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

final class FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('stdclass', EntityType::class, [
'query_builder' => fn(EntityRepository $er) => $er->createQueryBuilder('foo')->where('foo.id = 1'),
]);
}
}
Loading