Skip to content

Commit

Permalink
Improve type inference
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jan 10, 2022
1 parent 99bd8ac commit a11d4e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
22 changes: 2 additions & 20 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.18.1@dda05fa913f4dc6eb3386f2f7ce5a45d37a71bcb">
<file src="src/FlashMessenger.php">
<MissingConstructor occurrences="1">
<code>$session</code>
</MissingConstructor>
<MixedArrayOffset occurrences="1">
<code>$this-&gt;messages[$namespace]</code>
</MixedArrayOffset>
Expand All @@ -17,26 +14,12 @@
<code>$namespaces[]</code>
<code>$namespaces[]</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>array</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="2">
<MixedMethodCall occurrences="1">
<code>push</code>
<code>toArray</code>
</MixedMethodCall>
<MixedPropertyTypeCoercion occurrences="1">
<code>$this-&gt;messages</code>
</MixedPropertyTypeCoercion>
<MixedReturnStatement occurrences="1">
<code>$container-&gt;{$namespace}-&gt;toArray()</code>
</MixedReturnStatement>
<MixedReturnTypeCoercion occurrences="2">
<code>$this-&gt;messages[$namespace]-&gt;toArray()</code>
<code>array&lt;array-key, string&gt;</code>
</MixedReturnTypeCoercion>
<PossiblyNullArgument occurrences="1">
<code>$hops</code>
</PossiblyNullArgument>
<UnusedForeachValue occurrences="1">
<code>$messages</code>
</UnusedForeachValue>
Expand All @@ -52,9 +35,8 @@
<code>$item</code>
<code>$messagesToPrint</code>
</MixedArgument>
<MixedArgumentTypeCoercion occurrences="2">
<MixedArgumentTypeCoercion occurrences="1">
<code>$classes</code>
<code>$messages</code>
</MixedArgumentTypeCoercion>
<MixedArrayAssignment occurrences="2">
<code>$messagesToPrint[]</code>
Expand Down
41 changes: 24 additions & 17 deletions src/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use Laminas\Stdlib\SplQueue;
use ReturnTypeWillChange;

use function assert;
use function count;

/**
* Flash Messenger - implement session-based messages
*
* @template-implements IteratorAggregate<array-key, string>
* @psalm-type MessageList = SplQueue<array-key, string>
*/
class FlashMessenger extends AbstractPlugin implements IteratorAggregate, Countable
{
Expand Down Expand Up @@ -53,11 +55,11 @@ class FlashMessenger extends AbstractPlugin implements IteratorAggregate, Counta
/**
* Messages from previous request
*
* @var array<string, SplQueue>
* @var array<string, MessageList>
*/
protected $messages = [];

/** @var Manager */
/** @var Manager|null */
protected $session;

/**
Expand Down Expand Up @@ -98,6 +100,8 @@ public function getSessionManager()
$this->setSessionManager(Container::getDefaultManager());
}

assert($this->session instanceof Manager);

return $this->session;
}

Expand Down Expand Up @@ -147,7 +151,7 @@ public function getNamespace()
*
* @param string $message
* @param null|string $namespace
* @param null|int $hops
* @param int $hops
* @return FlashMessenger Provides a fluent interface
*/
public function addMessage($message, $namespace = null, $hops = 1)
Expand Down Expand Up @@ -314,7 +318,7 @@ public function getInfoMessages()
/**
* Get messages from "success" namespace
*
* @return array
* @return array<array-key, string>
*/
public function getSuccessMessages()
{
Expand All @@ -324,7 +328,7 @@ public function getSuccessMessages()
/**
* Get messages from "warning" namespace
*
* @return array
* @return array<array-key, string>
*/
public function getWarningMessages()
{
Expand All @@ -334,7 +338,7 @@ public function getWarningMessages()
/**
* Get messages from "error" namespace
*
* @return array
* @return array<array-key, string>
*/
public function getErrorMessages()
{
Expand All @@ -344,7 +348,7 @@ public function getErrorMessages()
/**
* Clear all messages from the previous request & current namespace
*
* @param string $namespace
* @param string|null $namespace
* @return bool True if messages were cleared, false if none existed
*/
public function clearMessages($namespace = null)
Expand Down Expand Up @@ -394,7 +398,7 @@ public function clearMessagesFromContainer()
* Check to see if messages have been added to the current
* namespace within this request
*
* @param string $namespace
* @param string|null $namespace
* @return bool
*/
public function hasCurrentMessages($namespace = null)
Expand Down Expand Up @@ -455,8 +459,8 @@ public function hasCurrentErrorMessages()
* Get messages that have been added to the current
* namespace within this request
*
* @param string $namespace
* @return array
* @param string|null $namespace
* @return array<array-key, string>
*/
public function getCurrentMessages($namespace = null)
{
Expand All @@ -466,7 +470,10 @@ public function getCurrentMessages($namespace = null)

if ($this->hasCurrentMessages($namespace)) {
$container = $this->getContainer();
return $container->{$namespace}->toArray();
/** @psalm-var MessageList $queue */
$queue = $container->{$namespace};

return $queue->toArray();
}

return [];
Expand All @@ -476,7 +483,7 @@ public function getCurrentMessages($namespace = null)
* Get messages that have been added to the "info"
* namespace within this request
*
* @return array
* @return array<array-key, string>
*/
public function getCurrentInfoMessages()
{
Expand All @@ -487,7 +494,7 @@ public function getCurrentInfoMessages()
* Get messages that have been added to the "success"
* namespace within this request
*
* @return array
* @return array<array-key, string>
*/
public function getCurrentSuccessMessages()
{
Expand All @@ -498,7 +505,7 @@ public function getCurrentSuccessMessages()
* Get messages that have been added to the "warning"
* namespace within this request
*
* @return array
* @return array<array-key, string>
*/
public function getCurrentWarningMessages()
{
Expand All @@ -509,7 +516,7 @@ public function getCurrentWarningMessages()
* Get messages that have been added to the "error"
* namespace within this request
*
* @return array
* @return array<array-key, string>
*/
public function getCurrentErrorMessages()
{
Expand All @@ -521,7 +528,7 @@ public function getCurrentErrorMessages()
* namespace in specific namespace
*
* @param string $namespaceToGet
* @return array
* @return array<array-key, string>
*/
public function getCurrentMessagesFromNamespace($namespaceToGet)
{
Expand Down Expand Up @@ -588,7 +595,7 @@ public function clearCurrentMessagesFromContainer()
/**
* Complete the IteratorAggregate interface, for iterating
*
* @return ArrayIterator
* @return ArrayIterator<array-key, string>
*/
#[ReturnTypeWillChange]
public function getIterator()
Expand Down
1 change: 0 additions & 1 deletion test/FlashMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public function testIterationOccursOverMessages(): void
$this->seedMessages();
$test = [];
foreach ($this->helper as $message) {
self::assertIsString($message);
$test[] = $message;
}
$this->assertEquals(['foo', 'bar'], $test);
Expand Down

0 comments on commit a11d4e5

Please sign in to comment.