Skip to content

Commit

Permalink
Update return types and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Mar 18, 2024
1 parent 0db93a6 commit 46ed757
Show file tree
Hide file tree
Showing 79 changed files with 607 additions and 762 deletions.
36 changes: 10 additions & 26 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ZBateson\MailMimeParser;

use Exception;
use Throwable;
use InvalidArgumentException;
use Psr\Log\LogLevel;

Expand All @@ -22,24 +22,24 @@ class Error
/**
* @var string The error message.
*/
protected $message;
protected string $message;

/**
* @var string The PSR log level for this error.
*/
protected $psrLevel;
protected string $psrLevel;

/**
* @var ErrorBag The object the error/notice occurred on.
*/
protected $object;
protected ErrorBag $object;

/**
* @var Exception An Exception object if one happened, or null if not
* @var ?Throwable An Exception object if one happened, or null if not
*/
protected $exception;
protected ?Throwable $exception;

private $levelMap = [
private array $levelMap = [
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 2,
Expand All @@ -55,7 +55,7 @@ class Error
* @throws InvalidArgumentException if the passed $psrLogLevelAsErrorLevel
* is not a known PSR log level (see \Psr\Log\LogLevel)
*/
public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Exception $exception = null)
public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Throwable $exception = null)
{
if (!isset($this->levelMap[$psrLogLevelAsErrorLevel])) {
throw new InvalidArgumentException($psrLogLevelAsErrorLevel . ' is not a known PSR Log Level');
Expand All @@ -68,8 +68,6 @@ public function __construct(string $message, string $psrLogLevelAsErrorLevel, Er

/**
* Returns the error message.
*
* @return string
*/
public function getMessage() : string
{
Expand All @@ -78,8 +76,6 @@ public function getMessage() : string

/**
* Returns the PSR string log level for this error message.
*
* @return string
*/
public function getPsrLevel() : string
{
Expand All @@ -88,21 +84,14 @@ public function getPsrLevel() : string

/**
* Returns the class type the error occurred on.
*
* @return string
*/
public function getClass() : string
{
if ($this->object !== null) {
return get_class($this->object);
}
return null;
return get_class($this->object);
}

/**
* Returns the object the error occurred on.
*
* @return ErrorBag
*/
public function getObject() : ErrorBag
{
Expand All @@ -111,10 +100,8 @@ public function getObject() : ErrorBag

/**
* Returns the exception that occurred, if any, or null.
*
* @return Exception|null
*/
public function getException() : ?Exception
public function getException() : ?Throwable
{
return $this->exception;
}
Expand All @@ -123,9 +110,6 @@ public function getException() : ?Exception
* Returns true if the PSR log level for this error is equal to or greater
* than the one passed, e.g. passing LogLevel::ERROR would return true for
* LogLevel::ERROR and LogLevel::CRITICAL, ALERT and EMERGENCY.
*
* @param string $minLevel
* @return bool
*/
public function isPsrLevelGreaterOrEqualTo(string $minLevel) : bool
{
Expand Down
6 changes: 3 additions & 3 deletions src/ErrorBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ abstract class ErrorBag implements IErrorBag
/**
* @var Error[] array of Error objects belonging to this object.
*/
private $errors = [];
private array $errors = [];

/**
* @var bool true once the object has been validated.
*/
private $validated = false;
private bool $validated = false;

public function __construct()
{
Expand Down Expand Up @@ -66,7 +66,7 @@ protected function validate() : void
// do nothing
}

public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : IErrorBag
public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : static
{
$error = new Error($message, $psrLogLevel, $this, $exception);
$this->errors[] = $error;
Expand Down
10 changes: 5 additions & 5 deletions src/Header/AbstractHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ abstract class AbstractHeader extends ErrorBag implements IHeader
/**
* @var string the name of the header
*/
protected $name = '';
protected string $name;

/**
* @var IHeaderPart[] all parts not including CommentParts.
*/
protected $parts = [];
protected array $parts = [];

/**
* @var IHeaderPart[] the header's parts (as returned from the consumer),
* including commentParts
*/
protected $allParts = [];
protected array $allParts = [];

/**
* @var string[] array of comments, initialized on demand in getComments()
*/
private $comments;
private ?array $comments = null;

/**
* @var string the raw value
*/
protected $rawValue = '';
protected string $rawValue;

/**
* Assigns the header's name and raw value, then calls parseHeaderValue to
Expand Down
6 changes: 3 additions & 3 deletions src/Header/AddressHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class AddressHeader extends AbstractHeader
* @var AddressPart[] array of addresses, included all addresses contained
* in groups.
*/
protected $addresses = [];
protected array $addresses = [];

/**
* @var AddressGroupPart[] array of address groups (lists).
*/
protected $groups = [];
protected array $groups = [];

public function __construct(
AddressBaseConsumerService $consumerService,
Expand Down Expand Up @@ -108,7 +108,7 @@ public function hasAddress(string $email) : bool
public function getEmail() : ?string
{
if (!empty($this->addresses)) {
return $this->addresses->getEmail();
return $this->addresses[0]->getEmail();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Header/Consumer/AbstractGenericConsumerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function shouldAddSpace(HeaderPart $nextPart, HeaderPart $lastPart) : bo
* @param HeaderPart[] $parts
* @param HeaderPart[] $retParts
*/
private function addSpaceToRetParts(array $parts, array &$retParts, int $curIndex, HeaderPart &$spacePart, HeaderPart $lastPart) : self
private function addSpaceToRetParts(array $parts, array &$retParts, int $curIndex, HeaderPart &$spacePart, HeaderPart $lastPart) : static
{
$nextPart = $parts[$curIndex];
if ($this->shouldAddSpace($nextPart, $lastPart)) {
Expand All @@ -93,7 +93,7 @@ private function addSpaceToRetParts(array $parts, array &$retParts, int $curInde
* @param HeaderPart[] $parts
* @param HeaderPart[] $retParts
*/
private function addSpaces(array $parts, array &$retParts, int $curIndex, ?HeaderPart &$spacePart = null) : self
private function addSpaces(array $parts, array &$retParts, int $curIndex, ?HeaderPart &$spacePart = null) : static
{
$lastPart = \end($retParts);
if ($spacePart !== null && $curIndex < \count($parts) && $parts[$curIndex]->getValue() !== '' && $lastPart !== false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/MimeEncodedHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class MimeEncodedHeader extends AbstractHeader
/**
* @var MimeLiteralPartFactory for mime decoding.
*/
protected $mimeLiteralPartFactory;
protected MimeLiteralPartFactory $mimeLiteralPartFactory;

public function __construct(
MimeLiteralPartFactory $mimeLiteralPartFactory,
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ParameterHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ParameterHeader extends AbstractHeader
* @var ParameterPart[] key map of lower-case parameter names and associated
* ParameterParts.
*/
protected $parameters = [];
protected array $parameters = [];

public function __construct(
ParameterConsumerService $consumerService,
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 @@ -21,7 +21,7 @@ class HeaderPartFactory
* @var MbWrapper $charsetConverter passed to IHeaderPart constructors
* for converting strings in IHeaderPart::convertEncoding
*/
protected $charsetConverter;
protected MbWrapper $charsetConverter;

/**
* Sets up dependencies.
Expand Down
8 changes: 4 additions & 4 deletions src/Header/Part/MimeLiteralPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class MimeLiteralPart extends LiteralPart
/**
* @var bool set to true to ignore spaces before this part
*/
protected $canIgnoreSpacesBefore = false;
protected bool $canIgnoreSpacesBefore = false;

/**
* @var bool set to true to ignore spaces after this part
*/
protected $canIgnoreSpacesAfter = false;
protected bool $canIgnoreSpacesAfter = false;

/**
* @var array<int,array<string,string>> maintains an array mapping rfc1766
Expand All @@ -46,7 +46,7 @@ class MimeLiteralPart extends LiteralPart
* Each array element is an array containing two elements, one with key
* 'lang', and another with key 'value'.
*/
protected $languages = [];
protected array $languages = [];

/**
* Decoding the passed token value if it's mime-encoded and assigns the
Expand Down Expand Up @@ -155,7 +155,7 @@ public function ignoreSpacesAfter() : bool
/**
* Adds the passed part into the languages array with the given language.
*/
protected function addToLanguage(string $part, ?string $language = null) : self
protected function addToLanguage(string $part, ?string $language = null) : static
{
$this->languages[] = [
'lang' => $language,
Expand Down
4 changes: 2 additions & 2 deletions src/Header/Part/ParameterPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class ParameterPart extends MimeLiteralPart
/**
* @var string the name of the parameter
*/
protected $name;
protected string $name;

/**
* @var string the RFC-1766 language tag if set.
*/
protected $language;
protected ?string $language = null;

/**
* Constructs a ParameterPart out of a name/value pair. The name and
Expand Down
6 changes: 3 additions & 3 deletions src/Header/Part/ReceivedDomainPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class ReceivedDomainPart extends ReceivedPart
/**
* @var string The name used to identify the server in the EHLO line.
*/
protected $ehloName;
protected ?string $ehloName;

/**
* @var string The hostname.
*/
protected $hostname;
protected ?string $hostname;

/**
* @var string The address.
*/
protected $address;
protected ?string $address;

public function __construct(
MbWrapper $charsetConverter,
Expand Down
22 changes: 9 additions & 13 deletions src/Header/Part/SplitParameterToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ class SplitParameterToken extends HeaderPart
/**
* @var string name of the parameter.
*/
protected $name;
protected string $name;

/**
* @var string[] keeps encoded parts values that need to be decoded. Keys
* are set to the index part of the split parameter and used for
* sorting before decoding/concatenating.
*/
protected $encodedParts = [];
protected array $encodedParts = [];

/**
* @var string[] contains literal parts that don't require any decoding (and
* are therefore ISO-8859-1 (technically should be 7bit US-ASCII but
* allowing 8bit shouldn't be an issue as elsewhere in MMP).
*/
protected $literalParts = [];
protected array $literalParts = [];

/**
* @var string RFC-1766 (or subset) language code with optional subtags,
* regions, etc...
*/
protected $language;
protected ?string $language = null;

/**
* @var string charset of content in $encodedParts.
*/
protected $charset = 'ISO-8859-1';
protected string $charset = 'ISO-8859-1';

/**
* Initializes a SplitParameterToken.
Expand All @@ -66,7 +66,7 @@ public function __construct(MbWrapper $charsetConverter, string $name)
* current object if $index is 0 and adds the value part to the encodedParts
* array.
*/
protected function extractMetaInformationAndValue(string $value, int $index) : self
protected function extractMetaInformationAndValue(string $value, int $index) : static
{
if (\preg_match('~^([^\']*)\'([^\']*)\'(.*)$~', $value, $matches)) {
if ($index === 0) {
Expand All @@ -93,7 +93,7 @@ protected function extractMetaInformationAndValue(string $value, int $index) : s
* @param bool $isEncoded
* @param int $index
*/
public function addPart($value, $isEncoded, $index) : self
public function addPart($value, $isEncoded, $index) : static
{
if (empty($index)) {
$index = 0;
Expand Down Expand Up @@ -163,20 +163,16 @@ function($carry, $item) {

/**
* Returns the name of the parameter.
*
* @return string
*/
public function getName()
public function getName() : string
{
return $this->name;
}

/**
* Returns the language of the parameter if set, or null if not.
*
* @return string
*/
public function getLanguage()
public function getLanguage() : ?string
{
return $this->language;
}
Expand Down
Loading

0 comments on commit 46ed757

Please sign in to comment.