Skip to content

Commit

Permalink
Fix coverage, deprecations and types
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Mar 18, 2024
1 parent ca23875 commit 1584335
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/Header/Consumer/AbstractConsumerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ abstract class AbstractConsumerService implements IConsumerService
protected LoggerInterface $logger;

/**
* @var used to construct IHeaderPart objects
* @var HeaderPartFactory used to construct IHeaderPart objects
*/
protected HeaderPartFactory $partFactory;

/**
* @var the generated token split pattern on first run, so it doesn't
* @var ?string the generated token split pattern on first run, so it doesn't
* need to be regenerated every time.
*/
private ?string $tokenSplitPattern = null;
Expand Down
11 changes: 6 additions & 5 deletions src/Header/HeaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ public function newInstance(string $name, string $value) : IHeader
}

/**
* Creates an IHeader instance for the passed header name and value, and
* returns it.
* Creates an IHeader instance for the passed header name and value using
* the passed IHeader class, and returns it.
*
* @param $name The header name.
* @param $value The header value.
* @return The created header object.
* @param string $name The header name.
* @param string $value The header value.
* @param string $iHeaderClass The class to use for header creation
* @return IHeader The created header object.
*/
public function newInstanceOf(string $name, string $value, string $iHeaderClass) : IHeader
{
Expand Down
1 change: 1 addition & 0 deletions src/Header/IdHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory;
use ZBateson\MailMimeParser\Header\Consumer\IdBaseConsumerService;
use ZBateson\MailMimeParser\Header\Part\CommentPart;

/**
* Represents a Content-ID, Message-ID, In-Reply-To or References header.
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Part/AddressGroupPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function getErrorBagChildren() : array

protected function validate() : void
{
if (mb_strlen($this->value) === 0) {
if ($this->value === null || mb_strlen($this->value) === 0) {
$this->addError('Address group doesn\'t have a name', LogLevel::ERROR);
}
if (empty($this->addresses)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Part/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(MbWrapper $charsetConverter, $value)
*/
public function isSpace() : bool
{
return (\preg_match('/^\s+$/', $this->value) === 1);
return ($this->value !== null && \preg_match('/^\s+$/', $this->value) === 1);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Header/ReceivedHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getValue() : ?string
* exists in this position is returned -- be it contains spaces, or invalid
* characters, etc...
*
* @return The 'FROM' name.
* @return ?string The 'FROM' name.
*/
public function getFromName() : ?string
{
Expand All @@ -131,7 +131,7 @@ public function getFromName() : ?string
* not be valid. More details on how the value is parsed and extracted can
* be found in the class description for {@see ReceivedHeader}.
*
* @return The 'FROM' hostname.
* @return ?string The 'FROM' hostname.
*/
public function getFromHostname() : ?string
{
Expand All @@ -148,7 +148,7 @@ public function getFromHostname() : ?string
* not be valid. More details on how the value is parsed and extracted can
* be found in the class description for {@see ReceivedHeader}.
*
* @return The 'FROM' address.
* @return ?string The 'FROM' address.
*/
public function getFromAddress() : ?string
{
Expand All @@ -165,7 +165,7 @@ public function getFromAddress() : ?string
* exists in this position is returned -- be it contains spaces, or invalid
* characters, etc...
*
* @return The 'BY' name.
* @return ?string The 'BY' name.
*/
public function getByName() : ?string
{
Expand All @@ -182,7 +182,7 @@ public function getByName() : ?string
* not be valid. More details on how the value is parsed and extracted can
* be found in the class description for {@see ReceivedHeader}.
*
* @return The 'BY' hostname.
* @return ?string The 'BY' hostname.
*/
public function getByHostname() : ?string
{
Expand All @@ -199,7 +199,7 @@ public function getByHostname() : ?string
* not be valid. More details on how the value is parsed and extracted can
* be found in the class description for {@see ReceivedHeader}.
*
* @return The 'BY' address.
* @return ?string The 'BY' address.
*/
public function getByAddress() : ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
$partChildrenContainer
);
if ($multipartHelper === null || $privacyHelper === null) {
$di = MailMimeParser::getDependencyContainer();
$di = MailMimeParser::getGlobalContainer();
$multipartHelper = $di[\ZBateson\MailMimeParser\Message\Helper\MultipartHelper::class];
$privacyHelper = $di[\ZBateson\MailMimeParser\Message\Helper\PrivacyHelper::class];
}
Expand Down
7 changes: 5 additions & 2 deletions src/Message/Helper/MultipartHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ZBateson\MailMimeParser\Message\Factory\IUUEncodedPartFactory;
use ZBateson\MailMimeParser\Message\IMessagePart;
use ZBateson\MailMimeParser\Message\IMimePart;
use ZBateson\MailMimeParser\Message\IMultiPart;
use ZBateson\MailMimeParser\Message\PartFilter;

/**
Expand Down Expand Up @@ -155,7 +156,7 @@ public function removeAllContentPartsFromAlternative(
$alternativePart = $message->getPart(0, PartFilter::fromInlineContentType('multipart/alternative'));
}
$message->removePart($rmPart);
if ($alternativePart !== null) {
if ($alternativePart !== null && $alternativePart instanceof IMultiPart) {
if ($alternativePart->getChildCount() === 1) {
$this->genericHelper->replacePart($message, $alternativePart, $alternativePart->getChild(0));
} elseif ($alternativePart->getChildCount() === 0) {
Expand Down Expand Up @@ -385,7 +386,9 @@ public function setContentPartForMimeType(IMessage $message, string $mimeType, m
$part = $this->createContentPartForMimeType($message, $mimeType, $charset);
} else {
$contentType = $part->getContentType();
$part->setRawHeader(HeaderConsts::CONTENT_TYPE, "$contentType;\r\n\tcharset=\"$charset\"");
if ($part instanceof IMimePart) {
$part->setRawHeader(HeaderConsts::CONTENT_TYPE, "$contentType;\r\n\tcharset=\"$charset\"");
}
}
$part->setContent($stringOrHandle);
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Message/IMessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getContentDisposition(?string $default = null) : ?string;
* Returns the content transfer encoding used to encode the content on this
* part, or the value of $default if not defined.
*
* @param $default Optional default value to return if not
* @param ?string $default Optional default value to return if not
* applicable/defined
* @return string|null the transfer encoding defined for the part.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Message/IMultiPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ public function getPartByContentId($contentId) : ?IMessagePart;
* If the $position parameter is non-null, adds the part at the passed
* position index, otherwise adds it as the last child.
*
* @param IMessagePart $part The part to add.
* @param MessagePart $part The part to add.
* @param int $position Optional insertion position 0-based index.
*/
public function addChild(IMessagePart $part, ?int $position = null) : static;
public function addChild(MessagePart $part, ?int $position = null) : static;

/**
* Removes the child part from this part and returns its previous position
Expand Down
2 changes: 1 addition & 1 deletion src/Message/MultiPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getPartByContentId($contentId) : ?IMessagePart
});
}

public function addChild(IMessagePart $part, ?int $position = null) : static
public function addChild(MessagePart $part, ?int $position = null) : static
{
if ($part !== $this) {
$part->parent = $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Proxy/ParserMimePartProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ZBateson\MailMimeParser\Parser\Proxy;

use ZBateson\MailMimeParser\Header\IHeader;
use ZBateson\MailMimeParser\Header\ParameterHeader;
use ZBateson\MailMimeParser\Header\HeaderConsts;
use ZBateson\MailMimeParser\Message\IMessagePart;
use Psr\Log\LogLevel;
Expand Down Expand Up @@ -131,7 +131,7 @@ public function parseAll() : static
* Returns a ParameterHeader representing the parsed Content-Type header for
* this part.
*/
public function getContentType() : ?IHeader
public function getContentType() : ?ParameterHeader
{
return $this->getHeaderContainer()->get(HeaderConsts::CONTENT_TYPE);
}
Expand Down
10 changes: 8 additions & 2 deletions tests/MailMimeParser/Message/Helper/GenericHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ protected function setUp() : void

private function newMockIMimePart() : \ZBateson\MailMimeParser\Message\IMimePart
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\Message\IMimePart::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message\MimePart::class)
->disableOriginalConstructor()
->getMock();
}

private function newMockIMessage() : \ZBateson\MailMimeParser\IMessage
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\IMessage::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message::class)
->disableOriginalConstructor()
->getMock();
}

private function newGenericHelper() : GenericHelper
Expand Down
15 changes: 12 additions & 3 deletions tests/MailMimeParser/Message/Helper/MultipartHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ protected function setUp() : void

private function newMockIMimePart() : \ZBateson\MailMimeParser\Message\IMimePart
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\Message\IMimePart::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message\MimePart::class)
->disableOriginalConstructor()
->getMock();
}

private function newMockIUUEncodedPart() : \ZBateson\MailMimeParser\Message\IUUEncodedPart
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\Message\IUUEncodedPart::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message\UUEncodedPart::class)
->disableOriginalConstructor()
->getMock();
}

private function newMockIMessage() : \ZBateson\MailMimeParser\IMessage
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\IMessage::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message::class)
->disableOriginalConstructor()
->getMock();
}

private function newMultipartHelper() : MultipartHelper
Expand Down
10 changes: 8 additions & 2 deletions tests/MailMimeParser/Message/Helper/PrivacyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ protected function setUp() : void

private function newMockIMimePart() : \ZBateson\MailMimeParser\Message\IMimePart
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\Message\IMimePart::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message\MimePart::class)
->disableOriginalConstructor()
->getMock();
}

private function newMockIMessage() : \ZBateson\MailMimeParser\IMessage
{
return $this->getMockForAbstractClass(\ZBateson\MailMimeParser\IMessage::class);
return $this
->getMockBuilder(\ZBateson\MailMimeParser\Message::class)
->disableOriginalConstructor()
->getMock();
}

private function newPrivacyHelper() : PrivacyHelper
Expand Down
6 changes: 4 additions & 2 deletions tests/MailMimeParser/Parser/Proxy/ParserMimePartProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ZBateson\MailMimeParser\Parser\Proxy;

use PHPUnit\Framework\TestCase;
use ZBateson\MailMimeParser\Header\IHeader;
use ZBateson\MailMimeParser\Header\ParameterHeader;

/**
* ParserMimePartProxyTest
Expand Down Expand Up @@ -263,7 +263,9 @@ public function testGetContentType() : void
->method('getHeaderContainer')
->willReturn($this->headerContainer);

$tst = $this->getMockForAbstractClass(IHeader::class);
$tst = $this->getMockBuilder(ParameterHeader::class)
->disableOriginalConstructor()
->getMock();
$this->headerContainer->expects($this->any())
->method('get')
->with($this->equalTo('Content-Type'))
Expand Down

0 comments on commit 1584335

Please sign in to comment.