Skip to content

Commit

Permalink
phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Mar 18, 2024
1 parent 0e0c521 commit fde625b
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 89 deletions.
3 changes: 3 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Error
*/
protected ?Throwable $exception;

/**
* @var array<string, int>
*/
private array $levelMap = [
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
Expand Down
2 changes: 1 addition & 1 deletion src/ErrorBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getErrorLoggingContextName() : string
/**
* Return any children ErrorBag objects.
*
* @return ErrorBag[]
* @return IErrorBag[]
*/
abstract protected function getErrorBagChildren() : array;

Expand Down
1 change: 1 addition & 0 deletions src/Header/AddressHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function getEmail() : ?string
if (!empty($this->addresses)) {
return $this->addresses[0]->getEmail();
}
return null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Header/Consumer/AbstractConsumerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ArrayIterator;
use Iterator;
use NoRewindIterator;
use DI\Attribute\Inject;
use ZBateson\MailMimeParser\Header\IHeaderPart;
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPart;
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Consumer/AddressGroupConsumerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(HeaderPartFactory $partFactory)
*
* @param AddressConsumerService $subConsumer
*/
public function setAddressConsumerService(AddressConsumerService $subConsumer)
public function setAddressConsumerService(AddressConsumerService $subConsumer) : void
{
$this->subConsumers = [$subConsumer];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ protected function getTokenSeparators() : array
* as the first element, followed by any comment elements as subsequent
* elements.
*
* @param \ZBateson\MailMimeParser\Header\Part\IHeaderPart[] $parts
* @return \ZBateson\MailMimeParser\Header\Part\IHeaderPart[]
* @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
* @return \ZBateson\MailMimeParser\Header\IHeaderPart[]
*/
protected function processParts(array $parts) : array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Header/HeaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZBateson\MailMimeParser\Header;

use DI\Attribute\Inject;
use ZBateson\MailMimeParser\Header\Consumer\AddressBaseConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\DateConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\GenericConsumerMimeLiteralPartService;
Expand All @@ -15,6 +16,7 @@
use ZBateson\MailMimeParser\Header\Consumer\ReceivedConsumerService;
use ZBateson\MailMimeParser\Header\Consumer\SubjectConsumerService;
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
use ZBateson\MailMimeParser\Header\Consumer\IConsumerService;
use ReflectionClass;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Part/HeaderPartFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function newAddressPart(string $name, string $email) : AddressPart
/**
* Initializes and returns a new AddressGroupPart
*
* @param string[] $addresses
* @param AddressPart[] $addresses
*/
public function newAddressGroupPart(array $addresses, string $name = '') : AddressGroupPart
{
Expand Down
4 changes: 4 additions & 0 deletions src/IErrorBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function hasErrors(bool $validate = false, string $minPsrLevel = LogLevel
*
* If $validate is true, additional validation may be performed on the
* object to check for errors.
*
* @return Error[]
*/
public function getErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array;

Expand Down Expand Up @@ -77,6 +79,8 @@ public function hasAnyErrors(bool $validate = false, string $minPsrLevel = LogLe
*
* If $validate is true, additional validation may be performed on children
* to check for errors.
*
* @return Error[]
*/
public function getAllErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array;
}
14 changes: 7 additions & 7 deletions src/Message/IMimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function isSignaturePart() : bool;
* given name.
* @return ?IHeader the header object if it exists, or null if not
*/
public function getHeader($name, $offset = 0) : ?IHeader;
public function getHeader(string $name, int $offset = 0) : ?IHeader;

/**
* Returns the IHeader object for the header with the given $name, using the
Expand Down Expand Up @@ -128,7 +128,7 @@ public function getAllHeaders() : array;
* @param string $name
* @return IHeader[] an array of header objects
*/
public function getAllHeadersByName($name) : array;
public function getAllHeadersByName(string $name) : array;

/**
* Returns a two dimensional string array of all headers for the mime part
Expand Down Expand Up @@ -171,7 +171,7 @@ public function getRawHeaders() : array;
* with a certain name.
* @see IMimePart::getRawHeaders() to retrieve the array the returned
* iterator iterates over.
* @return Traversable an iterator for raw headers
* @return Traversable<array<string>> an iterator for raw headers
*/
public function getRawHeaderIterator() : Traversable;

Expand All @@ -193,11 +193,11 @@ public function getRawHeaderIterator() : Traversable;
* @see IMimePart::getRawHeaderIterator() to retrieve an iterator instead of
* the returned two-dimensional array
* @param string $name The name of the header
* @param string $defaultValue Optional default value to return if the
* @param ?string $defaultValue Optional default value to return if the
* header doesn't exist on this part.
* @return string|null the value of the header
*/
public function getHeaderValue($name, $defaultValue = null) : ?string;
public function getHeaderValue(string $name, ?string $defaultValue = null) : ?string;

/**
* Returns the value of the parameter named $param on a header with the
Expand All @@ -222,11 +222,11 @@ public function getHeaderValue($name, $defaultValue = null) : ?string;
* the returned two-dimensional array
* @param string $header The name of the header.
* @param string $param The name of the parameter.
* @param string $defaultValue Optional default value to return if the
* @param ?string $defaultValue Optional default value to return if the
* parameter doesn't exist.
* @return string|null The value of the parameter.
*/
public function getHeaderParameter($header, $param, $defaultValue = null) : ?string;
public function getHeaderParameter(string $header, string $param, ?string $defaultValue = null) : ?string;

/**
* Adds a header with the given $name and $value. An optional $offset may
Expand Down
23 changes: 12 additions & 11 deletions src/Message/IMultiPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface IMultiPart extends IMessagePart
* returning true if the part should be included.
* @return IMessagePart|null A matching part, or null if not found.
*/
public function getPart($index, $fnFilter = null) : ?IMessagePart;
public function getPart(int $index, callable $fnFilter = null) : ?IMessagePart;

/**
* Returns the current part, all child parts, and child parts of all
Expand All @@ -70,7 +70,7 @@ public function getPart($index, $fnFilter = null) : ?IMessagePart;
* returning true if the part should be included.
* @return IMessagePart[] An array of matching parts.
*/
public function getAllParts($fnFilter = null) : array;
public function getAllParts(callable $fnFilter = null) : array;

/**
* Returns the total number of parts in this and all children.
Expand All @@ -93,7 +93,7 @@ public function getAllParts($fnFilter = null) : array;
* returning true if the part should be included.
* @return int The number of matching parts.
*/
public function getPartCount($fnFilter = null) : int;
public function getPartCount(callable $fnFilter = null) : int;

/**
* Returns the direct child at the given 0-based index and optional filter,
Expand All @@ -118,7 +118,7 @@ public function getPartCount($fnFilter = null) : int;
* @return IMessagePart|null The matching direct child part or null if not
* found.
*/
public function getChild($index, $fnFilter = null) : ?IMessagePart;
public function getChild(int $index, callable $fnFilter = null) : ?IMessagePart;

/**
* Returns an array of all direct child parts, optionally filtering them
Expand All @@ -140,7 +140,7 @@ public function getChild($index, $fnFilter = null) : ?IMessagePart;
* returning true if the part should be included.
* @return IMessagePart[] An array of matching child parts.
*/
public function getChildParts($fnFilter = null) : array;
public function getChildParts(callable $fnFilter = null) : array;

/**
* Returns the number of direct children under this part (optionally
Expand All @@ -163,7 +163,7 @@ public function getChildParts($fnFilter = null) : array;
* @return int The number of children, or number of children matching the
* the passed filtering callable.
*/
public function getChildCount($fnFilter = null) : int;
public function getChildCount(callable $fnFilter = null) : int;

/**
* Returns a \RecursiveIterator of child parts.
Expand All @@ -184,6 +184,7 @@ public function getChildCount($fnFilter = null) : int;
* this part.
* @see IMultiPart::getAllParts() to get an array of all parts with an
* optional filter.
* @return RecursiveIterator<IMessagePart>
*/
public function getChildIterator() : RecursiveIterator;

Expand All @@ -203,7 +204,7 @@ public function getChildIterator() : RecursiveIterator;
* @param int $index Optional 0-based index (defaulting to '0').
* @return IMessagePart|null The part.
*/
public function getPartByMimeType($mimeType, $index = 0) : ?IMessagePart;
public function getPartByMimeType(string $mimeType, int $index = 0) : ?IMessagePart;

/**
* Returns an array of all parts that have a content type matching the
Expand All @@ -219,7 +220,7 @@ public function getPartByMimeType($mimeType, $index = 0) : ?IMessagePart;
* @param string $mimeType The mime type to find.
* @return IMessagePart[] An array of matching parts.
*/
public function getAllPartsByMimeType($mimeType) : array;
public function getAllPartsByMimeType(string $mimeType) : array;

/**
* Returns the number of parts that have content types matching the passed
Expand All @@ -231,7 +232,7 @@ public function getAllPartsByMimeType($mimeType) : array;
* @param string $mimeType The mime type to find.
* @return int The number of matching parts.
*/
public function getCountOfPartsByMimeType($mimeType) : int;
public function getCountOfPartsByMimeType(string $mimeType) : int;

/**
* Returns a part that has the given Content ID, or null if not found.
Expand All @@ -241,7 +242,7 @@ public function getCountOfPartsByMimeType($mimeType) : int;
* @param string $contentId The content ID to find a part for.
* @return IMessagePart|null The matching part.
*/
public function getPartByContentId($contentId) : ?IMessagePart;
public function getPartByContentId(string $contentId) : ?IMessagePart;

/**
* Registers the passed part as a child of the current part.
Expand Down Expand Up @@ -287,5 +288,5 @@ public function removePart(IMessagePart $part) : ?int;
* returning true if the part should be included.
* @return int The number of removed parts.
*/
public function removeAllParts($fnFilter = null) : int;
public function removeAllParts(callable $fnFilter = null) : int;
}
8 changes: 4 additions & 4 deletions src/Message/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function isSignaturePart() : bool
return $this->parent->getSignaturePart() === $this;
}

public function getHeader($name, $offset = 0) : ?IHeader
public function getHeader(string $name, int $offset = 0) : ?IHeader
{
return $this->headerContainer->get($name, $offset);
}
Expand All @@ -232,7 +232,7 @@ public function getAllHeaders() : array
return $this->headerContainer->getHeaderObjects();
}

public function getAllHeadersByName($name) : array
public function getAllHeadersByName(string $name) : array
{
return $this->headerContainer->getAll($name);
}
Expand All @@ -247,7 +247,7 @@ public function getRawHeaderIterator() : Traversable
return $this->headerContainer->getIterator();
}

public function getHeaderValue($name, $defaultValue = null) : ?string
public function getHeaderValue(string $name, ?string $defaultValue = null) : ?string
{
$header = $this->getHeader($name);
if ($header !== null) {
Expand All @@ -256,7 +256,7 @@ public function getHeaderValue($name, $defaultValue = null) : ?string
return $defaultValue;
}

public function getHeaderParameter($header, $param, $defaultValue = null) : ?string
public function getHeaderParameter(string $header, string $param, ?string $defaultValue = null) : ?string
{
$obj = $this->getHeader($header);
if ($obj && $obj instanceof ParameterHeader) {
Expand Down
29 changes: 17 additions & 12 deletions src/Message/MultiPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ private function getAllPartsIterator() : AppendIterator
return $iter;
}

private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null) : ?IMessagePart
/**
*
* @param callable $fnFilter
* @return IMessagePart|null
*/
private function iteratorFindAt(Iterator $iter, int $index, callable $fnFilter = null) : ?IMessagePart
{
$pos = 0;
foreach ($iter as $part) {
Expand All @@ -56,7 +61,7 @@ private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null) : ?IMe
return null;
}

public function getPart($index, $fnFilter = null) : ?IMessagePart
public function getPart(int $index, callable $fnFilter = null) : ?IMessagePart
{
return $this->iteratorFindAt(
$this->getAllPartsIterator(),
Expand All @@ -65,7 +70,7 @@ public function getPart($index, $fnFilter = null) : ?IMessagePart
);
}

public function getAllParts($fnFilter = null) : array
public function getAllParts(callable $fnFilter = null) : array
{
$array = \iterator_to_array($this->getAllPartsIterator(), false);
if ($fnFilter !== null) {
Expand All @@ -74,12 +79,12 @@ public function getAllParts($fnFilter = null) : array
return $array;
}

public function getPartCount($fnFilter = null) : int
public function getPartCount(callable $fnFilter = null) : int
{
return \count($this->getAllParts($fnFilter));
}

public function getChild($index, $fnFilter = null) : ?IMessagePart
public function getChild(int $index, callable $fnFilter = null) : ?IMessagePart
{
return $this->iteratorFindAt(
$this->partChildrenContainer,
Expand All @@ -93,7 +98,7 @@ public function getChildIterator() : RecursiveIterator
return $this->partChildrenContainer;
}

public function getChildParts($fnFilter = null) : array
public function getChildParts(callable $fnFilter = null) : array
{
$array = \iterator_to_array($this->partChildrenContainer, false);
if ($fnFilter !== null) {
Expand All @@ -102,27 +107,27 @@ public function getChildParts($fnFilter = null) : array
return $array;
}

public function getChildCount($fnFilter = null) : int
public function getChildCount(callable $fnFilter = null) : int
{
return \count($this->getChildParts($fnFilter));
}

public function getPartByMimeType($mimeType, $index = 0) : ?IMessagePart
public function getPartByMimeType(string $mimeType, int $index = 0) : ?IMessagePart
{
return $this->getPart($index, PartFilter::fromContentType($mimeType));
}

public function getAllPartsByMimeType($mimeType) : array
public function getAllPartsByMimeType(string $mimeType) : array
{
return $this->getAllParts(PartFilter::fromContentType($mimeType));
}

public function getCountOfPartsByMimeType($mimeType) : int
public function getCountOfPartsByMimeType(string $mimeType) : int
{
return $this->getPartCount(PartFilter::fromContentType($mimeType));
}

public function getPartByContentId($contentId) : ?IMessagePart
public function getPartByContentId(string $contentId) : ?IMessagePart
{
$sanitized = \preg_replace('/^\s*<|>\s*$/', '', $contentId);
return $this->getPart(0, function(IMessagePart $part) use ($sanitized) {
Expand Down Expand Up @@ -155,7 +160,7 @@ public function removePart(IMessagePart $part) : ?int
return $position;
}

public function removeAllParts($fnFilter = null) : int
public function removeAllParts(callable $fnFilter = null) : int
{
$parts = $this->getAllParts($fnFilter);
$count = \count($parts);
Expand Down
Loading

0 comments on commit fde625b

Please sign in to comment.