Skip to content

Commit

Permalink
Fixed PHPStan feedbacks (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Sep 26, 2021
1 parent f57ad98 commit 6843e88
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ parameters:
- '~Parameter \#2 \$eventName of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects string\|null, DH\\Auditor\\Event\\LifecycleEvent given\.~'
- '~Cannot call method setFetchMode\(\) on Doctrine\\DBAL\\ForwardCompatibility\\DriverStatement\|int~'
- '~Cannot call method fetchAll\(\) on Doctrine\\DBAL\\ForwardCompatibility\\DriverStatement\|int~'
- '~Parameter \#1 \$keep of method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Command\\CleanAuditLogsCommand\:\:validateKeepArgument\(\) expects string, string\|null given~'
- '~Method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Reader\\Filter\\RangeFilter\:\:__construct\(\) has parameter \$minValue with no typehint specified~'
- '~Method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Reader\\Filter\\RangeFilter\:\:__construct\(\) has parameter \$maxValue with no typehint specified~'
- '~Method DH\\Auditor\\Provider\\Doctrine\\Persistence\\Reader\\Filter\\\SimpleFilter\:\:__construct\(\) has parameter \$value with no typehint specified.~'
- '~Call to an undefined method ReflectionClass\<object\>\:\:getAttributes\(\)\.~'
- '~Call to an undefined method ReflectionProperty\:\:getAttributes\(\)\.~'
17 changes: 12 additions & 5 deletions src/Provider/Doctrine/Auditing/Annotation/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class AnnotationLoader
{
/**
* @var AnnotationReader
* @var null|AnnotationReader
*/
private $reader;

Expand Down Expand Up @@ -42,14 +42,19 @@ public function load(): array

private function getEntityConfiguration(ClassMetadata $metadata): ?array
{
$annotation = null;
$auditableAnnotation = null;
$securityAnnotation = null;
$annotationProperty = null;
$reflection = $metadata->getReflectionClass();

// Check that we have an Entity annotation
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Entity::class)) {
$annotation = $attributes[0]->newInstance();
} else {
} elseif (null !== $this->reader) {
$annotation = $this->reader->getClassAnnotation($reflection, Entity::class);
}

if (null === $annotation) {
return null;
}
Expand All @@ -58,10 +63,11 @@ private function getEntityConfiguration(ClassMetadata $metadata): ?array
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Auditable::class)) {
/** @var ?Auditable $auditableAnnotation */
$auditableAnnotation = $attributes[0]->newInstance();
} else {
} elseif (null !== $this->reader) {
/** @var ?Auditable $auditableAnnotation */
$auditableAnnotation = $this->reader->getClassAnnotation($reflection, Auditable::class);
}

if (null === $auditableAnnotation) {
return null;
}
Expand All @@ -70,10 +76,11 @@ private function getEntityConfiguration(ClassMetadata $metadata): ?array
if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Security::class)) {
/** @var ?Security $securityAnnotation */
$securityAnnotation = $attributes[0]->newInstance();
} else {
} elseif (null !== $this->reader) {
/** @var ?Security $securityAnnotation */
$securityAnnotation = $this->reader->getClassAnnotation($reflection, Security::class);
}

if (null === $securityAnnotation) {
$roles = null;
} else {
Expand All @@ -96,7 +103,7 @@ private function getEntityConfiguration(ClassMetadata $metadata): ?array
$annotationProperty = $this->reader->getPropertyAnnotation($property, Ignore::class);
}

if ($annotationProperty) {
if (null !== $annotationProperty) {
// TODO: $property->getName() might not be the column name
$config['ignored_columns'][] = $property->getName();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Doctrine/Auditing/Annotation/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* @Annotation
* @NamedArgumentConstructor()
* @NamedArgumentConstructor
* @Target("CLASS")
* @Attributes({
* @Attribute("enabled", required=false, type="bool"),
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/Doctrine/Auditing/Annotation/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @Annotation
* @NamedArgumentConstructor()
* @NamedArgumentConstructor
* @Target("CLASS")
* @Attributes({
* @Attribute("view", required=true, type="array<string>"),
Expand All @@ -21,7 +21,7 @@ final class Security
public const VIEW_SCOPE = 'view';

/**
* @var string
* @var array
* @Required
*/
public $view;
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Doctrine/Auditing/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function startQuery($sql, ?array $params = null, ?array $types = null): v
}
// on rollback remove flusher callback
if ('"ROLLBACK"' === $sql) {
$this->flusher = static function(){};
$this->flusher = static function (): void {};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ private function hydrateWithScheduledCollectionUpdates(Transaction $transaction,
$uow = $entityManager->getUnitOfWork();
/** @var PersistentCollection $collection */
foreach (array_reverse($uow->getScheduledCollectionUpdates()) as $collection) {
if ($this->provider->isAudited($collection->getOwner())) {
/** @var object $owner */
$owner = $collection->getOwner();
if ($this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();
foreach ($collection->getInsertDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
Expand Down Expand Up @@ -110,7 +112,9 @@ private function hydrateWithScheduledCollectionDeletions(Transaction $transactio
$uow = $entityManager->getUnitOfWork();
/** @var PersistentCollection $collection */
foreach (array_reverse($uow->getScheduledCollectionDeletions()) as $collection) {
if ($this->provider->isAudited($collection->getOwner())) {
/** @var object $owner */
$owner = $collection->getOwner();
if ($this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();
foreach ($collection->toArray() as $entity) {
if ($this->provider->isAudited($entity)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Security/RoleCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DH\Auditor\Security;
namespace DH\Auditor\Security;

interface RoleCheckerInterface
{
Expand Down

0 comments on commit 6843e88

Please sign in to comment.