diff --git a/src/Error.php b/src/Error.php index 3c2812a5..22b34725 100644 --- a/src/Error.php +++ b/src/Error.php @@ -7,7 +7,7 @@ namespace ZBateson\MailMimeParser; -use Exception; +use Throwable; use InvalidArgumentException; use Psr\Log\LogLevel; @@ -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, @@ -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'); @@ -68,8 +68,6 @@ public function __construct(string $message, string $psrLogLevelAsErrorLevel, Er /** * Returns the error message. - * - * @return string */ public function getMessage() : string { @@ -78,8 +76,6 @@ public function getMessage() : string /** * Returns the PSR string log level for this error message. - * - * @return string */ public function getPsrLevel() : string { @@ -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 { @@ -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; } @@ -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 { diff --git a/src/ErrorBag.php b/src/ErrorBag.php index 32505914..a441ffca 100644 --- a/src/ErrorBag.php +++ b/src/ErrorBag.php @@ -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() { @@ -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; diff --git a/src/Header/AbstractHeader.php b/src/Header/AbstractHeader.php index 94fd990a..6d3ca1e3 100644 --- a/src/Header/AbstractHeader.php +++ b/src/Header/AbstractHeader.php @@ -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 diff --git a/src/Header/AddressHeader.php b/src/Header/AddressHeader.php index 6cc7bdfd..92e7acfb 100644 --- a/src/Header/AddressHeader.php +++ b/src/Header/AddressHeader.php @@ -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, @@ -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(); } } diff --git a/src/Header/Consumer/AbstractGenericConsumerService.php b/src/Header/Consumer/AbstractGenericConsumerService.php index bcaef23e..b89a1cae 100644 --- a/src/Header/Consumer/AbstractGenericConsumerService.php +++ b/src/Header/Consumer/AbstractGenericConsumerService.php @@ -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)) { @@ -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) { diff --git a/src/Header/MimeEncodedHeader.php b/src/Header/MimeEncodedHeader.php index b7ca96d2..29b3885d 100644 --- a/src/Header/MimeEncodedHeader.php +++ b/src/Header/MimeEncodedHeader.php @@ -22,7 +22,7 @@ abstract class MimeEncodedHeader extends AbstractHeader /** * @var MimeLiteralPartFactory for mime decoding. */ - protected $mimeLiteralPartFactory; + protected MimeLiteralPartFactory $mimeLiteralPartFactory; public function __construct( MimeLiteralPartFactory $mimeLiteralPartFactory, diff --git a/src/Header/ParameterHeader.php b/src/Header/ParameterHeader.php index 88f2437e..af8de1a9 100644 --- a/src/Header/ParameterHeader.php +++ b/src/Header/ParameterHeader.php @@ -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, diff --git a/src/Header/Part/HeaderPartFactory.php b/src/Header/Part/HeaderPartFactory.php index 4d1f1423..38f41384 100644 --- a/src/Header/Part/HeaderPartFactory.php +++ b/src/Header/Part/HeaderPartFactory.php @@ -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. diff --git a/src/Header/Part/MimeLiteralPart.php b/src/Header/Part/MimeLiteralPart.php index 131eab6a..3e8f132c 100644 --- a/src/Header/Part/MimeLiteralPart.php +++ b/src/Header/Part/MimeLiteralPart.php @@ -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> maintains an array mapping rfc1766 @@ -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 @@ -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, diff --git a/src/Header/Part/ParameterPart.php b/src/Header/Part/ParameterPart.php index 44c43d83..7a403ce1 100644 --- a/src/Header/Part/ParameterPart.php +++ b/src/Header/Part/ParameterPart.php @@ -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 diff --git a/src/Header/Part/ReceivedDomainPart.php b/src/Header/Part/ReceivedDomainPart.php index ce6af91e..5819dd67 100644 --- a/src/Header/Part/ReceivedDomainPart.php +++ b/src/Header/Part/ReceivedDomainPart.php @@ -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, diff --git a/src/Header/Part/SplitParameterToken.php b/src/Header/Part/SplitParameterToken.php index d2f890c0..4a06fe93 100644 --- a/src/Header/Part/SplitParameterToken.php +++ b/src/Header/Part/SplitParameterToken.php @@ -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. @@ -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) { @@ -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; @@ -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; } diff --git a/src/Header/ReceivedHeader.php b/src/Header/ReceivedHeader.php index 8fa24743..fd32344b 100644 --- a/src/Header/ReceivedHeader.php +++ b/src/Header/ReceivedHeader.php @@ -79,9 +79,14 @@ class ReceivedHeader extends ParameterHeader { /** - * @var DateTime|bool the date/time stamp in the header. + * @var DateTime the date/time stamp in the header. */ - private $date = false; + private ?DateTime $date = null; + + /** + * @var bool set to true once $date has been looked for + */ + private bool $dateSet = false; public function __construct( ReceivedConsumerService $consumerService, @@ -208,13 +213,13 @@ public function getByAddress() : ?string */ public function getDateTime() : ?DateTime { - if ($this->date === false) { - $this->date = null; + if ($this->dateSet === false) { foreach ($this->parts as $part) { if ($part instanceof DatePart) { $this->date = $part->getDateTime(); } } + $this->dateSet = true; } return $this->date; } diff --git a/src/IErrorBag.php b/src/IErrorBag.php index 81771b02..7f1860a7 100644 --- a/src/IErrorBag.php +++ b/src/IErrorBag.php @@ -21,50 +21,39 @@ interface IErrorBag /** * Returns a context name for the current object to help identify it in * logs. - * - * @return string */ public function getErrorLoggingContextName() : string; /** * Creates and adds an Error object to this ErrorBag. - * - * @param Error $error - * @return self */ - public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : IErrorBag; + public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : static; /** * Returns true if this object has an error in its error bag at or above * the passed $minPsrLevel (defaults to ERROR). If $validate is true, * additional validation may be performed. * - * @param bool $validate - * @param string $minPsrLevel - * @return bool + * The PSR levels are defined in Psr\Log\LogLevel. */ public function hasErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool; /** * Returns any local errors this object has at or above the passed PSR log - * level (defaulting to LogLevel::ERROR). + * level in Psr\Log\LogLevel (defaulting to LogLevel::ERROR). * * If $validate is true, additional validation may be performed on the * object to check for errors. - * - * @param bool $validate - * @param string $minPsrLevel - * @return array */ public function getErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array; /** * Returns true if there are errors on this object, or any IErrorBag child - * of this object at or above the passed PSR log level (defaulting to - * LogLevel::ERROR). Note that this will stop after finding the first error - * and return, so may be slightly more performant if an error actually - * exists over calling getAllErrors if only interested in whether an error - * exists. + * of this object at or above the passed PSR log level in Psr\Log\LogLevel + * (defaulting to LogLevel::ERROR). Note that this will stop after finding + * the first error and return, so may be slightly more performant if an + * error actually exists over calling getAllErrors if only interested in + * whether an error exists. * * Care should be taken using this if the intention is to only 'preview' a * message without parsing it entirely, since this will cause the whole @@ -73,17 +62,13 @@ public function getErrors(bool $validate = false, string $minPsrLevel = LogLevel * * If $validate is true, additional validation may be performed to check for * errors. - * - * @param bool $validate - * @param string $minPsrLevel - * @return bool */ public function hasAnyErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool; /** * Returns any errors on this object, and all IErrorBag children of this - * object at or above the passed PSR log level (defaulting to - * LogLevel::ERROR). + * object at or above the passed PSR log level from Psr\Log\LogLevel + * (defaulting to LogLevel::ERROR). * * Care should be taken using this if the intention is to only 'preview' a * message without parsing it entirely, since this will cause the whole @@ -92,10 +77,6 @@ public function hasAnyErrors(bool $validate = false, string $minPsrLevel = LogLe * * If $validate is true, additional validation may be performed on children * to check for errors. - * - * @param bool $validate - * @param string $minPsrLevel - * @return array */ public function getAllErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array; } diff --git a/src/IMessage.php b/src/IMessage.php index 9a7c7139..8e799bf5 100644 --- a/src/IMessage.php +++ b/src/IMessage.php @@ -8,6 +8,8 @@ namespace ZBateson\MailMimeParser; use ZBateson\MailMimeParser\Message\IMimePart; +use ZBateson\MailMimeParser\Message\IMessagePart; +use Psr\Http\Message\StreamInterface; /** * An interface representing an email message. @@ -35,9 +37,8 @@ interface IMessage extends IMimePart * @see IMessage::getHtmlPart() to get the HTML part(s). * @see IMessage::getHtmlPartCount() to get a count of html parts. * @param int $index Optional index of part to return. - * @return \ZBateson\MailMimeParser\Message\IMessagePart|null */ - public function getTextPart($index = 0); + public function getTextPart(int $index = 0) : ?IMessagePart; /** * Returns the number of inline text/plain parts this message contains. @@ -45,9 +46,8 @@ public function getTextPart($index = 0); * @see IMessage::getTextPart() to get the text part(s). * @see IMessage::getHtmlPart() to get the HTML part(s). * @see IMessage::getHtmlPartCount() to get a count of html parts. - * @return int */ - public function getTextPartCount(); + public function getTextPartCount() : int; /** * Returns the inline text/html IMessagePart for a message. @@ -65,9 +65,8 @@ public function getTextPartCount(); * @see IMessage::getTextPartCount() to get a count of text parts. * @see IMessage::getHtmlPartCount() to get a count of html parts. * @param int $index Optional index of part to return. - * @return \ZBateson\MailMimeParser\Message\IMessagePart|null */ - public function getHtmlPart($index = 0); + public function getHtmlPart(int $index = 0) : ?IMessagePart; /** * Returns the number of inline text/html parts this message contains. @@ -75,9 +74,8 @@ public function getHtmlPart($index = 0); * @see IMessage::getTextPart() to get the text part(s). * @see IMessage::getTextPartCount() to get a count of text parts. * @see IMessage::getHtmlPart() to get the HTML part(s). - * @return int */ - public function getHtmlPartCount(); + public function getHtmlPartCount() : int; /** * Returns a Psr7 Stream for the 'inline' text/plain content. @@ -92,9 +90,8 @@ public function getHtmlPartCount(); * @see IMessage::getTextContent() to get the text content in a string. * @param int $index Optional 0-based index of inline text part stream. * @param string $charset Optional charset to encode the stream with. - * @return \Psr\Http\Message\StreamInterface|null */ - public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET); + public function getTextStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface; /** * Returns the content of the inline text/plain part as a string. @@ -110,9 +107,8 @@ public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHA * @param int $index Optional 0-based index of inline text part content. * @param string $charset Optional charset for the returned string to be * encoded in. - * @return string|null */ - public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET); + public function getTextContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string; /** * Returns a Psr7 Stream for the 'inline' text/html content. @@ -127,9 +123,8 @@ public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CH * @see IMessage::getHtmlContent() to get the html content in a string. * @param int $index Optional 0-based index of inline html part stream. * @param string $charset Optional charset to encode the stream with. - * @return \Psr\Http\Message\StreamInterface|null */ - public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET); + public function getHtmlStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface; /** * Returns the content of the inline text/html part as a string. @@ -145,9 +140,8 @@ public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHA * @param int $index Optional 0-based index of inline html part content. * @param string $charset Optional charset for the returned string to be * encoded in. - * @return string|null */ - public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET); + public function getHtmlContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string; /** * Sets the text/plain part of the message to the passed $resource, either @@ -166,7 +160,7 @@ public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CH * @param string $contentTypeCharset the charset to use as the text/plain * part's content-type header charset value. */ - public function setTextPart($resource, string $contentTypeCharset = 'UTF-8'); + public function setTextPart(mixed $resource, string $contentTypeCharset = 'UTF-8') : static; /** * Sets the text/html part of the message to the passed $resource, either @@ -185,7 +179,7 @@ public function setTextPart($resource, string $contentTypeCharset = 'UTF-8'); * @param string $contentTypeCharset the charset to use as the text/html * part's content-type header charset value. */ - public function setHtmlPart($resource, string $contentTypeCharset = 'UTF-8'); + public function setHtmlPart(mixed $resource, string $contentTypeCharset = 'UTF-8') : static; /** * Removes the text/plain part of the message at the passed index if one @@ -285,9 +279,8 @@ public function removeAllHtmlParts(bool $moveRelatedPartsBelowMessage = true) : * @see IMessage::getAllAttachmentParts() to get an array of all parts. * @see IMessage::getAttachmentCount() to get the number of attachments. * @param int $index the 0-based index of the attachment part to return. - * @return \ZBateson\MailMimeParser\Message\IMessagePart|null */ - public function getAttachmentPart(int $index); + public function getAttachmentPart(int $index) : ?IMessagePart; /** * Returns all attachment parts. @@ -301,18 +294,17 @@ public function getAttachmentPart(int $index); * * @see IMessage::getAllAttachmentPart() to get a single attachment. * @see IMessage::getAttachmentCount() to get the number of attachments. - * @return \ZBateson\MailMimeParser\Message\IMessagePart[] + * @return IMessagePart[] */ - public function getAllAttachmentParts(); + public function getAllAttachmentParts() : array; /** * Returns the number of attachments available. * * @see IMessage::getAllAttachmentPart() to get a single attachment. * @see IMessage::getAllAttachmentParts() to get an array of all parts. - * @return int */ - public function getAttachmentCount(); + public function getAttachmentCount() : int; /** * Adds an attachment part for the passed raw data string, handle, or stream @@ -329,7 +321,7 @@ public function getAttachmentCount(); * @param string $encoding defaults to 'base64', only applied for a mime * email */ - public function addAttachmentPart($resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64'); + public function addAttachmentPart(mixed $resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static; /** * Adds an attachment part using the passed file. @@ -347,7 +339,7 @@ public function addAttachmentPart($resource, string $mimeType, ?string $filename * @param string $encoding defaults to 'base64', only applied for a mime * email */ - public function addAttachmentPartFromFile($filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64'); + public function addAttachmentPartFromFile(string $filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static; /** * Removes the attachment at the given index. @@ -358,9 +350,8 @@ public function addAttachmentPartFromFile($filePath, string $mimeType, ?string $ * 'attachment' * - all multipart/* parts * - any signature part - * */ - public function removeAttachmentPart(int $index); + public function removeAttachmentPart(int $index) : static; /** * Returns a stream that can be used to read the content part of a signed @@ -376,10 +367,9 @@ public function removeAttachmentPart(int $index); * * @see IMessage::getSignedMessageAsString to get a string with CRLFs * normalized - * @return \Psr\Http\Message\StreamInterface or null if the message doesn't - * have any children + * @return ?StreamInterface null if the message doesn't have any children */ - public function getSignedMessageStream(); + public function getSignedMessageStream() : ?StreamInterface; /** * Returns a string containing the entire body of a signed message for @@ -389,9 +379,9 @@ public function getSignedMessageStream(); * * @see IMessage::setAsMultipartSigned to make the message a * multipart/signed message. - * @return string or null if the message doesn't have any children + * @return ?string null if the message doesn't have any children */ - public function getSignedMessageAsString(); + public function getSignedMessageAsString() : ?string; /** * Returns the signature part of a multipart/signed message or null. @@ -402,10 +392,8 @@ public function getSignedMessageAsString(); * Using the 'protocol' parameter of the Content-Type header is unreliable * in some instances (for instance a difference of x-pgp-signature versus * pgp-signature). - * - * @return IMimePart */ - public function getSignaturePart(); + public function getSignaturePart() : ?IMessagePart; /** * Turns the message into a multipart/signed message, moving the actual @@ -420,7 +408,7 @@ public function getSignaturePart(); * @param string $micalg The Message Integrity Check algorithm being used * @param string $protocol The mime-type of the signature body */ - public function setAsMultipartSigned(string $micalg, string $protocol); + public function setAsMultipartSigned(string $micalg, string $protocol) : static; /** * Sets the signature body of the message to the passed $body for a @@ -428,12 +416,12 @@ public function setAsMultipartSigned(string $micalg, string $protocol); * * @param string $body the message's hash */ - public function setSignature(string $body); + public function setSignature(string $body) : static; /** * Returns the value of the 'Message-ID' header, or null if not set. * - * @return string|null the content ID. + * @return string|null the ID. */ public function getMessageId() : ?string; } diff --git a/src/MailMimeParser.php b/src/MailMimeParser.php index 9e5778db..b3c1846e 100644 --- a/src/MailMimeParser.php +++ b/src/MailMimeParser.php @@ -160,9 +160,8 @@ public function __construct(?LoggerInterface $logger = null, array|string|Defini * mime message. * @param bool $attached pass true to have it attached to the returned * IMessage and destroyed with it. - * @return IMessage */ - public function parse($resource, $attached) : IMessage + public function parse(mixed $resource, bool $attached) : IMessage { $stream = Utils::streamFor( $resource, diff --git a/src/Message.php b/src/Message.php index 160f5c51..cbd99547 100644 --- a/src/Message.php +++ b/src/Message.php @@ -10,6 +10,7 @@ use GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; use ZBateson\MailMimeParser\Header\HeaderConsts; +use ZBateson\MailMimeParser\Message\IMessagePart; use ZBateson\MailMimeParser\Message\Helper\MultipartHelper; use ZBateson\MailMimeParser\Message\Helper\PrivacyHelper; use ZBateson\MailMimeParser\Message\MimePart; @@ -31,13 +32,13 @@ class Message extends MimePart implements IMessage /** * @var MultipartHelper service providing functions for multipart messages. */ - private $multipartHelper; + private MultipartHelper $multipartHelper; /** * @var PrivacyHelper service providing functions for multipart/signed * messages. */ - private $privacyHelper; + private PrivacyHelper $privacyHelper; public function __construct( ?PartStreamContainer $streamContainer = null, @@ -77,9 +78,8 @@ public function __construct( * mime message. * @param bool $attached pass true to have it attached to the returned * IMessage and destroyed with it. - * @return IMessage */ - public static function from($resource, $attached) + public static function from(mixed $resource, bool $attached) : IMessage { static $mmp = null; if ($mmp === null) { @@ -102,7 +102,7 @@ public function isMime() : bool return ($contentType !== null || $mimeVersion !== null); } - public function getTextPart($index = 0) + public function getTextPart(int $index = 0) : ?IMessagePart { return $this->getPart( $index, @@ -110,14 +110,14 @@ public function getTextPart($index = 0) ); } - public function getTextPartCount() + public function getTextPartCount() : int { return $this->getPartCount( PartFilter::fromInlineContentType('text/plain') ); } - public function getHtmlPart($index = 0) + public function getHtmlPart(int $index = 0) : ?IMessagePart { return $this->getPart( $index, @@ -125,14 +125,14 @@ public function getHtmlPart($index = 0) ); } - public function getHtmlPartCount() + public function getHtmlPartCount() : int { return $this->getPartCount( PartFilter::fromInlineContentType('text/html') ); } - public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) + public function getTextStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface { $textPart = $this->getTextPart($index); if ($textPart !== null) { @@ -141,7 +141,7 @@ public function getTextStream($index = 0, $charset = MailMimeParser::DEFAULT_CHA return null; } - public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) + public function getTextContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string { $part = $this->getTextPart($index); if ($part !== null) { @@ -150,7 +150,7 @@ public function getTextContent($index = 0, $charset = MailMimeParser::DEFAULT_CH return null; } - public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) + public function getHtmlStream(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface { $htmlPart = $this->getHtmlPart($index); if ($htmlPart !== null) { @@ -159,7 +159,7 @@ public function getHtmlStream($index = 0, $charset = MailMimeParser::DEFAULT_CHA return null; } - public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CHARSET) + public function getHtmlContent(int $index = 0, string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string { $part = $this->getHtmlPart($index); if ($part !== null) { @@ -168,10 +168,7 @@ public function getHtmlContent($index = 0, $charset = MailMimeParser::DEFAULT_CH return null; } - /** - * @return static - */ - public function setTextPart($resource, string $charset = 'UTF-8') + public function setTextPart(mixed $resource, string $charset = 'UTF-8') : static { $this->multipartHelper ->setContentPartForMimeType( @@ -183,7 +180,7 @@ public function setTextPart($resource, string $charset = 'UTF-8') return $this; } - public function setHtmlPart($resource, string $charset = 'UTF-8') : self + public function setHtmlPart(mixed $resource, string $charset = 'UTF-8') : static { $this->multipartHelper ->setContentPartForMimeType( @@ -235,7 +232,7 @@ public function removeAllHtmlParts(bool $moveRelatedPartsBelowMessage = true) : ); } - public function getAttachmentPart(int $index) + public function getAttachmentPart(int $index) : ?IMessagePart { return $this->getPart( $index, @@ -243,7 +240,7 @@ public function getAttachmentPart(int $index) ); } - public function getAllAttachmentParts() + public function getAllAttachmentParts() : array { return $this->getAllParts( PartFilter::fromAttachmentFilter() @@ -255,10 +252,7 @@ public function getAttachmentCount() : int return \count($this->getAllAttachmentParts()); } - /** - * @return static - */ - public function addAttachmentPart($resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') + public function addAttachmentPart(mixed $resource, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static { $this->multipartHelper ->createAndAddPartForAttachment( @@ -272,10 +266,7 @@ public function addAttachmentPart($resource, string $mimeType, ?string $filename return $this; } - /** - * @return static - */ - public function addAttachmentPartFromFile($filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') + public function addAttachmentPartFromFile(string $filePath, string $mimeType, ?string $filename = null, string $disposition = 'attachment', string $encoding = 'base64') : static { $handle = Psr7\Utils::streamFor(\fopen($filePath, 'r')); if ($filename === null) { @@ -285,47 +276,43 @@ public function addAttachmentPartFromFile($filePath, string $mimeType, ?string $ return $this; } - public function removeAttachmentPart(int $index) : self + public function removeAttachmentPart(int $index) : static { $part = $this->getAttachmentPart($index); $this->removePart($part); return $this; } - public function getSignedMessageStream() + public function getSignedMessageStream() : ?StreamInterface { return $this ->privacyHelper ->getSignedMessageStream($this); } - public function getSignedMessageAsString() + public function getSignedMessageAsString() : ?string { return $this ->privacyHelper ->getSignedMessageAsString($this); } - public function getSignaturePart() + public function getSignaturePart() : ?IMessagePart { if (\strcasecmp($this->getContentType(), 'multipart/signed') === 0) { return $this->getChild(1); } - return null; - + return null; } - /** - * @return static - */ - public function setAsMultipartSigned(string $micalg, string $protocol) + public function setAsMultipartSigned(string $micalg, string $protocol) : static { $this->privacyHelper ->setMessageAsMultipartSigned($this, $micalg, $protocol); return $this; } - public function setSignature(string $body) : self + public function setSignature(string $body) : static { $this->privacyHelper ->setSignature($this, $body); diff --git a/src/Message/Factory/IMessagePartFactory.php b/src/Message/Factory/IMessagePartFactory.php index 71e78ef4..c8daa3d8 100644 --- a/src/Message/Factory/IMessagePartFactory.php +++ b/src/Message/Factory/IMessagePartFactory.php @@ -8,6 +8,7 @@ namespace ZBateson\MailMimeParser\Message\Factory; use ZBateson\MailMimeParser\Stream\StreamFactory; +use ZBateson\MailMimeParser\Message\IMessagePart; /** * Abstract factory for subclasses of IMessagePart. @@ -19,12 +20,12 @@ abstract class IMessagePartFactory /** * @var StreamFactory */ - protected $streamFactory; + protected StreamFactory $streamFactory; /** * @var PartStreamContainerFactory */ - protected $partStreamContainerFactory; + protected PartStreamContainerFactory $partStreamContainerFactory; public function __construct( StreamFactory $streamFactory, @@ -36,8 +37,6 @@ public function __construct( /** * Constructs a new IMessagePart object and returns it - * - * @return \ZBateson\MailMimeParser\Message\IMessagePart */ - abstract public function newInstance(); + abstract public function newInstance() : IMessagePart; } diff --git a/src/Message/Factory/IMimePartFactory.php b/src/Message/Factory/IMimePartFactory.php index 7efa7833..30178442 100644 --- a/src/Message/Factory/IMimePartFactory.php +++ b/src/Message/Factory/IMimePartFactory.php @@ -7,6 +7,7 @@ namespace ZBateson\MailMimeParser\Message\Factory; +use ZBateson\MailMimeParser\Message\IMimePart; use ZBateson\MailMimeParser\Message\MimePart; use ZBateson\MailMimeParser\Stream\StreamFactory; @@ -20,12 +21,12 @@ class IMimePartFactory extends IMessagePartFactory /** * @var PartHeaderContainerFactory */ - protected $partHeaderContainerFactory; + protected PartHeaderContainerFactory $partHeaderContainerFactory; /** * @var PartChildrenContainerFactory */ - protected $partChildrenContainerFactory; + protected PartChildrenContainerFactory $partChildrenContainerFactory; public function __construct( StreamFactory $streamFactory, @@ -40,10 +41,8 @@ public function __construct( /** * Constructs a new IMimePart object and returns it - * - * @return \ZBateson\MailMimeParser\Message\IMimePart */ - public function newInstance() + public function newInstance() : IMimePart { $streamContainer = $this->partStreamContainerFactory->newInstance(); $headerContainer = $this->partHeaderContainerFactory->newInstance(); diff --git a/src/Message/Factory/IUUEncodedPartFactory.php b/src/Message/Factory/IUUEncodedPartFactory.php index 4a003efe..de267e7e 100644 --- a/src/Message/Factory/IUUEncodedPartFactory.php +++ b/src/Message/Factory/IUUEncodedPartFactory.php @@ -7,6 +7,7 @@ namespace ZBateson\MailMimeParser\Message\Factory; +use ZBateson\MailMimeParser\Message\IUUEncodedPart; use ZBateson\MailMimeParser\Message\UUEncodedPart; /** @@ -18,10 +19,8 @@ class IUUEncodedPartFactory extends IMessagePartFactory { /** * Constructs a new UUEncodedPart object and returns it - * - * @return \ZBateson\MailMimeParser\Message\IUUEncodedPart */ - public function newInstance() + public function newInstance() : IUUEncodedPart { $streamContainer = $this->partStreamContainerFactory->newInstance(); $part = new UUEncodedPart( diff --git a/src/Message/Factory/PartChildrenContainerFactory.php b/src/Message/Factory/PartChildrenContainerFactory.php index 808cb292..d5c103b1 100644 --- a/src/Message/Factory/PartChildrenContainerFactory.php +++ b/src/Message/Factory/PartChildrenContainerFactory.php @@ -16,7 +16,7 @@ */ class PartChildrenContainerFactory { - public function newInstance() + public function newInstance() : PartChildrenContainer { return new PartChildrenContainer(); } diff --git a/src/Message/Factory/PartHeaderContainerFactory.php b/src/Message/Factory/PartHeaderContainerFactory.php index ab7198df..71100396 100644 --- a/src/Message/Factory/PartHeaderContainerFactory.php +++ b/src/Message/Factory/PartHeaderContainerFactory.php @@ -20,7 +20,7 @@ class PartHeaderContainerFactory /** * @var HeaderFactory the HeaderFactory passed to HeaderContainer instances. */ - protected $headerFactory; + protected HeaderFactory $headerFactory; /** * Constructor @@ -33,10 +33,8 @@ public function __construct(HeaderFactory $headerFactory) /** * Creates and returns a PartHeaderContainer. - * - * @return PartHeaderContainer */ - public function newInstance(?PartHeaderContainer $from = null) + public function newInstance(?PartHeaderContainer $from = null) : PartHeaderContainer { return new PartHeaderContainer($this->headerFactory, $from); } diff --git a/src/Message/Factory/PartStreamContainerFactory.php b/src/Message/Factory/PartStreamContainerFactory.php index 8d0a6d7b..a3e0cdae 100644 --- a/src/Message/Factory/PartStreamContainerFactory.php +++ b/src/Message/Factory/PartStreamContainerFactory.php @@ -20,14 +20,14 @@ class PartStreamContainerFactory /** * @var StreamFactory */ - protected $streamFactory; + protected StreamFactory $streamFactory; public function __construct(StreamFactory $streamFactory) { $this->streamFactory = $streamFactory; } - public function newInstance() + public function newInstance() : PartStreamContainer { return new PartStreamContainer($this->streamFactory); } diff --git a/src/Message/Helper/AbstractHelper.php b/src/Message/Helper/AbstractHelper.php index 40e72f09..ede79c77 100644 --- a/src/Message/Helper/AbstractHelper.php +++ b/src/Message/Helper/AbstractHelper.php @@ -20,12 +20,12 @@ abstract class AbstractHelper /** * @var IMimePartFactory to create parts for attachments/content */ - protected $mimePartFactory; + protected IMimePartFactory $mimePartFactory; /** * @var IUUEncodedPartFactory to create parts for attachments */ - protected $uuEncodedPartFactory; + protected IUUEncodedPartFactory $uuEncodedPartFactory; public function __construct( IMimePartFactory $mimePartFactory, diff --git a/src/Message/Helper/GenericHelper.php b/src/Message/Helper/GenericHelper.php index 9fc3d8ef..25856b31 100644 --- a/src/Message/Helper/GenericHelper.php +++ b/src/Message/Helper/GenericHelper.php @@ -24,7 +24,7 @@ class GenericHelper extends AbstractHelper * @var string[] non mime content fields that are not related to the content * of a part. */ - private static $nonMimeContentFields = ['contentreturn', 'contentidentifier']; + private static array $nonMimeContentFields = ['contentreturn', 'contentidentifier']; /** * Returns true if the passed header's name is a Content-* header other than @@ -44,13 +44,14 @@ private function isMimeContentField(IHeader $header, array $exceptions = []) : b * @param string $header * @param string $default */ - public function copyHeader(IMimePart $from, IMimePart $to, $header, $default = null) + public function copyHeader(IMimePart $from, IMimePart $to, $header, $default = null) : static { $fromHeader = $from->getHeader($header); $set = ($fromHeader !== null) ? $fromHeader->getRawValue() : $default; if ($set !== null) { $to->setRawHeader($header, $set); } + return $this; } /** @@ -60,7 +61,7 @@ public function copyHeader(IMimePart $from, IMimePart $to, $header, $default = n * An exception is made for the obsolete Content-Return header, which isn't * isn't a MIME content field and so isn't removed. */ - public function removeContentHeadersAndContent(IMimePart $part) : self + public function removeContentHeadersAndContent(IMimePart $part) : static { foreach ($part->getAllHeaders() as $header) { if ($this->isMimeContentField($header)) { @@ -81,7 +82,7 @@ public function removeContentHeadersAndContent(IMimePart $part) : self * * @param bool $move */ - public function copyContentHeadersAndContent(IMimePart $from, IMimePart $to, $move = false) + public function copyContentHeadersAndContent(IMimePart $from, IMimePart $to, $move = false) : static { $this->copyHeader($from, $to, HeaderConsts::CONTENT_TYPE, 'text/plain; charset=utf-8'); if ($from->getHeader(HeaderConsts::CONTENT_TYPE) === null) { @@ -100,6 +101,7 @@ public function copyContentHeadersAndContent(IMimePart $from, IMimePart $to, $mo if ($move) { $this->removeContentHeadersAndContent($from); } + return $this; } /** @@ -109,7 +111,7 @@ public function copyContentHeadersAndContent(IMimePart $from, IMimePart $to, $mo * * @return IMimePart the newly-created IMimePart */ - public function createNewContentPartFrom(IMimePart $part) + public function createNewContentPartFrom(IMimePart $part) : IMimePart { $mime = $this->mimePartFactory->newInstance(); $this->copyContentHeadersAndContent($part, $mime, true); @@ -123,7 +125,7 @@ public function createNewContentPartFrom(IMimePart $part) * removing them from $from and adding them to $to. * */ - public function movePartContentAndChildren(IMimePart $from, IMimePart $to) + public function movePartContentAndChildren(IMimePart $from, IMimePart $to) : static { $this->copyContentHeadersAndContent($from, $to, true); if ($from->getChildCount() > 0) { @@ -132,6 +134,7 @@ public function movePartContentAndChildren(IMimePart $from, IMimePart $to) $to->addChild($child); } } + return $this; } /** @@ -142,7 +145,7 @@ public function movePartContentAndChildren(IMimePart $from, IMimePart $to) * replaced, and instead $replacement's type headers are copied to $message, * and any children below $replacement are added directly below $message. */ - public function replacePart(IMessage $message, IMimePart $part, IMimePart $replacement) : self + public function replacePart(IMessage $message, IMimePart $part, IMimePart $replacement) : static { $position = $message->removePart($replacement); if ($part === $message) { diff --git a/src/Message/Helper/MultipartHelper.php b/src/Message/Helper/MultipartHelper.php index 8d80af58..7104c2a2 100644 --- a/src/Message/Helper/MultipartHelper.php +++ b/src/Message/Helper/MultipartHelper.php @@ -27,10 +27,13 @@ class MultipartHelper extends AbstractHelper /** * @var GenericHelper a GenericHelper instance */ - private $genericHelper; + private GenericHelper $genericHelper; - public function __construct(IMimePartFactory $mimePartFactory, IUUEncodedPartFactory $uuEncodedPartFactory, GenericHelper $genericHelper) - { + public function __construct( + IMimePartFactory $mimePartFactory, + IUUEncodedPartFactory $uuEncodedPartFactory, + GenericHelper $genericHelper + ) { parent::__construct($mimePartFactory, $uuEncodedPartFactory); $this->genericHelper = $genericHelper; } @@ -51,7 +54,7 @@ public function getUniqueBoundary(string $mimeType) : string * Creates a unique mime boundary and assigns it to the passed part's * Content-Type header with the passed mime type. */ - public function setMimeHeaderBoundaryOnPart(IMimePart $part, string $mimeType) : self + public function setMimeHeaderBoundaryOnPart(IMimePart $part, string $mimeType) : static { $part->setRawHeader( HeaderConsts::CONTENT_TYPE, @@ -69,7 +72,7 @@ public function setMimeHeaderBoundaryOnPart(IMimePart $part, string $mimeType) : * the message. The message's content and content headers are moved to the * new part. */ - public function setMessageAsMixed(IMessage $message) : self + public function setMessageAsMixed(IMessage $message) : static { if ($message->hasContent()) { $part = $this->genericHelper->createNewContentPartFrom($message); @@ -92,7 +95,7 @@ public function setMessageAsMixed(IMessage $message) : self * the message. The message's content and content headers are moved to the * new part. */ - public function setMessageAsAlternative(IMessage $message) : self + public function setMessageAsAlternative(IMessage $message) : static { if ($message->hasContent()) { $part = $this->genericHelper->createNewContentPartFrom($message); @@ -115,7 +118,7 @@ public function setMessageAsAlternative(IMessage $message) : self * under * @return bool|IMimePart false if a part is not found */ - public function getContentPartContainerFromAlternative($mimeType, IMimePart $alternativePart) + public function getContentPartContainerFromAlternative($mimeType, IMimePart $alternativePart) : bool|IMimePart { $part = $alternativePart->getPart(0, PartFilter::fromInlineContentType($mimeType)); $contPart = null; @@ -137,8 +140,12 @@ public function getContentPartContainerFromAlternative($mimeType, IMimePart $alt * removed, and parts of different content-types can optionally be moved to * the main message part. */ - public function removeAllContentPartsFromAlternative(IMessage $message, string $mimeType, IMimePart $alternativePart, bool $keepOtherContent) : bool - { + public function removeAllContentPartsFromAlternative( + IMessage $message, + string $mimeType, + IMimePart $alternativePart, + bool $keepOtherContent + ) : bool { $rmPart = $this->getContentPartContainerFromAlternative($mimeType, $alternativePart); if ($rmPart === false) { return false; @@ -167,7 +174,7 @@ public function removeAllContentPartsFromAlternative(IMessage $message, string $ * * @return IMimePart the alternative part */ - public function createAlternativeContentPart(IMessage $message, IMessagePart $contentPart) + public function createAlternativeContentPart(IMessage $message, IMessagePart $contentPart) : IMimePart { $altPart = $this->mimePartFactory->newInstance(); $this->setMimeHeaderBoundaryOnPart($altPart, 'multipart/alternative'); @@ -182,7 +189,7 @@ public function createAlternativeContentPart(IMessage $message, IMessagePart $co * content-type equal to $exceptMimeType. If the message is not a * multipart/mixed message, it is set to multipart/mixed first. */ - public function moveAllNonMultiPartsToMessageExcept(IMessage $message, IMimePart $from, string $exceptMimeType) : self + public function moveAllNonMultiPartsToMessageExcept(IMessage $message, IMimePart $from, string $exceptMimeType) : static { $parts = $from->getAllParts(function(IMessagePart $part) use ($exceptMimeType) { if ($part instanceof IMimePart && $part->isMultiPart()) { @@ -206,7 +213,7 @@ public function moveAllNonMultiPartsToMessageExcept(IMessage $message, IMimePart * up the message as a multipart/mixed message and creates a separate * content part. */ - public function enforceMime(IMessage $message) + public function enforceMime(IMessage $message) : static { if (!$message->isMime()) { if ($message->getAttachmentCount()) { @@ -216,15 +223,14 @@ public function enforceMime(IMessage $message) } $message->setRawHeader(HeaderConsts::MIME_VERSION, '1.0'); } + return $this; } /** * Creates a multipart/related part out of 'inline' children of $parent and * returns it. - * - * @return IMimePart */ - public function createMultipartRelatedPartForInlineChildrenOf(IMimePart $parent) + public function createMultipartRelatedPartForInlineChildrenOf(IMimePart $parent) : IMimePart { $relatedPart = $this->mimePartFactory->newInstance(); $this->setMimeHeaderBoundaryOnPart($relatedPart, 'multipart/related'); @@ -245,7 +251,7 @@ public function createMultipartRelatedPartForInlineChildrenOf(IMimePart $parent) * * @return IMimePart or null if not found */ - public function findOtherContentPartFor(IMessage $message, string $mimeType) + public function findOtherContentPartFor(IMessage $message, string $mimeType) : ?IMimePart { $altPart = $message->getPart( 0, @@ -263,10 +269,8 @@ public function findOtherContentPartFor(IMessage $message, string $mimeType) /** * Creates a new content part for the passed mimeType and charset, making * space by creating a multipart/alternative if needed - * - * @return \ZBateson\MailMimeParser\Message\IMimePart */ - public function createContentPartForMimeType(IMessage $message, string $mimeType, string $charset) + public function createContentPartForMimeType(IMessage $message, string $mimeType, string $charset) : IMimePart { $mimePart = $this->mimePartFactory->newInstance(); $mimePart->setRawHeader(HeaderConsts::CONTENT_TYPE, "$mimeType;\r\n\tcharset=\"$charset\""); @@ -294,8 +298,14 @@ public function createContentPartForMimeType(IMessage $message, string $mimeType * * @param string|resource|\Psr\Http\Message\StreamInterface $resource */ - public function createAndAddPartForAttachment(IMessage $message, $resource, string $mimeType, string $disposition, ?string $filename = null, string $encoding = 'base64') - { + public function createAndAddPartForAttachment( + IMessage $message, + $resource, + string $mimeType, + string $disposition, + ?string $filename = null, + string $encoding = 'base64' + ) : IMessagePart { if ($filename === null) { $filename = 'file' . \uniqid(); } @@ -315,6 +325,7 @@ public function createAndAddPartForAttachment(IMessage $message, $resource, stri } $part->setContent($resource); $message->addChild($part); + return $part; } /** @@ -367,7 +378,7 @@ public function removePartByMimeType(IMessage $message, string $mimeType, int $i * * @param string|resource $stringOrHandle */ - public function setContentPartForMimeType(IMessage $message, string $mimeType, $stringOrHandle, string $charset) : self + public function setContentPartForMimeType(IMessage $message, string $mimeType, mixed $stringOrHandle, string $charset) : static { $part = ($mimeType === 'text/html') ? $message->getHtmlPart() : $message->getTextPart(); if ($part === null) { diff --git a/src/Message/Helper/PrivacyHelper.php b/src/Message/Helper/PrivacyHelper.php index 8852168d..91a759ff 100644 --- a/src/Message/Helper/PrivacyHelper.php +++ b/src/Message/Helper/PrivacyHelper.php @@ -12,6 +12,7 @@ use ZBateson\MailMimeParser\Message\Factory\IMimePartFactory; use ZBateson\MailMimeParser\Message\Factory\IUUEncodedPartFactory; use ZBateson\MailMimeParser\Message\IMessagePart; +use Psr\Http\Message\StreamInterface; /** * Provides routines to set or retrieve the signature part of a signed message. @@ -23,12 +24,12 @@ class PrivacyHelper extends AbstractHelper /** * @var GenericHelper a GenericHelper instance */ - private $genericHelper; + private GenericHelper $genericHelper; /** * @var MultipartHelper a MultipartHelper instance */ - private $multipartHelper; + private MultipartHelper $multipartHelper; public function __construct( IMimePartFactory $mimePartFactory, @@ -49,7 +50,7 @@ public function __construct( * @param string $micalg * @param string $protocol */ - public function setMessageAsMultipartSigned(IMessage $message, $micalg, $protocol) + public function setMessageAsMultipartSigned(IMessage $message, $micalg, $protocol) : static { if (\strcasecmp($message->getContentType(), 'multipart/signed') !== 0) { $this->multipartHelper->enforceMime($message); @@ -64,6 +65,7 @@ public function setMessageAsMultipartSigned(IMessage $message, $micalg, $protoco } $this->overwrite8bitContentEncoding($message); $this->setSignature($message, 'Empty'); + return $this; } /** @@ -72,7 +74,7 @@ public function setMessageAsMultipartSigned(IMessage $message, $micalg, $protoco * * @param string $body */ - public function setSignature(IMessage $message, $body) + public function setSignature(IMessage $message, $body) : static { $signedPart = $message->getSignaturePart(); if ($signedPart === null) { @@ -84,6 +86,7 @@ public function setSignature(IMessage $message, $body) $message->getHeaderParameter(HeaderConsts::CONTENT_TYPE, 'protocol') ); $signedPart->setContent($body); + return $this; } /** @@ -93,9 +96,8 @@ public function setSignature(IMessage $message, $body) * * Used for multipart/signed messages which doesn't support 8bit transfer * encodings. - * */ - public function overwrite8bitContentEncoding(IMessage $message) + public function overwrite8bitContentEncoding(IMessage $message) : static { $parts = $message->getAllParts(function(IMessagePart $part) { return \strcasecmp($part->getContentTransferEncoding(), '8bit') === 0; @@ -109,6 +111,7 @@ public function overwrite8bitContentEncoding(IMessage $message) 'base64' ); } + return $this; } /** @@ -122,10 +125,9 @@ public function overwrite8bitContentEncoding(IMessage $message) * Note that unlike getSignedMessageAsString, getSignedMessageStream doesn't * replace new lines. * - * @return \Psr\Http\Message\StreamInterface or null if the message doesn't - * have any children + * @return ?StreamInterface null if the message doesn't have any children */ - public function getSignedMessageStream(IMessage $message) + public function getSignedMessageStream(IMessage $message) : ?StreamInterface { $child = $message->getChild(0); if ($child !== null) { @@ -140,9 +142,9 @@ public function getSignedMessageStream(IMessage $message) * * Non-CRLF new lines are replaced to always be CRLF. * - * @return string or null if the message doesn't have any children + * @return ?string null if the message doesn't have any children */ - public function getSignedMessageAsString(IMessage $message) + public function getSignedMessageAsString(IMessage $message) : ?string { $stream = $this->getSignedMessageStream($message); if ($stream !== null) { diff --git a/src/Message/IMessagePart.php b/src/Message/IMessagePart.php index 403e7a65..f972dab7 100644 --- a/src/Message/IMessagePart.php +++ b/src/Message/IMessagePart.php @@ -28,10 +28,8 @@ interface IMessagePart extends SplSubject, IErrorBag { /** * Returns this part's parent. - * - * @return IMimePart the parent part */ - public function getParent(); + public function getParent() : ?IMimePart; /** * Returns true if the part contains a 'body' (content). @@ -122,7 +120,7 @@ public function isMime() : bool; * @param bool $onlyIfNoCharset if true, $charsetOverride is used only if * getCharset returns null. */ - public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharset = false); + public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharset = false) : static; /** * Returns the StreamInterface for the part's content or null if the part @@ -167,7 +165,7 @@ public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharse * @param string $charset Optional charset for the returned stream. * @return StreamInterface|null the stream */ - public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARSET); + public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface; /** * Returns the raw data stream for the current part, if it exists, or null @@ -192,7 +190,7 @@ public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARS * @see IMessagePart::saveContent() to save the binary contents to file. * @return StreamInterface|null the stream */ - public function getBinaryContentStream(); + public function getBinaryContentStream() : ?StreamInterface; /** * Returns a resource handle for the content's raw data stream, or null if @@ -205,7 +203,7 @@ public function getBinaryContentStream(); * @see IMessagePart::saveContent() to save the binary contents to file. * @return resource|null the resource */ - public function getBinaryContentResourceHandle(); + public function getBinaryContentResourceHandle() : mixed; /** * Saves the binary content of the stream to the passed file, resource or @@ -231,7 +229,7 @@ public function getBinaryContentResourceHandle(); * a resource handle. * @param string|resource|StreamInterface $filenameResourceOrStream */ - public function saveContent($filenameResourceOrStream); + public function saveContent($filenameResourceOrStream) : static; /** * Shortcut to reading stream content and assigning it to a string. Returns @@ -241,7 +239,7 @@ public function saveContent($filenameResourceOrStream); * * @see IMessagePart::getContentStream() * @param string $charset the target charset for the returned string - * @return string|null the content + * @return ?string the content */ public function getContent(string $charset = MailMimeParser::DEFAULT_CHARSET) : ?string; @@ -256,7 +254,7 @@ public function getContent(string $charset = MailMimeParser::DEFAULT_CHARSET) : * @param StreamInterface $stream the content * @param string $streamCharset the charset of $stream */ - public function attachContentStream(StreamInterface $stream, string $streamCharset = MailMimeParser::DEFAULT_CHARSET); + public function attachContentStream(StreamInterface $stream, string $streamCharset = MailMimeParser::DEFAULT_CHARSET) : static; /** * Detaches the content stream. @@ -264,7 +262,7 @@ public function attachContentStream(StreamInterface $stream, string $streamChars * @see IMessagePart::getContentStream() to get the content stream. * @see IMessagePart::attachContentStream() to attach a content stream. */ - public function detachContentStream(); + public function detachContentStream() : static; /** * Sets the content of the part to the passed string, resource, or stream. @@ -275,7 +273,7 @@ public function detachContentStream(); * @param string|resource|StreamInterface $resource the content. * @param string $resourceCharset the charset of the passed $resource. */ - public function setContent($resource, string $resourceCharset = MailMimeParser::DEFAULT_CHARSET); + public function setContent($resource, string $resourceCharset = MailMimeParser::DEFAULT_CHARSET) : static; /** * Returns a resource handle for the string representation of this part, @@ -305,7 +303,7 @@ public function setContent($resource, string $resourceCharset = MailMimeParser:: * Psr7 stream. * @return resource the resource handle containing the part. */ - public function getResourceHandle(); + public function getResourceHandle() : mixed; /** * Returns a Psr7 StreamInterface for the string representation of this @@ -333,7 +331,7 @@ public function getResourceHandle(); * Psr7 stream. * @return StreamInterface the stream containing the part. */ - public function getStream(); + public function getStream() : StreamInterface; /** * Writes a string representation of this part, including its headers, @@ -369,7 +367,7 @@ public function getStream(); * @param string $filemode Optional filemode to open a file in (if * $filenameResourceOrStream is a string) */ - public function save($filenameResourceOrStream, string $filemode = 'w+'); + public function save(mixed $filenameResourceOrStream, string $filemode = 'w+') : static; /** * Returns the message/part as a string, containing its headers, content and diff --git a/src/Message/IMimePart.php b/src/Message/IMimePart.php index ce5fdc49..b4958849 100644 --- a/src/Message/IMimePart.php +++ b/src/Message/IMimePart.php @@ -8,6 +8,7 @@ namespace ZBateson\MailMimeParser\Message; use ZBateson\MailMimeParser\Header\IHeader; +use Traversable; /** * An interface representation of any MIME email part. @@ -20,17 +21,13 @@ interface IMimePart extends IMultiPart { /** * Returns true if this part's content type matches multipart/* - * - * @return bool */ - public function isMultiPart(); + public function isMultiPart() : bool; /** * Returns true if this part is the 'signature' part of a signed message. - * - * @return bool */ - public function isSignaturePart(); + public function isSignaturePart() : bool; /** * Returns the IHeader object for the header with the given $name. @@ -61,9 +58,9 @@ public function isSignaturePart(); * @param string $name The name of the header to retrieve. * @param int $offset Optional offset if there are multiple headers with the * given name. - * @return \ZBateson\MailMimeParser\Header\IHeader|null the header object + * @return ?IHeader the header object if it exists, or null if not */ - public function getHeader($name, $offset = 0); + public function getHeader($name, $offset = 0) : ?IHeader; /** * Returns the IHeader object for the header with the given $name, using the @@ -110,10 +107,9 @@ public function getHeaderAs(string $name, string $iHeaderClass, int $offset = 0) * array of raw headers in this part. * @see IMimePart::getRawHeaderIterator() to retrieve an iterator traversing * a two-dimensional string[] array of raw headers. - * @return \ZBateson\MailMimeParser\Header\IHeader[] an array of header - * objects + * @return IHeader[] an array of header objects */ - public function getAllHeaders(); + public function getAllHeaders() : array; /** * Returns an array of headers that match the passed name. @@ -130,10 +126,9 @@ public function getAllHeaders(); * @see IMimePart::getRawHeaderIterator() to retrieve an iterator traversing * a two-dimensional string[] array of raw headers. * @param string $name - * @return \ZBateson\MailMimeParser\Header\IHeader[] an array of header - * objects + * @return IHeader[] an array of header objects */ - public function getAllHeadersByName($name); + public function getAllHeadersByName($name) : array; /** * Returns a two dimensional string array of all headers for the mime part @@ -156,7 +151,7 @@ public function getAllHeadersByName($name); * the returned two-dimensional array * @return string[][] an array of raw headers */ - public function getRawHeaders(); + public function getRawHeaders() : array; /** * Returns an iterator to all headers in this part. Each returned element @@ -176,9 +171,9 @@ public function getRawHeaders(); * with a certain name. * @see IMimePart::getRawHeaders() to retrieve the array the returned * iterator iterates over. - * @return \Iterator an iterator for raw headers + * @return Traversable an iterator for raw headers */ - public function getRawHeaderIterator(); + public function getRawHeaderIterator() : Traversable; /** * Returns the string value for the header with the given $name, or null if @@ -202,7 +197,7 @@ public function getRawHeaderIterator(); * header doesn't exist on this part. * @return string|null the value of the header */ - public function getHeaderValue($name, $defaultValue = null); + public function getHeaderValue($name, $defaultValue = null) : ?string; /** * Returns the value of the parameter named $param on a header with the @@ -231,7 +226,7 @@ public function getHeaderValue($name, $defaultValue = null); * parameter doesn't exist. * @return string|null The value of the parameter. */ - public function getHeaderParameter($header, $param, $defaultValue = null); + public function getHeaderParameter($header, $param, $defaultValue = null) : ?string; /** * Adds a header with the given $name and $value. An optional $offset may @@ -270,7 +265,7 @@ public function getHeaderParameter($header, $param, $defaultValue = null); * @param int $offset An optional offset, defaulting to '0' and therefore * overriding the first header of the given $name if one exists. */ - public function setRawHeader(string $name, ?string $value, int $offset = 0); + public function setRawHeader(string $name, ?string $value, int $offset = 0) : static; /** * Adds a header with the given $name and $value. @@ -296,7 +291,7 @@ public function setRawHeader(string $name, ?string $value, int $offset = 0); * @param string $name The name of the header * @param string $value The raw value of the header. */ - public function addRawHeader(string $name, string $value); + public function addRawHeader(string $name, string $value) : static; /** * Removes all headers from this part with the passed name. @@ -309,7 +304,7 @@ public function addRawHeader(string $name, string $value); * one with the passed name exists. * @param string $name The name of the header(s) to remove. */ - public function removeHeader(string $name); + public function removeHeader(string $name) : static; /** * Removes a single header with the passed name (in cases where more than @@ -325,5 +320,5 @@ public function removeHeader(string $name); * @param int $offset Optional offset of the header to remove (defaults to * 0 -- the first header). */ - public function removeSingleHeader(string $name, int $offset = 0); + public function removeSingleHeader(string $name, int $offset = 0) : static; } diff --git a/src/Message/IMultiPart.php b/src/Message/IMultiPart.php index 51f3d87d..e647a09f 100644 --- a/src/Message/IMultiPart.php +++ b/src/Message/IMultiPart.php @@ -7,6 +7,8 @@ namespace ZBateson\MailMimeParser\Message; +use RecursiveIterator; + /** * An interface representing a message part that contains children. * @@ -42,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); + public function getPart($index, $fnFilter = null) : ?IMessagePart; /** * Returns the current part, all child parts, and child parts of all @@ -68,7 +70,7 @@ public function getPart($index, $fnFilter = null); * returning true if the part should be included. * @return IMessagePart[] An array of matching parts. */ - public function getAllParts($fnFilter = null); + public function getAllParts($fnFilter = null) : array; /** * Returns the total number of parts in this and all children. @@ -91,7 +93,7 @@ public function getAllParts($fnFilter = null); * returning true if the part should be included. * @return int The number of matching parts. */ - public function getPartCount($fnFilter = null); + public function getPartCount($fnFilter = null) : int; /** * Returns the direct child at the given 0-based index and optional filter, @@ -116,7 +118,7 @@ public function getPartCount($fnFilter = null); * @return IMessagePart|null The matching direct child part or null if not * found. */ - public function getChild($index, $fnFilter = null); + public function getChild($index, $fnFilter = null) : ?IMessagePart; /** * Returns an array of all direct child parts, optionally filtering them @@ -138,7 +140,7 @@ public function getChild($index, $fnFilter = null); * returning true if the part should be included. * @return IMessagePart[] An array of matching child parts. */ - public function getChildParts($fnFilter = null); + public function getChildParts($fnFilter = null) : array; /** * Returns the number of direct children under this part (optionally @@ -161,7 +163,7 @@ public function getChildParts($fnFilter = null); * @return int The number of children, or number of children matching the * the passed filtering callable. */ - public function getChildCount($fnFilter = null); + public function getChildCount($fnFilter = null) : int; /** * Returns a \RecursiveIterator of child parts. @@ -182,9 +184,8 @@ public function getChildCount($fnFilter = null); * this part. * @see IMultiPart::getAllParts() to get an array of all parts with an * optional filter. - * @return \RecursiveIterator */ - public function getChildIterator(); + public function getChildIterator() : RecursiveIterator; /** * Returns the part that has a content type matching the passed mime type at @@ -202,7 +203,7 @@ public function getChildIterator(); * @param int $index Optional 0-based index (defaulting to '0'). * @return IMessagePart|null The part. */ - public function getPartByMimeType($mimeType, $index = 0); + public function getPartByMimeType($mimeType, $index = 0) : ?IMessagePart; /** * Returns an array of all parts that have a content type matching the @@ -218,7 +219,7 @@ public function getPartByMimeType($mimeType, $index = 0); * @param string $mimeType The mime type to find. * @return IMessagePart[] An array of matching parts. */ - public function getAllPartsByMimeType($mimeType); + public function getAllPartsByMimeType($mimeType) : array; /** * Returns the number of parts that have content types matching the passed @@ -230,7 +231,7 @@ public function getAllPartsByMimeType($mimeType); * @param string $mimeType The mime type to find. * @return int The number of matching parts. */ - public function getCountOfPartsByMimeType($mimeType); + public function getCountOfPartsByMimeType($mimeType) : int; /** * Returns a part that has the given Content ID, or null if not found. @@ -240,7 +241,7 @@ public function getCountOfPartsByMimeType($mimeType); * @param string $contentId The content ID to find a part for. * @return IMessagePart|null The matching part. */ - public function getPartByContentId($contentId); + public function getPartByContentId($contentId) : ?IMessagePart; /** * Registers the passed part as a child of the current part. @@ -251,7 +252,7 @@ public function getPartByContentId($contentId); * @param IMessagePart $part The part to add. * @param int $position Optional insertion position 0-based index. */ - public function addChild(IMessagePart $part, ?int $position = null); + public function addChild(IMessagePart $part, ?int $position = null) : static; /** * Removes the child part from this part and returns its previous position diff --git a/src/Message/IUUEncodedPart.php b/src/Message/IUUEncodedPart.php index ceaabd0b..8dcf5b52 100644 --- a/src/Message/IUUEncodedPart.php +++ b/src/Message/IUUEncodedPart.php @@ -29,9 +29,8 @@ interface IUUEncodedPart extends IMessagePart { /** * Sets the filename included in the uuencoded 'begin' line. - * */ - public function setFilename(string $filename); + public function setFilename(string $filename) : static; /** * Returns the file mode included in the uuencoded 'begin' line for this @@ -42,5 +41,5 @@ public function getUnixFileMode() : ?int; /** * Sets the unix file mode for the uuencoded 'begin' line. */ - public function setUnixFileMode(int $mode); + public function setUnixFileMode(int $mode) : static; } diff --git a/src/Message/MessagePart.php b/src/Message/MessagePart.php index 752c8956..ed786bb2 100644 --- a/src/Message/MessagePart.php +++ b/src/Message/MessagePart.php @@ -27,32 +27,32 @@ abstract class MessagePart extends ErrorBag implements IMessagePart /** * @var ?IMimePart parent part */ - protected $parent; + protected ?IMimePart $parent; /** * @var PartStreamContainer holds 'stream' and 'content stream'. */ - protected $partStreamContainer; + protected PartStreamContainer $partStreamContainer; /** * @var ?string can be used to set an override for content's charset in cases * where a user knows the charset on the content is not what it claims * to be. */ - protected $charsetOverride; + protected ?string $charsetOverride = null; /** * @var bool set to true when a user attaches a stream manually, it's * assumed to already be decoded or to have relevant transfer encoding * decorators attached already. */ - protected $ignoreTransferEncoding; + protected bool $ignoreTransferEncoding = false; /** * @var SplObjectStorage attached observers that need to be notified of * modifications to this part. */ - protected $observers; + protected SplObjectStorage $observers; public function __construct(?PartStreamContainer $streamContainer = null, ?IMimePart $parent = null) { @@ -87,7 +87,7 @@ public function notify() : void } } - public function getParent() + public function getParent() : ?IMimePart { return $this->parent; } @@ -102,10 +102,7 @@ public function getFilename() : ?string return null; } - /** - * @return static - */ - public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharset = false) + public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharset = false) : static { if (!$onlyIfNoCharset || $this->getCharset() === null) { $this->charsetOverride = $charsetOverride; @@ -113,7 +110,7 @@ public function setCharsetOverride(string $charsetOverride, bool $onlyIfNoCharse return $this; } - public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARSET) + public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARSET) : ?StreamInterface { if ($this->hasContent()) { $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding(); @@ -127,7 +124,7 @@ public function getContentStream(string $charset = MailMimeParser::DEFAULT_CHARS return null; } - public function getBinaryContentStream() + public function getBinaryContentStream() : ?StreamInterface { if ($this->hasContent()) { $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding(); @@ -136,7 +133,7 @@ public function getBinaryContentStream() return null; } - public function getBinaryContentResourceHandle() + public function getBinaryContentResourceHandle() : mixed { $stream = $this->getBinaryContentStream(); if ($stream !== null) { @@ -145,7 +142,7 @@ public function getBinaryContentResourceHandle() return null; } - public function saveContent($filenameResourceOrStream) : self + public function saveContent($filenameResourceOrStream) : static { $resourceOrStream = $filenameResourceOrStream; if (\is_string($filenameResourceOrStream)) { @@ -173,10 +170,7 @@ public function getContent(string $charset = MailMimeParser::DEFAULT_CHARSET) : return null; } - /** - * @return static - */ - public function attachContentStream(StreamInterface $stream, string $streamCharset = MailMimeParser::DEFAULT_CHARSET) + public function attachContentStream(StreamInterface $stream, string $streamCharset = MailMimeParser::DEFAULT_CHARSET) : static { $ch = $this->charsetOverride ?? $this->getCharset(); if ($ch !== null && $streamCharset !== $ch) { @@ -188,20 +182,14 @@ public function attachContentStream(StreamInterface $stream, string $streamChars return $this; } - /** - * @return static - */ - public function detachContentStream() + public function detachContentStream() : static { $this->partStreamContainer->setContentStream(null); $this->notify(); return $this; } - /** - * @return static - */ - public function setContent($resource, string $charset = MailMimeParser::DEFAULT_CHARSET) + public function setContent($resource, string $charset = MailMimeParser::DEFAULT_CHARSET) : static { $stream = Utils::streamFor($resource); $this->attachContentStream($stream, $charset); @@ -209,20 +197,17 @@ public function setContent($resource, string $charset = MailMimeParser::DEFAULT_ return $this; } - public function getResourceHandle() + public function getResourceHandle() : mixed { return StreamWrapper::getResource($this->getStream()); } - public function getStream() + public function getStream() : StreamInterface { return $this->partStreamContainer->getStream(); } - /** - * @return static - */ - public function save($filenameResourceOrStream, string $filemode = 'w+') + public function save($filenameResourceOrStream, string $filemode = 'w+') : static { $resourceOrStream = $filenameResourceOrStream; if (\is_string($filenameResourceOrStream)) { diff --git a/src/Message/MimePart.php b/src/Message/MimePart.php index 2505f03e..fd5bc186 100644 --- a/src/Message/MimePart.php +++ b/src/Message/MimePart.php @@ -13,6 +13,7 @@ use ZBateson\MailMimeParser\Header\ParameterHeader; use ZBateson\MailMimeParser\IMessage; use ZBateson\MailMimeParser\MailMimeParser; +use Traversable; /** * A mime email message part. @@ -26,7 +27,7 @@ class MimePart extends MultiPart implements IMimePart /** * @var PartHeaderContainer Container for this part's headers. */ - protected $headerContainer; + protected PartHeaderContainer $headerContainer; public function __construct( ?IMimePart $parent = null, @@ -73,7 +74,7 @@ public function isMime() : bool return true; } - public function isMultiPart() + public function isMultiPart() : bool { // casting to bool, preg_match returns 1 for true return (bool) (\preg_match( @@ -207,10 +208,8 @@ public function getContentId() : ?string /** * Returns true if this part's parent is an IMessage, and is the same part * returned by {@see IMessage::getSignaturePart()}. - * - * @return bool */ - public function isSignaturePart() + public function isSignaturePart() : bool { if ($this->parent === null || !$this->parent instanceof IMessage) { return false; @@ -218,7 +217,7 @@ public function isSignaturePart() return $this->parent->getSignaturePart() === $this; } - public function getHeader($name, $offset = 0) + public function getHeader($name, $offset = 0) : ?IHeader { return $this->headerContainer->get($name, $offset); } @@ -228,27 +227,27 @@ public function getHeaderAs(string $name, string $iHeaderClass, int $offset = 0) return $this->headerContainer->getAs($name, $iHeaderClass, $offset); } - public function getAllHeaders() + public function getAllHeaders() : array { return $this->headerContainer->getHeaderObjects(); } - public function getAllHeadersByName($name) + public function getAllHeadersByName($name) : array { return $this->headerContainer->getAll($name); } - public function getRawHeaders() + public function getRawHeaders() : array { return $this->headerContainer->getHeaders(); } - public function getRawHeaderIterator() + public function getRawHeaderIterator() : Traversable { return $this->headerContainer->getIterator(); } - public function getHeaderValue($name, $defaultValue = null) + public function getHeaderValue($name, $defaultValue = null) : ?string { $header = $this->getHeader($name); if ($header !== null) { @@ -257,7 +256,7 @@ public function getHeaderValue($name, $defaultValue = null) return $defaultValue; } - public function getHeaderParameter($header, $param, $defaultValue = null) + public function getHeaderParameter($header, $param, $defaultValue = null) : ?string { $obj = $this->getHeader($header); if ($obj && $obj instanceof ParameterHeader) { @@ -266,40 +265,28 @@ public function getHeaderParameter($header, $param, $defaultValue = null) return $defaultValue; } - /** - * @return static - */ - public function setRawHeader(string $name, ?string $value, int $offset = 0) + public function setRawHeader(string $name, ?string $value, int $offset = 0) : static { $this->headerContainer->set($name, $value, $offset); $this->notify(); return $this; } - /** - * @return static - */ - public function addRawHeader(string $name, string $value) + public function addRawHeader(string $name, string $value) : static { $this->headerContainer->add($name, $value); $this->notify(); return $this; } - /** - * @return static - */ - public function removeHeader(string $name) + public function removeHeader(string $name) : static { $this->headerContainer->removeAll($name); $this->notify(); return $this; } - /** - * @return static - */ - public function removeSingleHeader(string $name, int $offset = 0) + public function removeSingleHeader(string $name, int $offset = 0) : static { $this->headerContainer->remove($name, $offset); $this->notify(); diff --git a/src/Message/MultiPart.php b/src/Message/MultiPart.php index 0be021be..3cc76ce2 100644 --- a/src/Message/MultiPart.php +++ b/src/Message/MultiPart.php @@ -10,6 +10,7 @@ use AppendIterator; use ArrayIterator; use Iterator; +use RecursiveIterator; use RecursiveIteratorIterator; /** @@ -22,7 +23,7 @@ abstract class MultiPart extends MessagePart implements IMultiPart /** * @var PartChildrenContainer child part container */ - protected $partChildrenContainer; + protected PartChildrenContainer $partChildrenContainer; public function __construct( ?IMimePart $parent = null, @@ -41,7 +42,7 @@ private function getAllPartsIterator() : AppendIterator return $iter; } - private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null) + private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null) : ?IMessagePart { $pos = 0; foreach ($iter as $part) { @@ -52,9 +53,10 @@ private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null) ++$pos; } } + return null; } - public function getPart($index, $fnFilter = null) + public function getPart($index, $fnFilter = null) : ?IMessagePart { return $this->iteratorFindAt( $this->getAllPartsIterator(), @@ -63,7 +65,7 @@ public function getPart($index, $fnFilter = null) ); } - public function getAllParts($fnFilter = null) + public function getAllParts($fnFilter = null) : array { $array = \iterator_to_array($this->getAllPartsIterator(), false); if ($fnFilter !== null) { @@ -72,12 +74,12 @@ public function getAllParts($fnFilter = null) return $array; } - public function getPartCount($fnFilter = null) + public function getPartCount($fnFilter = null) : int { return \count($this->getAllParts($fnFilter)); } - public function getChild($index, $fnFilter = null) + public function getChild($index, $fnFilter = null) : ?IMessagePart { return $this->iteratorFindAt( $this->partChildrenContainer, @@ -86,12 +88,12 @@ public function getChild($index, $fnFilter = null) ); } - public function getChildIterator() + public function getChildIterator() : RecursiveIterator { return $this->partChildrenContainer; } - public function getChildParts($fnFilter = null) + public function getChildParts($fnFilter = null) : array { $array = \iterator_to_array($this->partChildrenContainer, false); if ($fnFilter !== null) { @@ -100,27 +102,27 @@ public function getChildParts($fnFilter = null) return $array; } - public function getChildCount($fnFilter = null) + public function getChildCount($fnFilter = null) : int { return \count($this->getChildParts($fnFilter)); } - public function getPartByMimeType($mimeType, $index = 0) + public function getPartByMimeType($mimeType, $index = 0) : ?IMessagePart { return $this->getPart($index, PartFilter::fromContentType($mimeType)); } - public function getAllPartsByMimeType($mimeType) + public function getAllPartsByMimeType($mimeType) : array { return $this->getAllParts(PartFilter::fromContentType($mimeType)); } - public function getCountOfPartsByMimeType($mimeType) + public function getCountOfPartsByMimeType($mimeType) : int { return $this->getPartCount(PartFilter::fromContentType($mimeType)); } - public function getPartByContentId($contentId) + public function getPartByContentId($contentId) : ?IMessagePart { $sanitized = \preg_replace('/^\s*<|>\s*$/', '', $contentId); return $this->getPart(0, function(IMessagePart $part) use ($sanitized) { @@ -129,13 +131,14 @@ public function getPartByContentId($contentId) }); } - public function addChild(IMessagePart $part, ?int $position = null) + public function addChild(IMessagePart $part, ?int $position = null) : static { if ($part !== $this) { $part->parent = $this; $this->partChildrenContainer->add($part, $position); $this->notify(); } + return $this; } public function removePart(IMessagePart $part) : ?int diff --git a/src/Message/PartChildrenContainer.php b/src/Message/PartChildrenContainer.php index f629fc85..2fae9344 100644 --- a/src/Message/PartChildrenContainer.php +++ b/src/Message/PartChildrenContainer.php @@ -22,7 +22,7 @@ class PartChildrenContainer implements ArrayAccess, RecursiveIterator * @var IMessagePart[] array of child parts of the IMultiPart object that is * holding this container. */ - protected $children; + protected array $children; /** * @var int current key position within $children for iteration. @@ -35,14 +35,12 @@ public function __construct(array $children = []) } /** - * Returns true if the current element is an IMultiPart and doesn't return - * null for {@see IMultiPart::getChildIterator()}. Note that the iterator - * may still be empty. + * Returns true if the current element is an IMultiPart. Note that the + * iterator may still be empty. */ public function hasChildren() : bool { - return ($this->current() instanceof IMultiPart - && $this->current()->getChildIterator() !== null); + return ($this->current() instanceof IMultiPart); } /** @@ -59,8 +57,7 @@ public function getChildren() : ?RecursiveIterator return null; } - #[\ReturnTypeWillChange] - public function current() + public function current() : mixed { return $this->offsetGet($this->position); } @@ -95,7 +92,7 @@ public function valid() : bool * @param int $position An optional index position (0-based) to add the * child at. */ - public function add(IMessagePart $part, $position = null) + public function add(IMessagePart $part, $position = null) : static { $index = $position ?? \count($this->children); \array_splice( @@ -104,6 +101,7 @@ public function add(IMessagePart $part, $position = null) 0, [$part] ); + return $this; } /** @@ -128,8 +126,7 @@ public function offsetExists($offset) : bool return isset($this->children[$offset]); } - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet($offset) : mixed { return $this->offsetExists($offset) ? $this->children[$offset] : null; } diff --git a/src/Message/PartFilter.php b/src/Message/PartFilter.php index 162d5055..0adb16b3 100644 --- a/src/Message/PartFilter.php +++ b/src/Message/PartFilter.php @@ -23,10 +23,8 @@ abstract class PartFilter * disposition * - any part that returns true for isMultiPart() * - any part that returns true for isSignaturePart() - * - * @return callable */ - public static function fromAttachmentFilter() + public static function fromAttachmentFilter() : callable { return function(IMessagePart $part) { $type = $part->getContentType(); diff --git a/src/Message/PartHeaderContainer.php b/src/Message/PartHeaderContainer.php index 9ee7361d..6805fa09 100644 --- a/src/Message/PartHeaderContainer.php +++ b/src/Message/PartHeaderContainer.php @@ -7,6 +7,7 @@ namespace ZBateson\MailMimeParser\Message; +use Traversable; use ArrayIterator; use IteratorAggregate; use ZBateson\MailMimeParser\ErrorBag; @@ -280,7 +281,7 @@ public function add($name, $value) * @param string $value * @param int $offset */ - public function set($name, $value, $offset = 0) : self + public function set($name, $value, $offset = 0) : static { $s = $this->headerFactory->getNormalizedHeaderName($name); if (!isset($this->headerMap[$s][$offset])) { @@ -328,11 +329,8 @@ public function getHeaders() * the second to its value: * * [ 'Header-Name', 'Header Value' ] - * - * @return ArrayIterator */ - #[\ReturnTypeWillChange] - public function getIterator() + public function getIterator() : Traversable { return new ArrayIterator($this->getHeaders()); } diff --git a/src/Message/PartStreamContainer.php b/src/Message/PartStreamContainer.php index 9efe3efa..687767ee 100644 --- a/src/Message/PartStreamContainer.php +++ b/src/Message/PartStreamContainer.php @@ -32,40 +32,40 @@ class PartStreamContainer * @var StreamFactory used to apply psr7 stream decorators to the * attached StreamInterface based on encoding. */ - protected $streamFactory; + protected StreamFactory $streamFactory; /** * @var StreamInterface stream containing the part's headers, content and * children */ - protected $stream; + protected StreamInterface $stream; /** * @var StreamInterface a stream containing this part's content */ - protected $contentStream; + protected ?StreamInterface $contentStream = null; /** * @var StreamInterface the content stream after attaching transfer encoding * streams to $contentStream. */ - protected $decodedStream; + protected ?StreamInterface $decodedStream = null; /** * @var StreamInterface attached charset stream to $decodedStream */ - protected $charsetStream; + protected ?StreamInterface $charsetStream = null; /** * @var bool true if the stream should be detached when this container is * destroyed. */ - protected $detachParsedStream; + protected bool $detachParsedStream = false; /** * @var array map of the active encoding filter on the current handle. */ - private $encoding = [ + private array $encoding = [ 'type' => null, 'filter' => null ]; @@ -73,7 +73,7 @@ class PartStreamContainer /** * @var array map of the active charset filter on the current handle. */ - private $charset = [ + private array $charset = [ 'from' => null, 'to' => null, 'filter' => null @@ -89,18 +89,17 @@ public function __construct(StreamFactory $streamFactory) * children. * */ - public function setStream(StreamInterface $stream) + public function setStream(StreamInterface $stream) : static { $this->stream = $stream; + return $this; } /** * Returns the part's stream containing the part's headers, content, and * children. - * - * @return StreamInterface */ - public function getStream() + public function getStream() : StreamInterface { // error out if called before setStream, getStream should never return // null. @@ -131,11 +130,12 @@ public function hasContent() : bool * setContentStream can be called with 'null' to indicate the IMessagePart * does not contain any content. */ - public function setContentStream(?StreamInterface $contentStream = null) + public function setContentStream(?StreamInterface $contentStream = null) : static { $this->contentStream = $contentStream; $this->decodedStream = null; $this->charsetStream = null; + return $this; } /** @@ -163,7 +163,7 @@ private function isCharsetFilterChanged(string $fromCharset, string $toCharset) * Attaches a decoding filter to the attached content handle, for the passed * $transferEncoding. */ - protected function attachTransferEncodingFilter(?string $transferEncoding) : self + protected function attachTransferEncodingFilter(?string $transferEncoding) : static { if ($this->decodedStream !== null) { $this->encoding['type'] = $transferEncoding; @@ -193,7 +193,7 @@ protected function attachTransferEncodingFilter(?string $transferEncoding) : sel * @param string $fromCharset the character set the content is encoded in * @param string $toCharset the target encoding to return */ - protected function attachCharsetFilter(string $fromCharset, string $toCharset) : self + protected function attachCharsetFilter(string $fromCharset, string $toCharset) : static { if ($this->charsetStream !== null) { $this->charsetStream = new CachingStream($this->streamFactory->newCharsetStream( @@ -210,7 +210,7 @@ protected function attachCharsetFilter(string $fromCharset, string $toCharset) : /** * Resets just the charset stream, and rewinds the decodedStream. */ - private function resetCharsetStream() : self + private function resetCharsetStream() : static { $this->charset = [ 'from' => null, @@ -225,7 +225,7 @@ private function resetCharsetStream() : self /** * Resets cached encoding and charset streams, and rewinds the stream. */ - public function reset() + public function reset() : static { $this->encoding = [ 'type' => null, @@ -239,6 +239,7 @@ public function reset() $this->contentStream->rewind(); $this->decodedStream = $this->contentStream; $this->charsetStream = $this->contentStream; + return $this; } /** @@ -249,9 +250,8 @@ public function reset() * @param string $transferEncoding the transfer encoding * @param string $fromCharset the character set the content is encoded in * @param string $toCharset the target encoding to return - * @return ?StreamInterface */ - public function getContentStream(?string $transferEncoding, ?string $fromCharset, ?string $toCharset) + public function getContentStream(?string $transferEncoding, ?string $fromCharset, ?string $toCharset) : ?StreamInterface { if ($this->contentStream === null) { return null; diff --git a/src/Message/UUEncodedPart.php b/src/Message/UUEncodedPart.php index f3709853..5965b614 100644 --- a/src/Message/UUEncodedPart.php +++ b/src/Message/UUEncodedPart.php @@ -17,12 +17,12 @@ class UUEncodedPart extends NonMimePart implements IUUEncodedPart /** * @var int the unix file permission */ - protected $mode = null; + protected ?int $mode = null; /** * @var string the name of the file in the uuencoding 'header'. */ - protected $filename = null; + protected ?string $filename = null; public function __construct( ?int $mode = null, @@ -47,10 +47,7 @@ public function getFilename() : ?string return $this->filename; } - /** - * @return static - */ - public function setFilename(string $filename) + public function setFilename(string $filename) : static { $this->filename = $filename; $this->notify(); @@ -108,10 +105,7 @@ public function getUnixFileMode() : ?int return $this->mode; } - /** - * @return static - */ - public function setUnixFileMode(int $mode) + public function setUnixFileMode(int $mode) : static { $this->mode = $mode; $this->notify(); diff --git a/src/Parser/AbstractParserService.php b/src/Parser/AbstractParserService.php index 67918b27..9eb4547e 100644 --- a/src/Parser/AbstractParserService.php +++ b/src/Parser/AbstractParserService.php @@ -26,26 +26,26 @@ abstract class AbstractParserService implements IParserService * responsible for creating an IMessage part wrapped in a * ParserPartProxy. */ - protected $parserMessageProxyFactory; + protected ParserPartProxyFactory $parserMessageProxyFactory; /** * @var ParserPartProxyFactory the parser's part proxy factory service * responsible for creating IMessagePart parts wrapped in a * ParserPartProxy. */ - protected $parserPartProxyFactory; + protected ParserPartProxyFactory $parserPartProxyFactory; /** * @var PartBuilderFactory Service for creating PartBuilder objects for new * children. */ - protected $partBuilderFactory; + protected PartBuilderFactory $partBuilderFactory; /** * @var ParserManagerService the ParserManager, which should call setParserManager * when the parser is added. */ - protected $parserManager; + protected ParserManagerService $parserManager; public function __construct( ParserPartProxyFactory $parserMessageProxyFactory, @@ -57,21 +57,18 @@ public function __construct( $this->partBuilderFactory = $partBuilderFactory; } - /** - * @return static - */ - public function setParserManager(ParserManagerService $pm) + public function setParserManager(ParserManagerService $pm) : static { $this->parserManager = $pm; return $this; } - public function getParserMessageProxyFactory() + public function getParserMessageProxyFactory() : ParserPartProxyFactory { return $this->parserMessageProxyFactory; } - public function getParserPartProxyFactory() + public function getParserPartProxyFactory() : ParserPartProxyFactory { return $this->parserPartProxyFactory; } diff --git a/src/Parser/HeaderParserService.php b/src/Parser/HeaderParserService.php index 47940683..666dd02e 100644 --- a/src/Parser/HeaderParserService.php +++ b/src/Parser/HeaderParserService.php @@ -25,7 +25,7 @@ class HeaderParserService * @param string $header the header line * @param PartHeaderContainer $headerContainer the container */ - private function addRawHeaderToPart(int $offset, string $header, PartHeaderContainer $headerContainer) : self + private function addRawHeaderToPart(int $offset, string $header, PartHeaderContainer $headerContainer) : static { if ($header !== '') { if (\strpos($header, ':') !== false) { @@ -48,7 +48,7 @@ private function addRawHeaderToPart(int $offset, string $header, PartHeaderConta * @param resource $handle The resource handle to read from. * @param PartHeaderContainer $container the container to add headers to. */ - public function parse($handle, PartHeaderContainer $container) : self + public function parse($handle, PartHeaderContainer $container) : static { $header = ''; do { diff --git a/src/Parser/IParserService.php b/src/Parser/IParserService.php index 5b2b2ddf..2ce080bf 100644 --- a/src/Parser/IParserService.php +++ b/src/Parser/IParserService.php @@ -25,7 +25,7 @@ interface IParserService * * @param ParserManagerService $pm The ParserManager to set. */ - public function setParserManager(ParserManagerService $pm); + public function setParserManager(ParserManagerService $pm) : static; /** * Called by the ParserManager to determine if the passed PartBuilder is a @@ -39,10 +39,8 @@ public function canParse(PartBuilder $part) : bool; * * This is called by ParserManager after 'canParse' if it returns true so * a ParserPartProxy can be created out of the PartBuilder. - * - * @return ParserPartProxyFactory */ - public function getParserMessageProxyFactory(); + public function getParserMessageProxyFactory() : ParserPartProxyFactory; /** * Returns the ParserPartProxyFactory responsible for creating IMessagePart @@ -50,10 +48,8 @@ public function getParserMessageProxyFactory(); * * This is called by ParserManager after 'canParse' if it returns true so * a ParserPartProxy can be created out of the PartBuilder. - * - * @return ParserPartProxyFactory */ - public function getParserPartProxyFactory(); + public function getParserPartProxyFactory() : ParserPartProxyFactory; /** * Performs read operations for content from the stream of the passed @@ -69,7 +65,7 @@ public function getParserPartProxyFactory(); * message has been reached $proxy->setEof() should be called in addition to * setStreamContentAndPartEndPos(). */ - public function parseContent(ParserPartProxy $proxy); + public function parseContent(ParserPartProxy $proxy) : static; /** * Performs read operations to read children from the passed $proxy, using @@ -91,5 +87,5 @@ public function parseContent(ParserPartProxy $proxy); * @return ParserPartProxy|null The child ParserPartProxy or null if there * are no more children under $proxy. */ - public function parseNextChild(ParserMimePartProxy $proxy); + public function parseNextChild(ParserMimePartProxy $proxy) : ?ParserPartProxy; } diff --git a/src/Parser/MessageParserService.php b/src/Parser/MessageParserService.php index 8dfc0959..76cee1f6 100644 --- a/src/Parser/MessageParserService.php +++ b/src/Parser/MessageParserService.php @@ -9,6 +9,7 @@ use Psr\Http\Message\StreamInterface; use ZBateson\MailMimeParser\Message\Factory\PartHeaderContainerFactory; +use ZBateson\MailMimeParser\IMessage; /** * Parses a mail mime message into its component parts. To invoke, call @@ -22,24 +23,24 @@ class MessageParserService * @var PartHeaderContainerFactory To create a container to read the * message's headers into. */ - protected $partHeaderContainerFactory; + protected PartHeaderContainerFactory $partHeaderContainerFactory; /** * @var ParserManagerService To figure out what parser is responsible for parsing a * message. */ - protected $parserManager; + protected ParserManagerService $parserManager; /** * @var PartBuilderFactory To create a PartBuilder representing this * message, and to pass it to ParserManager. */ - protected $partBuilderFactory; + protected PartBuilderFactory $partBuilderFactory; /** * @var HeaderParserService To parse the headers into a PartHeaderContainer. */ - protected $headerParser; + protected HeaderParserService $headerParser; public function __construct( PartBuilderFactory $pbf, @@ -62,9 +63,9 @@ public function __construct( * returned. * * @param resource $handle - * @return string|bool the read line or false on EOF or on error. + * @return string|false the read line or false on EOF or on error. */ - public static function readLine($handle) + public static function readLine($handle) : string|false { $size = 4096; $ret = $line = \fgets($handle, $size); @@ -79,9 +80,8 @@ public static function readLine($handle) * object and returns it. * * @param StreamInterface $stream the stream to parse the message from - * @return \ZBateson\MailMimeParser\IMessage */ - public function parse(StreamInterface $stream) + public function parse(StreamInterface $stream) : IMessage { $headerContainer = $this->partHeaderContainerFactory->newInstance(); $partBuilder = $this->partBuilderFactory->newPartBuilder($headerContainer, $stream); diff --git a/src/Parser/MimeParserService.php b/src/Parser/MimeParserService.php index 92aa6a80..e3f0a48b 100644 --- a/src/Parser/MimeParserService.php +++ b/src/Parser/MimeParserService.php @@ -25,12 +25,12 @@ class MimeParserService extends AbstractParserService * @var PartHeaderContainerFactory Factory service for creating * PartHeaderContainers for headers. */ - protected $partHeaderContainerFactory; + protected PartHeaderContainerFactory $partHeaderContainerFactory; /** * @var HeaderParserService The HeaderParser service. */ - protected $headerParser; + protected HeaderParserService $headerParser; public function __construct( ParserMessageProxyFactory $parserMessageProxyFactory, @@ -92,7 +92,7 @@ private function readBoundaryLine($handle, ParserMimePartProxy $proxy) : string * the passed $handle's read pos before the boundary and its line separator * were read. */ - private function findContentBoundary(ParserMimePartProxy $proxy) : self + private function findContentBoundary(ParserMimePartProxy $proxy) : static { $handle = $proxy->getMessageResourceHandle(); // last separator before a boundary belongs to the boundary, and is not @@ -111,10 +111,7 @@ private function findContentBoundary(ParserMimePartProxy $proxy) : self return $this; } - /** - * @return static - */ - public function parseContent(ParserPartProxy $proxy) + public function parseContent(ParserPartProxy $proxy) : static { $proxy->setStreamContentStartPos($proxy->getMessageResourceHandlePos()); $this->findContentBoundary($proxy); @@ -141,10 +138,8 @@ public function parseContent(ParserPartProxy $proxy) * In this case, $this->parserPartProxyFactory is called directly to create * a part, $this->parseContent is called immediately to parse it and discard * it, and null is returned. - * - * @return ParserPartProxy|null */ - private function createPart(ParserMimePartProxy $parent, PartHeaderContainer $headerContainer, PartBuilder $child) + private function createPart(ParserMimePartProxy $parent, PartHeaderContainer $headerContainer, PartBuilder $child) : ?ParserPartProxy { if (!$parent->isEndBoundaryFound()) { $this->headerParser->parse( @@ -160,7 +155,7 @@ private function createPart(ParserMimePartProxy $parent, PartHeaderContainer $he return null; } - public function parseNextChild(ParserMimePartProxy $proxy) + public function parseNextChild(ParserMimePartProxy $proxy) : ?ParserPartProxy { if ($proxy->isParentBoundaryFound()) { return null; diff --git a/src/Parser/NonMimeParserService.php b/src/Parser/NonMimeParserService.php index d458bb05..670f2feb 100644 --- a/src/Parser/NonMimeParserService.php +++ b/src/Parser/NonMimeParserService.php @@ -25,7 +25,7 @@ class NonMimeParserService extends AbstractParserService /** * @var UUEncodedPartHeaderContainerFactory */ - protected $partHeaderContainerFactory; + protected UUEncodedPartHeaderContainerFactory $partHeaderContainerFactory; public function __construct( ParserNonMimeMessageProxyFactory $parserNonMimeMessageProxyFactory, @@ -40,7 +40,6 @@ public function __construct( /** * Always returns true, and should therefore be the last parser reached by * a ParserManager. - * */ public function canParse(PartBuilder $part) : bool { @@ -54,10 +53,8 @@ public function canParse(PartBuilder $part) : bool * It also sets the PartBuilder's stream part start pos and content start * pos to that of $parent->getNextParStart() (since a 'begin' line is read * prior to another child being created, see parseNextPart()). - * - * @return ParserPartProxy */ - private function createPart(ParserNonMimeMessageProxy $parent) + private function createPart(ParserNonMimeMessageProxy $parent) : ParserPartProxy { $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), $parent->getNextPartFilename()); $pb = $this->partBuilderFactory->newChildPartBuilder($hc, $parent); @@ -74,7 +71,7 @@ private function createPart(ParserNonMimeMessageProxy $parent) * * @param ParserNonMimeMessageProxy|ParserUUEncodedPartProxy $proxy */ - private function parseNextPart(ParserPartProxy $proxy) : self + private function parseNextPart(ParserPartProxy $proxy) : static { $handle = $proxy->getMessageResourceHandle(); while (!\feof($handle)) { @@ -91,10 +88,7 @@ private function parseNextPart(ParserPartProxy $proxy) : self return $this; } - /** - * @return static - */ - public function parseContent(ParserPartProxy $proxy) + public function parseContent(ParserPartProxy $proxy) : static { $handle = $proxy->getMessageResourceHandle(); if ($proxy->getNextPartStart() !== null || \feof($handle)) { @@ -107,7 +101,7 @@ public function parseContent(ParserPartProxy $proxy) return $this; } - public function parseNextChild(ParserMimePartProxy $proxy) + public function parseNextChild(ParserMimePartProxy $proxy) : ?ParserPartProxy { $handle = $proxy->getMessageResourceHandle(); if ($proxy->getNextPartStart() === null || \feof($handle)) { diff --git a/src/Parser/ParserManagerService.php b/src/Parser/ParserManagerService.php index dbc48051..c837af89 100644 --- a/src/Parser/ParserManagerService.php +++ b/src/Parser/ParserManagerService.php @@ -24,7 +24,7 @@ class ParserManagerService * @var IParserService[] List of parsers in order of priority (0 is highest * priority). */ - protected $parsers = []; + protected array $parsers = []; public function __construct(MimeParserService $mimeParser, NonMimeParserService $nonMimeParser) { @@ -36,7 +36,7 @@ public function __construct(MimeParserService $mimeParser, NonMimeParserService * calling $parser->setParserManager($this) on each one. * */ - public function setParsers(array $parsers) : self + public function setParsers(array $parsers) : static { foreach ($parsers as $parser) { $parser->setParserManager($this); @@ -51,7 +51,7 @@ public function setParsers(array $parsers) : self * * @param IParserService $parser The parser to add. */ - public function prependParser(IParserService $parser) : self + public function prependParser(IParserService $parser) : static { $parser->setParserManager($this); \array_unshift($this->parsers, $parser); @@ -73,10 +73,10 @@ public function prependParser(IParserService $parser) : self * an IParser * @throws CompatibleParserNotFoundException if a compatible parser for the * type is not configured. - * @return ?ParserPartProxy The created ParserPartProxy tied to a new + * @return ParserPartProxy The created ParserPartProxy tied to a new * IMessagePart and associated IParser. */ - public function createParserProxyFor(PartBuilder $partBuilder) + public function createParserProxyFor(PartBuilder $partBuilder) : ParserPartProxy { foreach ($this->parsers as $parser) { if ($parser->canParse($partBuilder)) { diff --git a/src/Parser/Part/ParserPartChildrenContainer.php b/src/Parser/Part/ParserPartChildrenContainer.php index bdd11412..743670e9 100644 --- a/src/Parser/Part/ParserPartChildrenContainer.php +++ b/src/Parser/Part/ParserPartChildrenContainer.php @@ -22,13 +22,13 @@ class ParserPartChildrenContainer extends PartChildrenContainer * @var ParserMimePartProxy The parser to proxy requests to when trying to * get child parts. */ - protected $parserProxy; + protected ParserMimePartProxy $parserProxy; /** * @var bool Set to true once all parts have been parsed, and requests to * the proxy won't result in any more child parts. */ - private $allParsed = false; + private bool $allParsed = false; public function __construct(ParserMimePartProxy $parserProxy) { diff --git a/src/Parser/Part/ParserPartChildrenContainerFactory.php b/src/Parser/Part/ParserPartChildrenContainerFactory.php index ede8fab7..858883c4 100644 --- a/src/Parser/Part/ParserPartChildrenContainerFactory.php +++ b/src/Parser/Part/ParserPartChildrenContainerFactory.php @@ -16,7 +16,7 @@ */ class ParserPartChildrenContainerFactory { - public function newInstance(ParserMimePartProxy $parserProxy) + public function newInstance(ParserMimePartProxy $parserProxy) : ParserPartChildrenContainer { return new ParserPartChildrenContainer($parserProxy); } diff --git a/src/Parser/Part/ParserPartStreamContainer.php b/src/Parser/Part/ParserPartStreamContainer.php index 9c680a6c..dae0d622 100644 --- a/src/Parser/Part/ParserPartStreamContainer.php +++ b/src/Parser/Part/ParserPartStreamContainer.php @@ -34,30 +34,24 @@ class ParserPartStreamContainer extends PartStreamContainer implements SplObserv /** * @var ParserPartProxy The parser proxy to ferry requests to on-demand. */ - protected $parserProxy; + protected ParserPartProxy $parserProxy; /** * @var StreamInterface the original stream for a parsed message, used when * the message hasn't changed */ - protected $parsedStream; - - /** - * @var bool true if the stream should be detached when this container is - * destroyed (thereby not closing the stream). - */ - protected $detachParsedStream = false; + protected ?StreamInterface $parsedStream = null; /** * @var bool set to true if the part's been updated since it was created. */ - protected $partUpdated = false; + protected bool $partUpdated = false; /** * @var bool false if the content for the part represented by this container * has not yet been requested from the parser. */ - protected $contentParseRequested = false; + protected bool $contentParseRequested = false; public function __construct(StreamFactory $streamFactory, ParserPartProxy $parserProxy) { @@ -76,7 +70,7 @@ public function __destruct() * Requests content from the parser if not previously requested, and calls * PartStreamContainer::setContentStream(). */ - protected function requestParsedContentStream() : self + protected function requestParsedContentStream() : static { if (!$this->contentParseRequested) { $this->contentParseRequested = true; @@ -93,7 +87,7 @@ protected function requestParsedContentStream() : self * $this->parsedStream to the original parsed stream (or a limited part of * it corresponding to the current part this stream container belongs to). */ - protected function requestParsedStream() : self + protected function requestParsedStream() : static { if ($this->parsedStream === null) { $this->parserProxy->parseAll(); @@ -113,7 +107,7 @@ public function hasContent() : bool return parent::hasContent(); } - public function getContentStream(?string $transferEncoding, ?string $fromCharset, ?string $toCharset) + public function getContentStream(?string $transferEncoding, ?string $fromCharset, ?string $toCharset) : ?StreamInterface { $this->requestParsedContentStream(); return parent::getContentStream($transferEncoding, $fromCharset, $toCharset); @@ -125,7 +119,7 @@ public function getBinaryContentStream(?string $transferEncoding = null) : ?Stre return parent::getBinaryContentStream($transferEncoding); } - public function setContentStream(?StreamInterface $contentStream = null) : self + public function setContentStream(?StreamInterface $contentStream = null) : static { // has to be overridden because requestParsedContentStream calls // parent::setContentStream as well, so needs to be parsed before @@ -135,7 +129,7 @@ public function setContentStream(?StreamInterface $contentStream = null) : self return $this; } - public function getStream() + public function getStream() : StreamInterface { $this->requestParsedStream(); if (!$this->partUpdated) { diff --git a/src/Parser/Part/ParserPartStreamContainerFactory.php b/src/Parser/Part/ParserPartStreamContainerFactory.php index 7466c6cd..cb122467 100644 --- a/src/Parser/Part/ParserPartStreamContainerFactory.php +++ b/src/Parser/Part/ParserPartStreamContainerFactory.php @@ -27,7 +27,7 @@ public function __construct(StreamFactory $streamFactory) $this->streamFactory = $streamFactory; } - public function newInstance(ParserPartProxy $parserProxy) + public function newInstance(ParserPartProxy $parserProxy) : ParserPartStreamContainer { return new ParserPartStreamContainer($this->streamFactory, $parserProxy); } diff --git a/src/Parser/Part/UUEncodedPartHeaderContainer.php b/src/Parser/Part/UUEncodedPartHeaderContainer.php index 1ccaa4f0..469534d0 100644 --- a/src/Parser/Part/UUEncodedPartHeaderContainer.php +++ b/src/Parser/Part/UUEncodedPartHeaderContainer.php @@ -19,14 +19,14 @@ class UUEncodedPartHeaderContainer extends PartHeaderContainer { /** - * @var int the unix file permission + * @var ?int the unix file permission */ - protected $mode = null; + protected ?int $mode = null; /** - * @var string the name of the file in the uuencoding 'header'. + * @var ?string the name of the file in the uuencoding 'header'. */ - protected $filename = null; + protected ?string $filename = null; /** * Returns the file mode included in the uuencoded 'begin' line for this @@ -39,10 +39,8 @@ public function getUnixFileMode() : ?int /** * Sets the unix file mode for the uuencoded 'begin' line. - * - * @return static */ - public function setUnixFileMode(int $mode) + public function setUnixFileMode(int $mode) : static { $this->mode = $mode; return $this; @@ -59,10 +57,8 @@ public function getFilename() : ?string /** * Sets the filename included in the uuencoded 'begin' line. - * - * @return static */ - public function setFilename(string $filename) + public function setFilename(string $filename) : static { $this->filename = $filename; return $this; diff --git a/src/Parser/Part/UUEncodedPartHeaderContainerFactory.php b/src/Parser/Part/UUEncodedPartHeaderContainerFactory.php index 1518155e..70310d2c 100644 --- a/src/Parser/Part/UUEncodedPartHeaderContainerFactory.php +++ b/src/Parser/Part/UUEncodedPartHeaderContainerFactory.php @@ -20,7 +20,7 @@ class UUEncodedPartHeaderContainerFactory * @var HeaderFactory the HeaderFactory passed to * UUEncodedPartHeaderContainer instances. */ - protected $headerFactory; + protected HeaderFactory $headerFactory; /** * Constructor @@ -33,10 +33,8 @@ public function __construct(HeaderFactory $headerFactory) /** * Creates and returns a UUEncodedPartHeaderContainer. - * - * @return UUEncodedPartHeaderContainer */ - public function newInstance(int $mode, string $filename) + public function newInstance(int $mode, string $filename) : UUEncodedPartHeaderContainer { $container = new UUEncodedPartHeaderContainer($this->headerFactory); $container->setUnixFileMode($mode); diff --git a/src/Parser/PartBuilder.php b/src/Parser/PartBuilder.php index 77f0b0f7..090110b6 100644 --- a/src/Parser/PartBuilder.php +++ b/src/Parser/PartBuilder.php @@ -34,48 +34,49 @@ class PartBuilder * @var int The offset read start position for this part (beginning of * headers) in the message's stream. */ - private $streamPartStartPos = null; + private int $streamPartStartPos; /** * @var int The offset read end position for this part. If the part is a * multipart mime part, the end position is after all of this parts * children. */ - private $streamPartEndPos = null; + private int $streamPartEndPos; /** - * @var int The offset read start position in the message's stream for the + * @var ?int The offset read start position in the message's stream for the * beginning of this part's content (body). */ - private $streamContentStartPos = null; + private ?int $streamContentStartPos = null; /** - * @var int The offset read end position in the message's stream for the + * @var ?int The offset read end position in the message's stream for the * end of this part's content (body). */ - private $streamContentEndPos = null; + private ?int $streamContentEndPos = null; /** * @var PartHeaderContainer The parsed part's headers. */ - private $headerContainer; + private PartHeaderContainer $headerContainer; /** * @var StreamInterface the raw message input stream for a message, or null * for a child part. */ - private $messageStream = null; + private ?StreamInterface $messageStream = null; /** * @var resource the raw message input stream handle constructed from * $messageStream or null for a child part */ - private $messageHandle = null; + private mixed $messageHandle = null; /** - * @var ParserPartProxy The parent ParserPartProxy. + * @var ParserPartProxy The parent ParserPartProxy if one is set, or null if + * part doesn't have a parent. */ - private $parent = null; + private ?ParserPartProxy $parent = null; public function __construct(PartHeaderContainer $headerContainer, ?StreamInterface $messageStream = null, ?ParserPartProxy $parent = null) { @@ -97,20 +98,16 @@ public function __destruct() /** * The ParserPartProxy parent of this PartBuilder. - * - * @return ParserPartProxy */ - public function getParent() + public function getParent() : ?ParserPartProxy { return $this->parent; } /** * Returns this part's PartHeaderContainer. - * - * @return PartHeaderContainer the container */ - public function getHeaderContainer() + public function getHeaderContainer() : PartHeaderContainer { return $this->headerContainer; } @@ -118,10 +115,8 @@ public function getHeaderContainer() /** * Returns the raw message StreamInterface for a message, getting it from * the parent part if this is a child part. - * - * @return StreamInterface */ - public function getStream() + public function getStream() : StreamInterface { return ($this->messageStream === null && $this->parent !== null) ? $this->parent->getStream() : @@ -134,7 +129,7 @@ public function getStream() * * @return resource */ - public function getMessageResourceHandle() + public function getMessageResourceHandle() : mixed { return ($this->messageHandle === null && $this->parent !== null) ? $this->parent->getMessageResourceHandle() : @@ -143,7 +138,6 @@ public function getMessageResourceHandle() /** * Shortcut for calling ftell($partBuilder->getMessageResourceHandle()). - * */ public function getMessageResourceHandlePos() : int { @@ -152,9 +146,9 @@ public function getMessageResourceHandlePos() : int /** * Returns the byte offset start position for this part within the message - * stream if it's been set, or null otherwise. + * stream. */ - public function getStreamPartStartPos() : ?int + public function getStreamPartStartPos() : int { return $this->streamPartStartPos; } @@ -198,10 +192,8 @@ public function getStreamContentLength() : int /** * Sets the byte offset start position of the part in the raw message * stream. - * - * @return static */ - public function setStreamPartStartPos(int $streamPartStartPos) + public function setStreamPartStartPos(int $streamPartStartPos) : static { $this->streamPartStartPos = $streamPartStartPos; return $this; @@ -211,10 +203,8 @@ public function setStreamPartStartPos(int $streamPartStartPos) * Sets the byte offset end position of the part in the raw message stream, * and also calls its parent's setParentStreamPartEndPos to expand to parent * PartBuilders. - * - * @return static */ - public function setStreamPartEndPos(int $streamPartEndPos) + public function setStreamPartEndPos(int $streamPartEndPos) : static { $this->streamPartEndPos = $streamPartEndPos; if ($this->parent !== null) { @@ -226,10 +216,8 @@ public function setStreamPartEndPos(int $streamPartEndPos) /** * Sets the byte offset start position of the content in the raw message * stream. - * - * @return static */ - public function setStreamContentStartPos(int $streamContentStartPos) + public function setStreamContentStartPos(int $streamContentStartPos) : static { $this->streamContentStartPos = $streamContentStartPos; return $this; @@ -238,10 +226,8 @@ public function setStreamContentStartPos(int $streamContentStartPos) /** * Sets the byte offset end position of the content and part in the raw * message stream. - * - * @return static */ - public function setStreamPartAndContentEndPos(int $streamContentEndPos) + public function setStreamPartAndContentEndPos(int $streamContentEndPos) : static { $this->streamContentEndPos = $streamContentEndPos; $this->setStreamPartEndPos($streamContentEndPos); @@ -252,9 +238,9 @@ public function setStreamPartAndContentEndPos(int $streamContentEndPos) * Returns true if the byte offset positions for this part's content have * been set. * - * @return ?bool true if set. + * @return bool true if set. */ - public function isContentParsed() : ?bool + public function isContentParsed() : bool { return ($this->streamContentEndPos !== null); } diff --git a/src/Parser/PartBuilderFactory.php b/src/Parser/PartBuilderFactory.php index 7d48bd50..98c23c72 100644 --- a/src/Parser/PartBuilderFactory.php +++ b/src/Parser/PartBuilderFactory.php @@ -19,22 +19,18 @@ class PartBuilderFactory { /** - * Constructs a top-level (message) PartBuilder object and returns it - * - * @return PartBuilder + * Constructs a top-level (message) PartBuilder object and returns it. */ - public function newPartBuilder(PartHeaderContainer $headerContainer, StreamInterface $messageStream) + public function newPartBuilder(PartHeaderContainer $headerContainer, StreamInterface $messageStream) : PartBuilder { return new PartBuilder($headerContainer, $messageStream); } /** * Constructs a child PartBuilder object with the passed $parent as its - * parent, and returns it - * - * @return PartBuilder + * parent, and returns it. */ - public function newChildPartBuilder(PartHeaderContainer $headerContainer, ParserPartProxy $parent) + public function newChildPartBuilder(PartHeaderContainer $headerContainer, ParserPartProxy $parent) : PartBuilder { return new PartBuilder( $headerContainer, diff --git a/src/Parser/Proxy/ParserMessageProxy.php b/src/Parser/Proxy/ParserMessageProxy.php index 3002de68..f3c0ac9b 100644 --- a/src/Parser/Proxy/ParserMessageProxy.php +++ b/src/Parser/Proxy/ParserMessageProxy.php @@ -21,17 +21,14 @@ class ParserMessageProxy extends ParserMimePartProxy * for a part because the CRLF before a boundary is considered part of * the boundary. */ - protected $lastLineEndingLength = 0; + protected int $lastLineEndingLength = 0; public function getLastLineEndingLength() : int { return $this->lastLineEndingLength; } - /** - * @return static - */ - public function setLastLineEndingLength(int $lastLineEndingLength) + public function setLastLineEndingLength(int $lastLineEndingLength) : static { $this->lastLineEndingLength = $lastLineEndingLength; return $this; diff --git a/src/Parser/Proxy/ParserMessageProxyFactory.php b/src/Parser/Proxy/ParserMessageProxyFactory.php index 19ec1b38..0e04ac5f 100644 --- a/src/Parser/Proxy/ParserMessageProxyFactory.php +++ b/src/Parser/Proxy/ParserMessageProxyFactory.php @@ -29,12 +29,12 @@ class ParserMessageProxyFactory extends ParserMimePartProxyFactory /** * @var MultipartHelper */ - protected $multipartHelper; + protected MultipartHelper $multipartHelper; /** * @var PrivacyHelper */ - protected $privacyHelper; + protected PrivacyHelper $privacyHelper; public function __construct( StreamFactory $sdf, @@ -52,10 +52,8 @@ public function __construct( /** * Constructs a new ParserMessageProxy wrapping an IMessage object that will * dynamically parse a message's content and parts as they're requested. - * - * @return ParserMessageProxy */ - public function newInstance(PartBuilder $partBuilder, IParserService $parser) + public function newInstance(PartBuilder $partBuilder, IParserService $parser) : ParserMessageProxy { $parserProxy = new ParserMessageProxy($partBuilder, $parser); diff --git a/src/Parser/Proxy/ParserMimePartProxy.php b/src/Parser/Proxy/ParserMimePartProxy.php index 0b87ec06..1e69c149 100644 --- a/src/Parser/Proxy/ParserMimePartProxy.php +++ b/src/Parser/Proxy/ParserMimePartProxy.php @@ -7,6 +7,7 @@ namespace ZBateson\MailMimeParser\Parser\Proxy; +use ZBateson\MailMimeParser\Header\IHeader; use ZBateson\MailMimeParser\Header\HeaderConsts; use ZBateson\MailMimeParser\Message\IMessagePart; use Psr\Log\LogLevel; @@ -22,48 +23,53 @@ class ParserMimePartProxy extends ParserPartProxy * @var bool set to true once the end boundary of the currently-parsed * part is found. */ - protected $endBoundaryFound = false; + protected bool $endBoundaryFound = false; /** * @var bool set to true once a boundary belonging to this parent's part * is found. */ - protected $parentBoundaryFound = false; + protected bool $parentBoundaryFound = false; /** - * @var bool|null|string FALSE if not queried for in the content-type - * header of this part, NULL if the current part does not have a - * boundary, and otherwise contains the value of the boundary parameter - * of the content-type header if the part contains one. + * @var ?string NULL if the current part does not have a boundary, and + * otherwise contains the value of the boundary parameter of the + * content-type header if the part contains one. */ - protected $mimeBoundary = false; + private ?string $mimeBoundary = null; + + /** + * @var bool FALSE if not queried for in the content-type header of this + * part and set in $mimeBoundary. + */ + private bool $mimeBoundaryQueried = false; /** * @var bool true once all children of this part have been parsed. */ - protected $allChildrenParsed = false; + protected bool $allChildrenParsed = false; /** * @var ParserPartProxy[] Array of all parsed children. */ - protected $children = []; + protected array $children = []; /** * @var ParserPartProxy[] Parsed children used as a 'first-in-first-out' * stack as children are parsed. */ - protected $childrenStack = []; + protected array $childrenStack = []; /** * @var ParserPartProxy Reference to the last child added to this part. */ - protected $lastAddedChild = null; + protected ?ParserPartProxy $lastAddedChild = null; /** * Ensures that the last child added to this part is fully parsed (content * and children). */ - protected function ensureLastChildParsed() : self + protected function ensureLastChildParsed() : static { if ($this->lastAddedChild !== null) { $this->lastAddedChild->parseAll(); @@ -75,7 +81,7 @@ protected function ensureLastChildParsed() : self * Parses the next child of this part and adds it to the 'stack' of * children. */ - protected function parseNextChild() : self + protected function parseNextChild() : static { if ($this->allChildrenParsed) { return $this; @@ -111,9 +117,8 @@ public function popNextChild() : ?IMessagePart /** * Parses all content and children for this part. - * @return static */ - public function parseAll() + public function parseAll() : static { $this->parseContent(); while (!$this->allChildrenParsed) { @@ -125,10 +130,8 @@ public function parseAll() /** * Returns a ParameterHeader representing the parsed Content-Type header for * this part. - * - * @return ?\ZBateson\MailMimeParser\Header\IHeader */ - public function getContentType() + public function getContentType() : ?IHeader { return $this->getHeaderContainer()->get(HeaderConsts::CONTENT_TYPE); } @@ -139,10 +142,10 @@ public function getContentType() * * @return string */ - public function getMimeBoundary() + public function getMimeBoundary() : ?string { - if ($this->mimeBoundary === false) { - $this->mimeBoundary = null; + if ($this->mimeBoundaryQueried === false) { + $this->mimeBoundaryQueried = true; $contentType = $this->getContentType(); if ($contentType !== null) { $this->mimeBoundary = $contentType->getValueFor('boundary'); @@ -157,10 +160,8 @@ public function getMimeBoundary() * * If the passed $line is the ending boundary for the current part, * $this->isEndBoundaryFound will return true after. - * - * @return bool */ - public function setEndBoundaryFound(string $line) + public function setEndBoundaryFound(string $line) : bool { $boundary = $this->getMimeBoundary(); if ($this->getParent() !== null && $this->getParent()->setEndBoundaryFound($line)) { @@ -204,7 +205,7 @@ public function isEndBoundaryFound() : bool * * @return static */ - public function setEof() : self + public function setEof() : static { $this->parentBoundaryFound = true; if ($this->getParent() !== null) { @@ -218,10 +219,8 @@ public function setEof() : self * if the passed end pos is before the start pos (can happen if a mime * end boundary doesn't have an empty line before the next parent start * boundary). - * - * @return static */ - public function setStreamPartAndContentEndPos(int $streamContentEndPos) + public function setStreamPartAndContentEndPos(int $streamContentEndPos) : static { // check if we're expecting a boundary and didn't find one if (!$this->endBoundaryFound && !$this->parentBoundaryFound) { @@ -247,10 +246,8 @@ public function setStreamPartAndContentEndPos(int $streamContentEndPos) * ParserMimePartProxy simply calls setLastLineEndingLength on its parent, * which must eventually reach a ParserMessageProxy which actually stores * the length. - * - * @return static */ - public function setLastLineEndingLength(int $length) + public function setLastLineEndingLength(int $length) : static { $this->getParent()->setLastLineEndingLength($length); return $this; @@ -274,7 +271,6 @@ public function getLastLineEndingLength() : int /** * Returns the last part that was added. - * @return ParserPartProxy|null */ public function getLastAddedChild() : ?ParserPartProxy { @@ -284,8 +280,6 @@ public function getLastAddedChild() : ?ParserPartProxy /** * Returns the added child at the provided index, useful for looking at * previously parsed children. - * - * @return ParserPartProxy|null */ public function getAddedChildAt(int $index) : ?ParserPartProxy { diff --git a/src/Parser/Proxy/ParserMimePartProxyFactory.php b/src/Parser/Proxy/ParserMimePartProxyFactory.php index 163bd2a8..cc4cd3fd 100644 --- a/src/Parser/Proxy/ParserMimePartProxyFactory.php +++ b/src/Parser/Proxy/ParserMimePartProxyFactory.php @@ -26,22 +26,22 @@ class ParserMimePartProxyFactory extends ParserPartProxyFactory /** * @var StreamFactory the StreamFactory instance */ - protected $streamFactory; + protected StreamFactory $streamFactory; /** * @var ParserPartStreamContainerFactory */ - protected $parserPartStreamContainerFactory; + protected ParserPartStreamContainerFactory $parserPartStreamContainerFactory; /** * @var PartHeaderContainerFactory */ - protected $partHeaderContainerFactory; + protected PartHeaderContainerFactory $partHeaderContainerFactory; /** * @var ParserPartChildrenContainerFactory */ - protected $parserPartChildrenContainerFactory; + protected ParserPartChildrenContainerFactory $parserPartChildrenContainerFactory; public function __construct( StreamFactory $sdf, @@ -59,10 +59,8 @@ public function __construct( * Constructs a new ParserMimePartProxy wrapping an IMimePart object that * will dynamically parse a message's content and parts as they're * requested. - * - * @return ParserMimePartProxy */ - public function newInstance(PartBuilder $partBuilder, IParserService $parser) + public function newInstance(PartBuilder $partBuilder, IParserService $parser) : ParserMimePartProxy { $parserProxy = new ParserMimePartProxy($partBuilder, $parser); diff --git a/src/Parser/Proxy/ParserNonMimeMessageProxy.php b/src/Parser/Proxy/ParserNonMimeMessageProxy.php index ca389540..39905311 100644 --- a/src/Parser/Proxy/ParserNonMimeMessageProxy.php +++ b/src/Parser/Proxy/ParserNonMimeMessageProxy.php @@ -16,23 +16,22 @@ class ParserNonMimeMessageProxy extends ParserMessageProxy { /** - * @var int|null The next part's start position within the message's raw - * stream, or null if not set, not discovered, or there are no more - * parts. + * @var ?int The next part's start position within the message's raw stream + * or null if not set, not discovered, or there are no more parts. */ - protected $nextPartStart = null; + protected ?int $nextPartStart = null; /** - * @var int The next part's unix file mode in a uu-encoded 'begin' line if + * @var ?int The next part's unix file mode in a uu-encoded 'begin' line if * exists, or null otherwise. */ - protected $nextPartMode = null; + protected ?int $nextPartMode = null; /** - * @var string The next part's file name in a uu-encoded 'begin' line if + * @var ?string The next part's file name in a uu-encoded 'begin' line if * exists, or null otherwise. */ - protected $nextPartFilename = null; + protected ?string $nextPartFilename = null; /** * Returns the next part's start position within the message's raw stream, @@ -70,9 +69,8 @@ public function getNextPartFilename() : ?string /** * Sets the next part's start position within the message's raw stream. - * */ - public function setNextPartStart(int $nextPartStart) : self + public function setNextPartStart(int $nextPartStart) : static { $this->nextPartStart = $nextPartStart; return $this; @@ -81,7 +79,7 @@ public function setNextPartStart(int $nextPartStart) : self /** * Sets the next part's unix file mode from its 'begin' line. */ - public function setNextPartMode(int $nextPartMode) : self + public function setNextPartMode(int $nextPartMode) : static { $this->nextPartMode = $nextPartMode; return $this; @@ -91,7 +89,7 @@ public function setNextPartMode(int $nextPartMode) : self * Sets the next part's filename from its 'begin' line. * */ - public function setNextPartFilename(string $nextPartFilename) : self + public function setNextPartFilename(string $nextPartFilename) : static { $this->nextPartFilename = $nextPartFilename; return $this; @@ -100,7 +98,7 @@ public function setNextPartFilename(string $nextPartFilename) : self /** * Sets the next part start position, file mode, and filename to null */ - public function clearNextPart() : self + public function clearNextPart() : static { $this->nextPartStart = null; $this->nextPartMode = null; diff --git a/src/Parser/Proxy/ParserNonMimeMessageProxyFactory.php b/src/Parser/Proxy/ParserNonMimeMessageProxyFactory.php index 46793369..3425680f 100644 --- a/src/Parser/Proxy/ParserNonMimeMessageProxyFactory.php +++ b/src/Parser/Proxy/ParserNonMimeMessageProxyFactory.php @@ -21,10 +21,8 @@ class ParserNonMimeMessageProxyFactory extends ParserMessageProxyFactory { /** * Constructs a new ParserNonMimeMessageProxy wrapping an IMessage object. - * - * @return ParserMimePartProxy */ - public function newInstance(PartBuilder $partBuilder, IParserService $parser) + public function newInstance(PartBuilder $partBuilder, IParserService $parser) : ParserNonMimeMessageProxy { $parserProxy = new ParserNonMimeMessageProxy($partBuilder, $parser); diff --git a/src/Parser/Proxy/ParserPartProxy.php b/src/Parser/Proxy/ParserPartProxy.php index 63f9dda8..5732f26a 100644 --- a/src/Parser/Proxy/ParserPartProxy.php +++ b/src/Parser/Proxy/ParserPartProxy.php @@ -10,6 +10,8 @@ use ZBateson\MailMimeParser\Message\IMessagePart; use ZBateson\MailMimeParser\Parser\IParserService; use ZBateson\MailMimeParser\Parser\PartBuilder; +use ZBateson\MailMimeParser\Message\PartHeaderContainer; +use Psr\Http\Message\StreamInterface; /** * Proxy between a MessagePart and a Parser. @@ -25,17 +27,17 @@ abstract class ParserPartProxy extends PartBuilder /** * @var IParserService The parser. */ - protected $parser; + protected IParserService $parser; /** * @var PartBuilder The part's PartBuilder. */ - protected $partBuilder; + protected PartBuilder $partBuilder; /** * @var IMessagePart The part. */ - private $part; + private IMessagePart $part; public function __construct(PartBuilder $partBuilder, IParserService $parser) { @@ -48,7 +50,7 @@ public function __construct(PartBuilder $partBuilder, IParserService $parser) * * @param IMessagePart $part The part */ - public function setPart(IMessagePart $part) : self + public function setPart(IMessagePart $part) : static { $this->part = $part; return $this; @@ -59,7 +61,7 @@ public function setPart(IMessagePart $part) : self * * @return IMessagePart the part. */ - public function getPart() + public function getPart() : IMessagePart { return $this->part; } @@ -71,10 +73,8 @@ public function getPart() * * The method first checks to see if the content has already been parsed, * and is safe to call multiple times. - * - * @return static */ - public function parseContent() + public function parseContent() : static { if (!$this->isContentParsed()) { $this->parser->parseContent($this); @@ -87,31 +87,32 @@ public function parseContent() * * For ParserPartProxy, this is just content, but sub-classes may override * this to parse all children as well for example. - * - * @return static */ - public function parseAll() + public function parseAll() : static { $this->parseContent(); return $this; } - public function getParent() + public function getParent() : ?ParserPartProxy { return $this->partBuilder->getParent(); } - public function getHeaderContainer() + public function getHeaderContainer() : PartHeaderContainer { return $this->partBuilder->getHeaderContainer(); } - public function getStream() + public function getStream() : StreamInterface { return $this->partBuilder->getStream(); } - public function getMessageResourceHandle() + /** + * @return resource + */ + public function getMessageResourceHandle() : mixed { return $this->partBuilder->getMessageResourceHandle(); } @@ -141,43 +142,31 @@ public function getStreamContentLength() : int return $this->partBuilder->getStreamContentLength(); } - /** - * @return static - */ - public function setStreamPartStartPos(int $streamPartStartPos) + public function setStreamPartStartPos(int $streamPartStartPos) : static { $this->partBuilder->setStreamPartStartPos($streamPartStartPos); return $this; } - /** - * @return static - */ - public function setStreamPartEndPos(int $streamPartEndPos) + public function setStreamPartEndPos(int $streamPartEndPos) : static { $this->partBuilder->setStreamPartEndPos($streamPartEndPos); return $this; } - /** - * @return static - */ - public function setStreamContentStartPos(int $streamContentStartPos) + public function setStreamContentStartPos(int $streamContentStartPos) : static { $this->partBuilder->setStreamContentStartPos($streamContentStartPos); return $this; } - /** - * @return static - */ - public function setStreamPartAndContentEndPos(int $streamContentEndPos) + public function setStreamPartAndContentEndPos(int $streamContentEndPos) : static { $this->partBuilder->setStreamPartAndContentEndPos($streamContentEndPos); return $this; } - public function isContentParsed() : ?bool + public function isContentParsed() : bool { return $this->partBuilder->isContentParsed(); } diff --git a/src/Parser/Proxy/ParserPartProxyFactory.php b/src/Parser/Proxy/ParserPartProxyFactory.php index 7a69d54f..4ccbc628 100644 --- a/src/Parser/Proxy/ParserPartProxyFactory.php +++ b/src/Parser/Proxy/ParserPartProxyFactory.php @@ -22,5 +22,5 @@ abstract class ParserPartProxyFactory * * @return ParserPartProxy */ - abstract public function newInstance(PartBuilder $partBuilder, IParserService $parser); + abstract public function newInstance(PartBuilder $partBuilder, IParserService $parser) : ParserPartProxy; } diff --git a/src/Parser/Proxy/ParserUUEncodedPartProxy.php b/src/Parser/Proxy/ParserUUEncodedPartProxy.php index ca509692..df3cecf6 100644 --- a/src/Parser/Proxy/ParserUUEncodedPartProxy.php +++ b/src/Parser/Proxy/ParserUUEncodedPartProxy.php @@ -14,6 +14,15 @@ */ class ParserUUEncodedPartProxy extends ParserPartProxy { + /** + * Only has a single parent of type ParserNonMimeMessageProxy, overridden to + * specify ParserNonMimeMessageProxy as the return type. + */ + public function getParent() : ParserNonMimeMessageProxy + { + return parent::getParent(); + } + /** * Returns the next part's start position within the message's raw stream, * or null if not set, not discovered, or there are no more parts under this @@ -67,7 +76,7 @@ public function getNextPartFilename() : ?string * setNextPartStart() on its parent (a ParserNonMimeMessageProxy, which * stores/returns this information). */ - public function setNextPartStart(int $nextPartStart) : self + public function setNextPartStart(int $nextPartStart) : static { $this->getParent()->setNextPartStart($nextPartStart); return $this; @@ -80,7 +89,7 @@ public function setNextPartStart(int $nextPartStart) : self * setNextPartMode() on its parent (a ParserNonMimeMessageProxy, which * stores/returns this information). */ - public function setNextPartMode(int $nextPartMode) : self + public function setNextPartMode(int $nextPartMode) : static { $this->getParent()->setNextPartMode($nextPartMode); return $this; @@ -93,7 +102,7 @@ public function setNextPartMode(int $nextPartMode) : self * setNextPartFilename() on its parent (a ParserNonMimeMessageProxy, which * stores/returns this information). */ - public function setNextPartFilename(string $nextPartFilename) : self + public function setNextPartFilename(string $nextPartFilename) : static { $this->getParent()->setNextPartFilename($nextPartFilename); return $this; diff --git a/src/Parser/Proxy/ParserUUEncodedPartProxyFactory.php b/src/Parser/Proxy/ParserUUEncodedPartProxyFactory.php index e9bde085..ed0df3e4 100644 --- a/src/Parser/Proxy/ParserUUEncodedPartProxyFactory.php +++ b/src/Parser/Proxy/ParserUUEncodedPartProxyFactory.php @@ -24,12 +24,12 @@ class ParserUUEncodedPartProxyFactory extends ParserPartProxyFactory /** * @var StreamFactory the StreamFactory instance */ - protected $streamFactory; + protected StreamFactory $streamFactory; /** * @var ParserPartStreamContainerFactory */ - protected $parserPartStreamContainerFactory; + protected ParserPartStreamContainerFactory $parserPartStreamContainerFactory; public function __construct(StreamFactory $sdf, ParserPartStreamContainerFactory $parserPartStreamContainerFactory) { @@ -39,10 +39,8 @@ public function __construct(StreamFactory $sdf, ParserPartStreamContainerFactory /** * Constructs a new ParserUUEncodedPartProxy wrapping an IUUEncoded object. - * - * @return ParserPartProxy */ - public function newInstance(PartBuilder $partBuilder, IParserService $parser) + public function newInstance(PartBuilder $partBuilder, IParserService $parser) : ParserUUEncodedPartProxy { $parserProxy = new ParserUUEncodedPartProxy($partBuilder, $parser); $streamContainer = $this->parserPartStreamContainerFactory->newInstance($parserProxy); diff --git a/src/Stream/HeaderStream.php b/src/Stream/HeaderStream.php index e24f2db8..49090ce0 100644 --- a/src/Stream/HeaderStream.php +++ b/src/Stream/HeaderStream.php @@ -8,6 +8,7 @@ namespace ZBateson\MailMimeParser\Stream; use ArrayIterator; +use Iterator; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\StreamDecoratorTrait; use Psr\Http\Message\StreamInterface; @@ -31,15 +32,12 @@ class HeaderStream implements SplObserver, StreamInterface { use StreamDecoratorTrait; - /** - * @var StreamInterface - */ - private $stream; + private ?StreamInterface $stream; /** * @var IMessagePart the part to read from. */ - protected $part; + protected IMessagePart $part; public function __construct(IMessagePart $part) { @@ -53,9 +51,7 @@ public function __construct(IMessagePart $part) public function __destruct() { - if ($this->part !== null) { - $this->part->detach($this); - } + $this->part->detach($this); } public function update(SplSubject $subject) : void @@ -70,9 +66,8 @@ public function update(SplSubject $subject) : void * * If the part is not a MimePart, Content-Type, Content-Disposition and * Content-Transfer-Encoding headers are generated manually. - * */ - private function getPartHeadersIterator() : \Iterator + private function getPartHeadersIterator() : Iterator { if ($this->part instanceof IMimePart) { return $this->part->getRawHeaderIterator(); @@ -89,7 +84,7 @@ private function getPartHeadersIterator() : \Iterator /** * Writes out headers for $this->part and follows them with an empty line. */ - public function writePartHeadersTo(StreamInterface $stream) : self + public function writePartHeadersTo(StreamInterface $stream) : static { foreach ($this->getPartHeadersIterator() as $header) { $stream->write("{$header[0]}: {$header[1]}\r\n"); @@ -100,7 +95,6 @@ public function writePartHeadersTo(StreamInterface $stream) : self /** * Creates the underlying stream lazily when required. - * */ protected function createStream() : StreamInterface { diff --git a/src/Stream/MessagePartStream.php b/src/Stream/MessagePartStream.php index 2449edcd..bf253a56 100644 --- a/src/Stream/MessagePartStream.php +++ b/src/Stream/MessagePartStream.php @@ -27,25 +27,22 @@ class MessagePartStream implements SplObserver, StreamInterface { use StreamDecoratorTrait; - /** - * @var StreamInterface - */ - private $stream; + private ?StreamInterface $stream; /** * @var StreamFactory For creating needed stream decorators. */ - protected $streamFactory; + protected StreamFactory $streamFactory; /** * @var IMessagePart The part to read from. */ - protected $part; + protected IMessagePart $part; /** * @var ?AppendStream */ - protected $appendStream = null; + protected ?AppendStream $appendStream = null; /** * Constructor @@ -64,9 +61,7 @@ public function __construct(StreamFactory $sdf, IMessagePart $part) public function __destruct() { - if ($this->part !== null) { - $this->part->detach($this); - } + $this->part->detach($this); } public function update(SplSubject $subject) : void @@ -109,7 +104,6 @@ private function getCharsetDecoratorForStream(StreamInterface $stream) : StreamI * o QuotedPrintableStream * o Base64Stream * o UUStream - * */ private function getTransferEncodingDecoratorForStream(StreamInterface $stream) : StreamInterface { @@ -138,7 +132,7 @@ private function getTransferEncodingDecoratorForStream(StreamInterface $stream) * Writes out the content portion of the attached mime part to the passed * $stream. */ - private function writePartContentTo(StreamInterface $stream) : self + private function writePartContentTo(StreamInterface $stream) : static { $contentStream = $this->part->getContentStream(); if ($contentStream !== null) { @@ -205,7 +199,6 @@ protected function getStreamsArray() : array /** * Creates the underlying stream lazily when required. - * */ protected function createStream() : StreamInterface { diff --git a/src/Stream/StreamFactory.php b/src/Stream/StreamFactory.php index effa4abd..59038d9f 100644 --- a/src/Stream/StreamFactory.php +++ b/src/Stream/StreamFactory.php @@ -29,10 +29,8 @@ class StreamFactory /** * Returns a SeekingLimitStream using $part->getStreamPartLength() and * $part->getStreamPartStartPos() - * - * @return SeekingLimitStream */ - public function getLimitedPartStream(PartBuilder $part) + public function getLimitedPartStream(PartBuilder $part) : StreamInterface { return $this->newLimitStream( $part->getStream(), @@ -44,10 +42,8 @@ public function getLimitedPartStream(PartBuilder $part) /** * Returns a SeekingLimitStream using $part->getStreamContentLength() and * $part->getStreamContentStartPos() - * - * @return ?SeekingLimitStream */ - public function getLimitedContentStream(PartBuilder $part) + public function getLimitedContentStream(PartBuilder $part) : ?StreamInterface { $length = $part->getStreamContentLength(); if ($length !== 0) { @@ -62,9 +58,8 @@ public function getLimitedContentStream(PartBuilder $part) /** * Creates and returns a SeekingLimitedStream. - * */ - private function newLimitStream(StreamInterface $stream, int $length, int $start) : SeekingLimitStream + private function newLimitStream(StreamInterface $stream, int $length, int $start) : StreamInterface { return new SeekingLimitStream( $this->newNonClosingStream($stream), @@ -76,20 +71,16 @@ private function newLimitStream(StreamInterface $stream, int $length, int $start /** * Creates a non-closing stream that doesn't close it's internal stream when * closing/detaching. - * - * @return NonClosingStream */ - public function newNonClosingStream(StreamInterface $stream) + public function newNonClosingStream(StreamInterface $stream) : StreamInterface { return new NonClosingStream($stream); } /** * Creates a ChunkSplitStream. - * - * @return ChunkSplitStream */ - public function newChunkSplitStream(StreamInterface $stream) + public function newChunkSplitStream(StreamInterface $stream) : StreamInterface { return new ChunkSplitStream($stream); } @@ -97,10 +88,8 @@ public function newChunkSplitStream(StreamInterface $stream) /** * Creates and returns a Base64Stream with an internal * PregReplaceFilterStream that filters out non-base64 characters. - * - * @return Base64Stream */ - public function newBase64Stream(StreamInterface $stream) + public function newBase64Stream(StreamInterface $stream) : StreamInterface { return new Base64Stream( new PregReplaceFilterStream($stream, '/[^a-zA-Z0-9\/\+=]/', '') @@ -109,50 +98,40 @@ public function newBase64Stream(StreamInterface $stream) /** * Creates and returns a QuotedPrintableStream. - * - * @return QuotedPrintableStream */ - public function newQuotedPrintableStream(StreamInterface $stream) + public function newQuotedPrintableStream(StreamInterface $stream) : StreamInterface { return new QuotedPrintableStream($stream); } /** * Creates and returns a UUStream - * - * @return UUStream */ - public function newUUStream(StreamInterface $stream) + public function newUUStream(StreamInterface $stream) : StreamInterface { return new UUStream($stream); } /** * Creates and returns a CharsetStream - * - * @return CharsetStream */ - public function newCharsetStream(StreamInterface $stream, string $fromCharset, string $toCharset) + public function newCharsetStream(StreamInterface $stream, string $fromCharset, string $toCharset) : StreamInterface { return new CharsetStream($stream, $fromCharset, $toCharset); } /** * Creates and returns a MessagePartStream - * - * @return MessagePartStream */ - public function newMessagePartStream(IMessagePart $part) + public function newMessagePartStream(IMessagePart $part) : StreamInterface { return new MessagePartStream($this, $part); } /** * Creates and returns a HeaderStream - * - * @return HeaderStream */ - public function newHeaderStream(IMessagePart $part) + public function newHeaderStream(IMessagePart $part) : StreamInterface { return new HeaderStream($part); } diff --git a/tests/MailMimeParser/ErrorBagTest.php b/tests/MailMimeParser/ErrorBagTest.php index 26593ca7..104fea36 100644 --- a/tests/MailMimeParser/ErrorBagTest.php +++ b/tests/MailMimeParser/ErrorBagTest.php @@ -3,7 +3,6 @@ namespace ZBateson\MailMimeParser; use PHPUnit\Framework\TestCase; -use Psr\Log\NullLogger; use Psr\Log\LogLevel; /** diff --git a/tests/MailMimeParser/Header/AddressHeaderTest.php b/tests/MailMimeParser/Header/AddressHeaderTest.php index 27bea0e6..ac762cbb 100644 --- a/tests/MailMimeParser/Header/AddressHeaderTest.php +++ b/tests/MailMimeParser/Header/AddressHeaderTest.php @@ -101,6 +101,8 @@ public function testSingleAddressWithQuotedName() : void $this->assertCount(1, $addresses); $this->assertEquals('Jürgen Schmürgen', $addresses[0]->getName()); $this->assertEquals('schmuergen@example.com', $addresses[0]->getEmail()); + $this->assertEquals('Jürgen Schmürgen', $header->getPersonName()); + $this->assertEquals('schmuergen@example.com', $header->getEmail()); } public function testComplexSingleAddress() : void diff --git a/tests/MailMimeParser/Message/Helper/GenericHelperTest.php b/tests/MailMimeParser/Message/Helper/GenericHelperTest.php index 70a7f386..26f6f091 100644 --- a/tests/MailMimeParser/Message/Helper/GenericHelperTest.php +++ b/tests/MailMimeParser/Message/Helper/GenericHelperTest.php @@ -4,6 +4,7 @@ use GuzzleHttp\Psr7; use PHPUnit\Framework\TestCase; +use RecursiveArrayIterator; use ZBateson\MailMimeParser\MailMimeParser; /** @@ -241,7 +242,7 @@ public function testMovePartContentAndChildrenWithReplacePart() : void $to->expects($this->once()) ->method('getChildIterator') - ->willReturn([$child1, $child2]); + ->willReturn(new RecursiveArrayIterator([$child1, $child2])); $to->expects($this->once()) ->method('getChildCount') ->willReturn(2); diff --git a/tests/MailMimeParser/Message/Helper/PrivacyHelperTest.php b/tests/MailMimeParser/Message/Helper/PrivacyHelperTest.php index 930893b0..69fe62a0 100644 --- a/tests/MailMimeParser/Message/Helper/PrivacyHelperTest.php +++ b/tests/MailMimeParser/Message/Helper/PrivacyHelperTest.php @@ -140,7 +140,7 @@ public function testSetMessageAsMultipartSigned() : void $this->mockMultipartHelper->expects($this->once()) ->method('enforceMime') - ->willReturn($message); + ->willReturn($this->mockMultipartHelper); $this->mockMimePartFactory ->expects($this->once()) @@ -153,7 +153,7 @@ public function testSetMessageAsMultipartSigned() : void $message->expects($this->once()) ->method('addChild') - ->willReturn($messagePart); + ->willReturn($message); $this->mockMultipartHelper->expects($this->once()) ->method('getUniqueBoundary') ->with('multipart/signed') @@ -188,12 +188,14 @@ public function testSignedMessageStream() : void ->method('getChild') ->with(0) ->willReturnOnConsecutiveCalls(null, $part); + + $stream = Psr7\Utils::streamFor('test'); $part->expects($this->once()) ->method('getStream') - ->willReturn('test'); + ->willReturn($stream); $this->assertNull($helper->getSignedMessageStream($message)); - $this->assertEquals('test', $helper->getSignedMessageStream($message)); + $this->assertEquals($stream, $helper->getSignedMessageStream($message)); } public function testSignedMessageAsString() : void diff --git a/tests/MailMimeParser/Message/MessagePartTest.php b/tests/MailMimeParser/Message/MessagePartTest.php index 4d028e62..ed29ebdf 100644 --- a/tests/MailMimeParser/Message/MessagePartTest.php +++ b/tests/MailMimeParser/Message/MessagePartTest.php @@ -127,17 +127,18 @@ public function testContentStreamAndCharsetOverride() : void ->willReturn('wigidiwamwamwazzle'); $this->partStreamContainer->method('hasContent')->willReturn(true); + $stream = Psr7\Utils::streamFor('Que tonto'); $this->partStreamContainer->expects($this->exactly(2)) ->method('getContentStream') ->withConsecutive( ['wubalubadub-duuuuub', 'wigidiwamwamwazzle', 'oooohweee!'], ['wubalubadub-duuuuub', 'override', 'oooohweee!'] ) - ->willReturn('Que tonto'); + ->willReturn($stream); - $this->assertEquals('Que tonto', $messagePart->getContentStream('oooohweee!')); + $this->assertEquals('Que tonto', $messagePart->getContentStream('oooohweee!')->getContents()); $messagePart->setCharsetOverride('override'); - $this->assertEquals('Que tonto', $messagePart->getContentStream('oooohweee!')); + $this->assertEquals('Que tonto', $messagePart->getContentStream('oooohweee!')->getContents()); } public function testBinaryContentStream() : void diff --git a/tests/MailMimeParser/Message/MimePartTest.php b/tests/MailMimeParser/Message/MimePartTest.php index 0d875588..f983089b 100644 --- a/tests/MailMimeParser/Message/MimePartTest.php +++ b/tests/MailMimeParser/Message/MimePartTest.php @@ -3,6 +3,8 @@ namespace ZBateson\MailMimeParser\Message; use PHPUnit\Framework\TestCase; +use Traversable; +use ZBateson\MailMimeParser\Header\IHeader; /** * Description of MimePartTest @@ -230,15 +232,19 @@ public function testIsSignaturePart() : void public function testGetHeader() : void { $part = $this->getMimePart(); + + $h1 = $this->getMockForAbstractClass(IHeader::class); + $h2 = $this->getMockForAbstractClass(IHeader::class); + $this->mockHeaderContainer ->expects($this->exactly(2)) ->method('get') ->withConsecutive( ['foist', 0], ['sekint', 1] - )->willReturnOnConsecutiveCalls('giggidyfoist', 'giggidysekint'); - $this->assertEquals('giggidyfoist', $part->getHeader('foist')); - $this->assertEquals('giggidysekint', $part->getHeader('sekint', 1)); + )->willReturnOnConsecutiveCalls($h1, $h2); + $this->assertEquals($h1, $part->getHeader('foist')); + $this->assertEquals($h2, $part->getHeader('sekint', 1)); } public function testGetHeaderAs() : void @@ -260,42 +266,55 @@ public function testGetHeaderAs() : void public function testGetAllHeaders() : void { $part = $this->getMimePart(); + $headers = [ + $this->getMockForAbstractClass(IHeader::class), + $this->getMockForAbstractClass(IHeader::class) + ]; $this->mockHeaderContainer ->expects($this->once()) ->method('getHeaderObjects') - ->willReturn('noice'); - $this->assertEquals('noice', $part->getAllHeaders()); + ->willReturn($headers); + $this->assertEquals($headers, $part->getAllHeaders()); } public function testGetAllHeadersByName() : void { $part = $this->getMimePart(); + $headers = [ + $this->getMockForAbstractClass(IHeader::class), + $this->getMockForAbstractClass(IHeader::class) + ]; $this->mockHeaderContainer ->expects($this->once()) ->method('getAll') ->with('dahoida') - ->willReturn('noice'); - $this->assertEquals('noice', $part->getAllHeadersByName('dahoida')); + ->willReturn($headers); + $this->assertEquals($headers, $part->getAllHeadersByName('dahoida')); } public function testGetRawHeaders() : void { $part = $this->getMimePart(); + $headers = [ + $this->getMockForAbstractClass(IHeader::class), + $this->getMockForAbstractClass(IHeader::class) + ]; $this->mockHeaderContainer ->expects($this->once()) ->method('getHeaders') - ->willReturn('noice'); - $this->assertEquals('noice', $part->getRawHeaders()); + ->willReturn($headers); + $this->assertEquals($headers, $part->getRawHeaders()); } public function testGetRawHeadersIterator() : void { $part = $this->getMimePart(); + $iter = $this->getMockForAbstractClass(Traversable::class); $this->mockHeaderContainer ->expects($this->once()) ->method('getIterator') - ->willReturn('noice'); - $this->assertEquals('noice', $part->getRawHeaderIterator()); + ->willReturn($iter); + $this->assertEquals($iter, $part->getRawHeaderIterator()); } public function testGetHeaderValue() : void diff --git a/tests/MailMimeParser/Message/PartChildrenContainerTest.php b/tests/MailMimeParser/Message/PartChildrenContainerTest.php index c56f4553..5c0399f6 100644 --- a/tests/MailMimeParser/Message/PartChildrenContainerTest.php +++ b/tests/MailMimeParser/Message/PartChildrenContainerTest.php @@ -37,8 +37,6 @@ public function testHasAndGetChildren() : void $this->assertFalse($this->instance->hasChildren()); $part = $this->getIMultiPart(); $this->instance->add($part); - $this->assertFalse($this->instance->hasChildren()); - $this->assertNull($this->instance->getChildren()); $part->method('getChildIterator')->willReturn($this->instance); $this->assertTrue($this->instance->hasChildren()); diff --git a/tests/MailMimeParser/MessageTest.php b/tests/MailMimeParser/MessageTest.php index 33c53b44..39bb561a 100644 --- a/tests/MailMimeParser/MessageTest.php +++ b/tests/MailMimeParser/MessageTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use ZBateson\MailMimeParser\Message\PartChildrenContainer; +use GuzzleHttp\Psr7\Utils; /** * Description of MessageTest @@ -133,9 +134,11 @@ public function testGetTextPartAndTextPartCount() : void $parts[4]->method('getContentType') ->willReturn('Wheeep'); + $str1 = Utils::streamFor('oufa baloufa!'); + $str2 = Utils::streamFor('tilkomore'); $parts[1]->expects($this->once()) ->method('getContentStream') - ->willReturn('oufa baloufa!'); + ->willReturn($str1); $parts[1]->expects($this->once()) ->method('getContent') ->with('charset') @@ -143,7 +146,7 @@ public function testGetTextPartAndTextPartCount() : void $parts[3] ->method('getContentStream') ->with('charset') - ->willReturn('tilkomore'); + ->willReturn($str2); $this->assertEquals(2, $message->getTextPartCount()); $this->assertSame($parts[1], $message->getTextPart()); @@ -151,9 +154,9 @@ public function testGetTextPartAndTextPartCount() : void $this->assertNull($message->getTextPart(2)); $this->assertNull($message->getTextStream(2)); $this->assertNull($message->getTextContent(2)); - $this->assertEquals('oufa baloufa!', $message->getTextStream()); + $this->assertEquals($str1, $message->getTextStream()); $this->assertEquals('shabadabada...', $message->getTextContent(0, 'charset')); - $this->assertEquals('tilkomore', $message->getTextStream(1, 'charset')); + $this->assertEquals($str2, $message->getTextStream(1, 'charset')); } public function testGetHtmlPartAndHtmlPartCount() : void @@ -173,9 +176,11 @@ public function testGetHtmlPartAndHtmlPartCount() : void $parts[4]->method('getContentType') ->willReturn('Wheeep'); + $str1 = Utils::streamFor('oufa baloufa!'); + $str2 = Utils::streamFor('tilkomore'); $parts[1]->expects($this->once()) ->method('getContentStream') - ->willReturn('oufa baloufa!'); + ->willReturn($str1); $parts[1]->expects($this->once()) ->method('getContent') ->with('charset') @@ -183,7 +188,7 @@ public function testGetHtmlPartAndHtmlPartCount() : void $parts[3] ->method('getContentStream') ->with('charset') - ->willReturn('tilkomore'); + ->willReturn($str2); $this->assertEquals(2, $message->getHtmlPartCount()); $this->assertEquals($parts[1], $message->getHtmlPart()); @@ -191,9 +196,9 @@ public function testGetHtmlPartAndHtmlPartCount() : void $this->assertNull($message->getHtmlPart(2)); $this->assertNull($message->getHtmlStream(2)); $this->assertNull($message->getHtmlContent(2)); - $this->assertEquals('oufa baloufa!', $message->getHtmlStream()); + $this->assertEquals($str1, $message->getHtmlStream()); $this->assertEquals('shabadabada...', $message->getHtmlContent(0, 'charset')); - $this->assertEquals('tilkomore', $message->getHtmlStream(1, 'charset')); + $this->assertEquals($str2, $message->getHtmlStream(1, 'charset')); } public function testGetAndRemoveAttachmentParts() : void @@ -291,7 +296,7 @@ public function testSetAndRemoveTextPart() : void public function testAddAttachmentPart() : void { $helper = $this->mockMultipartHelper; - $message = $this->newMessage(); + $message = $this->newMessage($this->getChildrenContainerWithChildren()); $part = $message->getPart(2); $helper->expects($this->exactly(2))->method('createAndAddPartForAttachment') @@ -309,7 +314,7 @@ public function testAddAttachmentPart() : void public function testAddAttachmentPartUsingQuotedPrintable() : void { $helper = $this->mockMultipartHelper; - $message = $this->newMessage(); + $message = $this->newMessage($this->getChildrenContainerWithChildren()); $part = $message->getPart(2); $helper->expects($this->exactly(2))->method('createAndAddPartForAttachment') @@ -333,10 +338,11 @@ public function testSigningHelperMethods() : void ->method('getSignedMessageAsString') ->with($message) ->willReturn('test'); + $str1 = Utils::streamFor('test'); $helper->expects($this->once()) ->method('getSignedMessageStream') ->with($message) - ->willReturn('test'); + ->willReturn($str1); $helper->expects($this->once()) ->method('setMessageAsMultipartSigned') ->with($message, 'micalg', 'protocol'); @@ -344,7 +350,7 @@ public function testSigningHelperMethods() : void ->method('setSignature') ->with($message, 'signature body'); - $this->assertEquals('test', $message->getSignedMessageStream()); + $this->assertEquals($str1, $message->getSignedMessageStream()); $this->assertEquals('test', $message->getSignedMessageAsString()); $message->setAsMultipartSigned('micalg', 'protocol'); $message->setSignature('signature body'); diff --git a/tests/MailMimeParser/Parser/MimeParserServiceTest.php b/tests/MailMimeParser/Parser/MimeParserServiceTest.php index b4a56bbb..0bc00c57 100644 --- a/tests/MailMimeParser/Parser/MimeParserServiceTest.php +++ b/tests/MailMimeParser/Parser/MimeParserServiceTest.php @@ -361,9 +361,9 @@ public function testParseNextChild() : void $this->parserManager->expects($this->once()) ->method('createParserProxyFor') ->with($this->partBuilder) - ->willReturn('groot'); + ->willReturn($this->parserPartProxy); - $this->assertSame('groot', $this->instance->parseNextChild($this->parserPartProxy)); + $this->assertSame($this->parserPartProxy, $this->instance->parseNextChild($this->parserPartProxy)); } public function testParseNextChildWhenParentBoundaryFoundReturnsNull() : void diff --git a/tests/MailMimeParser/Parser/NonMimeParserServiceTest.php b/tests/MailMimeParser/Parser/NonMimeParserServiceTest.php index fe35b689..00758510 100644 --- a/tests/MailMimeParser/Parser/NonMimeParserServiceTest.php +++ b/tests/MailMimeParser/Parser/NonMimeParserServiceTest.php @@ -222,7 +222,7 @@ public function testParseNextChild() : void $this->parserManager->expects($this->once()) ->method('createParserProxyFor') ->with($this->partBuilder) - ->willReturn('A little somefin'); + ->willReturn($this->parserMessageProxy); $this->partBuilder->expects($this->once()) ->method('setStreamPartStartPos') @@ -234,7 +234,7 @@ public function testParseNextChild() : void $this->parserMessageProxy->expects($this->once()) ->method('clearNextPart'); - $this->assertSame('A little somefin', $this->instance->parseNextChild($this->parserMessageProxy)); + $this->assertSame($this->parserMessageProxy, $this->instance->parseNextChild($this->parserMessageProxy)); } public function testParseNextChildWithNullNextPartStartOrHandleAtEof() : void diff --git a/tests/MailMimeParser/Parser/ParserManagerServiceTest.php b/tests/MailMimeParser/Parser/ParserManagerServiceTest.php index 10448778..af9e106d 100644 --- a/tests/MailMimeParser/Parser/ParserManagerServiceTest.php +++ b/tests/MailMimeParser/Parser/ParserManagerServiceTest.php @@ -88,12 +88,17 @@ public function testCreateParserProxyForMessage() : void $this->nonMimeParser->expects($this->once()) ->method('getParserMessageProxyFactory') ->willReturn($proxyFactory); + + $parserPartProxy = $this->getMockBuilder(\ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy::class) + ->disableOriginalConstructor() + ->getMock(); + $proxyFactory->expects($this->once()) ->method('newInstance') ->with($partBuilder, $this->nonMimeParser) - ->willReturn('t000st'); + ->willReturn($parserPartProxy); - $this->assertSame('t000st', $instance->createParserProxyFor($partBuilder)); + $this->assertSame($parserPartProxy, $instance->createParserProxyFor($partBuilder)); } public function testCreateParserProxyForPart() : void @@ -105,13 +110,17 @@ public function testCreateParserProxyForPart() : void ->getMock(); $proxyFactory = $this->getMockForAbstractClass(\ZBateson\MailMimeParser\Parser\Proxy\ParserPartProxyFactory::class); + $parserPartProxy = $this->getMockBuilder(\ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy::class) + ->disableOriginalConstructor() + ->getMock(); + $this->mimeParser->expects($this->once()) ->method('canParse') ->with($partBuilder) ->willReturn(true); $partBuilder->expects($this->once()) ->method('getParent') - ->willReturn('non null'); + ->willReturn($parserPartProxy); $this->mimeParser->expects($this->never()) ->method('getParserMessageProxyFactory'); $this->mimeParser->expects($this->once()) @@ -121,11 +130,13 @@ public function testCreateParserProxyForPart() : void ->method('canParse'); $this->nonMimeParser->expects($this->never()) ->method('getParserPartProxyFactory'); + + $proxyFactory->expects($this->once()) ->method('newInstance') ->with($partBuilder, $this->mimeParser) - ->willReturn('t000st'); + ->willReturn($parserPartProxy); - $this->assertSame('t000st', $instance->createParserProxyFor($partBuilder)); + $this->assertSame($parserPartProxy, $instance->createParserProxyFor($partBuilder)); } } diff --git a/tests/MailMimeParser/Parser/Proxy/ParserMimePartProxyTest.php b/tests/MailMimeParser/Parser/Proxy/ParserMimePartProxyTest.php index 5f7dae99..c2473d0c 100644 --- a/tests/MailMimeParser/Parser/Proxy/ParserMimePartProxyTest.php +++ b/tests/MailMimeParser/Parser/Proxy/ParserMimePartProxyTest.php @@ -3,6 +3,7 @@ namespace ZBateson\MailMimeParser\Parser\Proxy; use PHPUnit\Framework\TestCase; +use ZBateson\MailMimeParser\Header\IHeader; /** * ParserMimePartProxyTest @@ -98,7 +99,7 @@ public function testPopNextChild() : void $this->partBuilder ->expects($this->any()) ->method('isContentParsed') - ->willReturnOnConsecutiveCalls(true); + ->willReturn(true); $c = $this->getMockBuilder(\ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy::class) ->disableOriginalConstructor(); @@ -177,7 +178,7 @@ public function testParseAllParsesContentAndChildren() : void $this->partBuilder ->expects($this->any()) ->method('isContentParsed') - ->willReturnOnConsecutiveCalls(true); + ->willReturn(true); $c = $this->getMockBuilder(\ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy::class) ->disableOriginalConstructor(); @@ -202,7 +203,7 @@ public function testPopChildrenAfterParseAll() : void $this->partBuilder ->expects($this->any()) ->method('isContentParsed') - ->willReturnOnConsecutiveCalls(true); + ->willReturn(true); $c = $this->getMockBuilder(\ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy::class) ->disableOriginalConstructor(); @@ -261,11 +262,13 @@ public function testGetContentType() : void $this->partBuilder->expects($this->once()) ->method('getHeaderContainer') ->willReturn($this->headerContainer); + + $tst = $this->getMockForAbstractClass(IHeader::class); $this->headerContainer->expects($this->any()) ->method('get') ->with($this->equalTo('Content-Type')) - ->willReturn('Fruity'); - $this->assertSame('Fruity', $instance->getContentType()); + ->willReturn($tst); + $this->assertSame($tst, $instance->getContentType()); } public function testGetMimeBoundary() : void