Skip to content
Merged
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
40 changes: 40 additions & 0 deletions Common/ClassUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Xiidea\EasyAuditBundle\Common;

use Doctrine\Persistence\Proxy;

class ClassUtils
{
/**
* Gets the real class name of a class name that could be a proxy.
*
* @param string $className
*
* @return string
* @psalm-return class-string
*/
public static function getRealClass(string $className): string
{
$pos = strrpos($className, '\\' . Proxy::MARKER . '\\');

if ($pos === false) {
return $className;
}

return substr($className, $pos + Proxy::MARKER_LENGTH + 2);
}

/**
* Gets the real class name of an object (even if its a proxy).
*
* @param object $object
*
* @return string
* @psalm-return class-string
*/
public static function getClass(object $object): string
{
return self::getRealClass(get_class($object));
}
}
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getConfigTreeBuilder(): TreeBuilder
/**
* @param ArrayNodeDefinition $rootNode
*/
private function addRequiredConfigs(ArrayNodeDefinition $rootNode)
private function addRequiredConfigs(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand All @@ -58,7 +58,7 @@ private function addRequiredConfigs(ArrayNodeDefinition $rootNode)
/**
* @param ArrayNodeDefinition $rootNode
*/
private function addDefaultServices(ArrayNodeDefinition $rootNode)
private function addDefaultServices(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/XiideaEasyAuditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
Expand Down Expand Up @@ -54,7 +54,7 @@ public function load(array $configs, ContainerBuilder $container): void
*
* @throws \Exception
*/
protected function loadDefaultResolverServices($config, LoaderInterface $loader)
protected function loadDefaultResolverServices($config, LoaderInterface $loader): void
{
if ('xiidea.easy_audit.default_event_resolver' === $config['resolver']) {
$loader->load('default/event-resolver.yml');
Expand Down
2 changes: 1 addition & 1 deletion Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(private ManagerRegistry $doctrine)
}

#[\Override]
public function log(AuditLog $event = null)
public function log(?AuditLog $event = null)
{
if (empty($event)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Logger/MonologLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(private \Psr\Log\LoggerInterface $logger)
}

#[\Override]
public function log(AuditLog $event = null)
public function log(?AuditLog $event = null)
{
if (null === $event) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Resolver/DoctrineObjectEventResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Xiidea\EasyAuditBundle\Resolver;

use Xiidea\EasyAuditBundle\Common\ClassUtils;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Contracts\EventDispatcher\Event;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
Expand Down
14 changes: 7 additions & 7 deletions Subscriber/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Xiidea\EasyAuditBundle\Subscriber;

use Doctrine\Common\Util\ClassUtils;
use Xiidea\EasyAuditBundle\Common\ClassUtils;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Xiidea\EasyAuditBundle\Attribute\SubscribeDoctrineEvents;
Expand All @@ -27,17 +27,17 @@ public function __construct(private array $entities = [])
{
}

public function postPersist(LifecycleEventArgs $args)
public function postPersist(LifecycleEventArgs $args): void
{
$this->handleEvent(DoctrineEvents::ENTITY_CREATED, $args);
}

public function postUpdate(LifecycleEventArgs $args)
public function postUpdate(LifecycleEventArgs $args): void
{
$this->handleEvent(DoctrineEvents::ENTITY_UPDATED, $args);
}

public function preRemove(LifecycleEventArgs $args)
public function preRemove(LifecycleEventArgs $args): void
{
if (false === $this->isConfiguredToTrack($args->getObject(), DoctrineEvents::ENTITY_DELETED)) {
return;
Expand All @@ -52,7 +52,7 @@ public function preRemove(LifecycleEventArgs $args)
$this->toBeDeleted[$className][spl_object_hash($args->getObject())] = $this->getIdentity($args, $className);
}

public function postRemove(LifecycleEventArgs $args)
public function postRemove(LifecycleEventArgs $args): void
{
$identity = $this->getToBeDeletedId($args->getObject());

Expand All @@ -74,7 +74,7 @@ private function getToBeDeletedId($entity)
* @param string $eventName
* @param LifecycleEventArgs $args
*/
private function handleEvent($eventName, LifecycleEventArgs $args)
private function handleEvent($eventName, LifecycleEventArgs $args): void
{
if (true === $this->isConfiguredToTrack($args->getObject(), $eventName)) {
$this->dispatcher->dispatch(
Expand All @@ -90,7 +90,7 @@ private function handleEvent($eventName, LifecycleEventArgs $args)
*
* @return bool
*/
private function isConfiguredToTrack($entity, $eventName = '')
private function isConfiguredToTrack($entity, $eventName = ''): ?bool
{
$class = ClassUtils::getClass($entity);
$eventType = DoctrineEvents::getShortEventType($eventName);
Expand Down
43 changes: 43 additions & 0 deletions Tests/Common/ClassUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Xiidea\EasyAuditBundle\Tests\Common;

use PHPUnit\Framework\TestCase;
use Xiidea\EasyAuditBundle\Common\ClassUtils;
use Doctrine\Persistence\Proxy;

class ClassUtilsTest extends TestCase
{
public function testGetRealClassWithNonProxyClassName()
{
$className = 'My\Real\Class';
$this->assertEquals($className, ClassUtils::getRealClass($className));
}

public function testGetRealClassWithProxyClassName()
{
$className = 'My\Proxy\Namespace\\' . Proxy::MARKER . '\My\Real\Class';
$this->assertEquals('My\Real\Class', ClassUtils::getRealClass($className));
}

public function testGetClassWithNonProxyObject()
{
$object = new \stdClass();
$this->assertEquals('stdClass', ClassUtils::getClass($object));
}

public function testGetClassWithProxyObject()
{
$proxy = new \My\Proxy\Namespace\__CG__\My\Real\RealClass();
$this->assertEquals('My\Real\RealClass', ClassUtils::getClass($proxy));
}
}

namespace My\Real;
class RealClass {}

namespace My\Proxy\Namespace\__CG__\My\Real;
class RealClass extends \My\Real\RealClass implements \Doctrine\Persistence\Proxy {
public function __load(): void {}
public function __isInitialized(): bool { return true; }
}
34 changes: 15 additions & 19 deletions Tests/DependencyInjection/Compiler/LoggerFactoryPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public function testProcessWithLoggerFactoryDefinitions()
*/
protected function getDefinitionMock()
{
$i = 0;

$definitionMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')
->disableOriginalConstructor()
->getMock();
Expand All @@ -63,28 +61,26 @@ protected function getDefinitionMock()
);

$definitionMock
->expects($this->at($i++))
->expects($this->exactly(2))
->method('getMethodCalls')
->will($this->returnValue(array()));
->willReturnOnConsecutiveCalls(array(), $loggers);

$definitionMock
->expects($this->at($i++))
->expects($this->exactly(2))
->method('setMethodCalls')
->with($this->equalTo(array()));
->withConsecutive(
array($this->equalTo(array())),
array($this->equalTo($loggers))
);

$definitionMock
->expects($this->at($i++))
->expects($this->exactly(2))
->method('addMethodCall')
->with($this->equalTo('addLogger'), $this->equalTo($loggers[0][1]));
$definitionMock->expects($this->at($i++))
->method('addMethodCall')
->with($this->equalTo('addLogger'), $this->equalTo($loggers[1][1]));
$definitionMock
->expects($this->at($i++))
->method('getMethodCalls')
->will($this->returnValue($loggers));
$definitionMock
->expects($this->at($i))
->method('setMethodCalls')
->with($this->equalTo($loggers));
->withConsecutive(
array($this->equalTo('addLogger'), $this->equalTo($loggers[0][1])),
array($this->equalTo('addLogger'), $this->equalTo($loggers[1][1]))
);

return $definitionMock;
}

Expand Down
Loading