Skip to content

Commit

Permalink
Cleanup unused code, fix a range of minor psalm issues
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed Jan 3, 2024
1 parent 76c6d05 commit 9a3675b
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 220 deletions.
85 changes: 51 additions & 34 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="src/EventManager.php">
<DocblockTypeContradiction>
<code>is_string($eventName)</code>
Expand All @@ -25,22 +25,45 @@
<code>(int) $priority</code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/EventManagerAwareInterface.php">
<UnusedClass>
<code>EventManagerAwareInterface</code>
</UnusedClass>
</file>
<file src="src/EventManagerAwareTrait.php">
<MissingConstructor>
<code>$events</code>
</MissingConstructor>
<MissingReturnType>
<code>setEventManager</code>
</MissingReturnType>
<MixedArgumentTypeCoercion>
<code>$identifiers</code>
</MixedArgumentTypeCoercion>
<RedundantConditionGivenDocblockType>
<PossiblyUnusedMethod>
<code>getEventManager</code>
</PossiblyUnusedMethod>
<RedundantCondition>
<code><![CDATA[(is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))]]></code>
<code><![CDATA[is_string($this->eventIdentifier)]]></code>
<code><![CDATA[isset($this->eventIdentifier)]]></code>
</RedundantConditionGivenDocblockType>
</RedundantCondition>
<TypeDoesNotContainType>
<code><![CDATA[$this->eventIdentifier instanceof Traversable]]></code>
<code><![CDATA[(is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))]]></code>
<code><![CDATA[(is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))
|| $this->eventIdentifier instanceof Traversable]]></code>
<code><![CDATA[(is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))
|| $this->eventIdentifier instanceof Traversable]]></code>
<code><![CDATA[is_array($this->eventIdentifier)]]></code>
<code><![CDATA[is_object($this->eventIdentifier)]]></code>
</TypeDoesNotContainType>
</file>
<file src="src/EventsCapableInterface.php">
<PossiblyUnusedMethod>
<code>getEventManager</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Filter/FilterInterface.php">
<MissingReturnType>
Expand Down Expand Up @@ -113,10 +136,19 @@
<code><![CDATA[$this->listener]]></code>
<code><![CDATA[$this->listener]]></code>
</MixedReturnStatement>
<PossiblyUnusedReturnValue>
<code>callable</code>
</PossiblyUnusedReturnValue>
<PropertyNotSetInConstructor>
<code>$listener</code>
</PropertyNotSetInConstructor>
</file>
<file src="src/LazyListenerAggregate.php">
<UnusedProperty>
<code>$container</code>
<code>$env</code>
</UnusedProperty>
</file>
<file src="src/ResponseCollection.php">
<MissingReturnType>
<code>setStopped</code>
Expand Down Expand Up @@ -209,9 +241,6 @@
</RedundantCastGivenDocblockType>
</file>
<file src="src/SharedEventManagerInterface.php">
<InvalidParamDefault>
<code>string</code>
</InvalidParamDefault>
<MissingReturnType>
<code>attach</code>
<code>clearListeners</code>
Expand Down Expand Up @@ -438,12 +467,6 @@
<code>LazyListener</code>
</MoreSpecificReturnType>
</file>
<file src="test/ListenerAggregateTraitTest.php">
<UndefinedInterfaceMethod>
<code>getCallbacks</code>
<code>getCallbacks</code>
</UndefinedInterfaceMethod>
</file>
<file src="test/SharedEventManagerTest.php">
<MissingReturnType>
<code>testAttachRaisesExceptionForInvalidEvent</code>
Expand Down Expand Up @@ -489,6 +512,11 @@
<code>$callback</code>
</UnusedVariable>
</file>
<file src="test/SharedListenerIntegrationTest.php">
<InvalidPropertyFetch>
<code><![CDATA[$listener->count]]></code>
</InvalidPropertyFetch>
</file>
<file src="test/Test/EventListenerIntrospectionTraitTest.php">
<MissingClosureParamType>
<code>$e</code>
Expand Down Expand Up @@ -573,34 +601,23 @@
<MissingReturnType>
<code>build</code>
</MissingReturnType>
<PossiblyUnusedMethod>
<code>build</code>
</PossiblyUnusedMethod>
</file>
<file src="test/TestAsset/CountingListener.php">
<PossiblyUnusedProperty>
<code>$index</code>
</PossiblyUnusedProperty>
</file>
<file src="test/TestAsset/MockAbstractListenerAggregate.php">
<MissingReturnType>
<code>doFoo</code>
</MissingReturnType>
</file>
<file src="test/TestAsset/MockAggregate.php">
<ImplementedReturnTypeMismatch>
<code>string</code>
</ImplementedReturnTypeMismatch>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->listeners]]></code>
</InvalidPropertyAssignmentValue>
<MixedArgument>
<code>$listener</code>
</MixedArgument>
<MixedAssignment>
<code>$listener</code>
</MixedAssignment>
</file>
<file src="test/TestAsset/MockListenerAggregateTrait.php">
<MissingReturnType>
<code>doFoo</code>
</MissingReturnType>
</file>
<file src="test/TestAsset/StaticEventsMock.php">
<MissingReturnType>
<code>attach</code>
</MissingReturnType>
</file>
</files>
17 changes: 6 additions & 11 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedCode="true"
findUnusedBaselineEntry="true"
findUnusedPsalmSuppress="true"
>
<projectFiles>
<directory name="src"/>
Expand All @@ -16,19 +19,11 @@
</projectFiles>

<issueHandlers>
<InternalMethod>
<UnusedClass>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
<directory name="psalm" />
</errorLevel>

<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn"/>
</errorLevel>

<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
</errorLevel>
</InternalMethod>
</UnusedClass>
</issueHandlers>

<plugins>
Expand Down
3 changes: 1 addition & 2 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getTarget()
*/
public function setParams($params)
{
/** @psalm-suppress DocblockTypeContradiction Sanity check to actually enforce docblock. */
/** @psalm-suppress DocblockTypeContradiction, RedundantCondition Sanity check to actually enforce docblock. */
if (! is_array($params) && ! is_object($params)) {
throw new Exception\InvalidArgumentException(
sprintf('Event parameters must be an array or object; received "%s"', gettype($params))
Expand Down Expand Up @@ -128,7 +128,6 @@ public function getParam($name, $default = null)
}

// Check in normal objects
/** @psalm-suppress MixedPropertyFetch Only object is left over from union. */
if (! isset($this->params->{$name})) {
return $default;
}
Expand Down
7 changes: 3 additions & 4 deletions src/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use function array_keys;
use function array_merge;
use function array_unique;
use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function krsort;
use function sprintf;
Expand Down Expand Up @@ -187,7 +186,7 @@ public function attach($eventName, callable $listener, $priority = 1)
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a string for the event; received %s',
__METHOD__,
is_object($eventName) ? $eventName::class : gettype($eventName)
get_debug_type($eventName),
));
}

Expand All @@ -213,7 +212,7 @@ public function detach(callable $listener, $eventName = null, $force = false)
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a string for the event; received %s',
__METHOD__,
is_object($eventName) ? $eventName::class : gettype($eventName)
get_debug_type($eventName),
));
}

Expand Down
5 changes: 2 additions & 3 deletions src/LazyListenerAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Psr\Container\ContainerInterface;

use function gettype;
use function get_debug_type;
use function is_array;
use function is_object;
use function sprintf;

/**
Expand Down Expand Up @@ -78,7 +77,7 @@ public function __construct(array $listeners, ContainerInterface $container, arr
if (! $listener instanceof LazyEventListener) {
throw new Exception\InvalidArgumentException(sprintf(
'All listeners must be LazyEventListener instances or definitions; received %s',
is_object($listener) ? $listener::class : gettype($listener)
get_debug_type($listener),
));
}

Expand Down
11 changes: 5 additions & 6 deletions src/SharedEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use function array_keys;
use function array_merge;
use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -63,14 +62,14 @@ public function attach($identifier, $event, callable $listener, $priority = 1)
if (! is_string($identifier) || empty($identifier)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid identifier provided; must be a string; received "%s"',
is_object($identifier) ? $identifier::class : gettype($identifier)
get_debug_type($identifier),
));
}

if (! is_string($event) || empty($event)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid event provided; must be a non-empty string; received "%s"',
is_object($event) ? $event::class : gettype($event)
get_debug_type($event),
));
}

Expand All @@ -93,7 +92,7 @@ public function detach(callable $listener, $identifier = null, $eventName = null
if (! is_string($identifier) || empty($identifier)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid identifier provided; must be a string, received %s',
is_object($identifier) ? $identifier::class : gettype($identifier)
get_debug_type($identifier),
));
}

Expand All @@ -112,7 +111,7 @@ public function detach(callable $listener, $identifier = null, $eventName = null
if (! is_string($eventName) || empty($eventName)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid event name provided; must be a string, received %s',
is_object($eventName) ? $eventName::class : gettype($eventName)
get_debug_type($eventName),
));
}

Expand Down
48 changes: 44 additions & 4 deletions test/AbstractListenerAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,50 @@

namespace LaminasTest\EventManager;

use Laminas\EventManager\ListenerAggregateInterface;
use Laminas\EventManager\EventManagerInterface;
use LaminasTest\EventManager\TestAsset\MockAbstractListenerAggregate;
use PHPUnit\Framework\TestCase;

class AbstractListenerAggregateTest extends ListenerAggregateTraitTest
use function in_array;

class AbstractListenerAggregateTest extends TestCase
{
/** @var class-string<ListenerAggregateInterface> */
public $aggregateClass = TestAsset\MockAbstractListenerAggregate::class;
public function testDetachRemovesAttachedListeners(): void
{
$aggregate = new MockAbstractListenerAggregate();

$events = $this->createMock(EventManagerInterface::class);
$events->expects(self::atLeast(2))
->method('attach')
->with(
self::callback(static function (string $value): bool {
self::assertTrue(in_array($value, ['foo.bar', 'foo.baz'], true));

return true;
}),
self::callback(static function (array $value) use ($aggregate): bool {
self::assertSame($aggregate, $value[0] ?? null);
self::assertSame('doFoo', $value[1] ?? null);

return true;
}),
)->willReturnArgument(1);

$events->expects(self::exactly(2))
->method('detach')
->with([$aggregate, 'doFoo']);

$aggregate->attach($events);

$listeners = $aggregate->getCallbacks();
self::assertCount(2, $listeners);

foreach ($listeners as $listener) {
self::assertSame([$aggregate, 'doFoo'], $listener);
}

$aggregate->detach($events);

self::assertSame([], $aggregate->getCallbacks());
}
}
2 changes: 0 additions & 2 deletions test/FilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ public function testFilterIsPassedContextAndArguments(): void

public function testInterceptingFilterShouldReceiveChain(): void
{
/** @psalm-suppress UnusedClosureParam */
$this->filterchain->attach(function (self $context, array $params, mixed $chain) {
self::assertInstanceOf(FilterIterator::class, $chain);
});
$this->filterchain->run($this);
}

/** @psalm-suppress UnusedClosureParam */
public function testFilteringStopsAsSoonAsAFilterFailsToCallNext(): void
{
$this->filterchain->attach(function ($context, $params, $chain) {
Expand Down
8 changes: 1 addition & 7 deletions test/ListenerAggregateTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
namespace LaminasTest\EventManager;

use Laminas\EventManager\EventManagerInterface;
use Laminas\EventManager\ListenerAggregateInterface;
use LaminasTest\EventManager\TestAsset\MockListenerAggregateTrait;
use PHPUnit\Framework\TestCase;

use function in_array;

class ListenerAggregateTraitTest extends TestCase
{
/** @var class-string<ListenerAggregateInterface> */
public $aggregateClass = MockListenerAggregateTrait::class;

public function testDetachRemovesAttachedListeners(): void
{
$class = $this->aggregateClass;
$aggregate = new $class();
$aggregate = new MockListenerAggregateTrait();

$events = $this->createMock(EventManagerInterface::class);
$events->expects(self::atLeast(2))
Expand All @@ -45,7 +40,6 @@ public function testDetachRemovesAttachedListeners(): void
$aggregate->attach($events);

$listeners = $aggregate->getCallbacks();
self::assertIsArray($listeners);
self::assertCount(2, $listeners);

foreach ($listeners as $listener) {
Expand Down
Loading

0 comments on commit 9a3675b

Please sign in to comment.