Skip to content

Commit

Permalink
Merge pull request #31 from rpkamp/criteria
Browse files Browse the repository at this point in the history
Enforce more strict code style
  • Loading branch information
rpkamp committed Oct 11, 2019
2 parents 520ea89 + bfd9eec commit 78da0db
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor
/composer.lock
/.phpunit.result.cache
/.phpcs-cache
/phpcs.xml
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ phpmd:

# Check code adheres to PSR-2
phpcs:
vendor/bin/phpcs --standard=PSR2 src/
vendor/bin/phpcs

# Run unit tests
unit-tests:
Expand All @@ -29,7 +29,7 @@ else
endif

phpstan:
vendor/bin/phpstan analyze --level max src/ tests/
vendor/bin/phpstan analyze -c phpstan.neon

composer-require-checker:
vendor/bin/composer-require-checker
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"phpstan/phpstan": "^0.11",
"pdepend/pdepend": "^2.5",
"maglnet/composer-require-checker": "^2.0",
"nyholm/psr7": "^1.2"
"nyholm/psr7": "^1.2",
"doctrine/coding-standard": "^6.0"
}
}
39 changes: 39 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="." />
<arg name="extensions" value="php" />
<arg name="parallel" value="80" />
<arg name="cache" value=".phpcs-cache" />
<arg name="colors" />

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps" />

<!-- Directories to be checked -->
<file>src</file>

<rule ref="Doctrine">
<exclude name="Generic.Formatting.SpaceAfterNot.Incorrect" />
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSame" />
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix" />
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment.MultiLinePropertyComment" />
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison.DisallowedYodaComparison" />
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.IncorrectWhitespaceBetweenOpenTagAndDeclare" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.IncorrectWhitespaceBeforeColon" />
</rule>

<!-- require declare(strict_types=1) in each file, with 0 empty lines above and 1 empty line below -->
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1" />
<property name="spacesCountAroundEqualsSign" value="0" />
<property name="newlinesCountAfterDeclare" value="2" />
</properties>
</rule>

<!-- require 0 spaces before colon in return type declaration -->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing.IncorrectWhitespaceBeforeColon"></rule>

<!-- Disallow dynamic creation of arrays -->
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"></rule>
</ruleset>
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: max
paths:
- src/
- tests/
ignoreErrors:
-
message: '~Parameter #8 \$attachments of class rpkamp\\Mailhog\\Message\\Message constructor expects array<rpkamp\\Mailhog\\Message\\Mime\\Attachment>, array<int, stdClass> given\.~'
path: tests/unit/Message/MessageTest.php
9 changes: 8 additions & 1 deletion src/MailhogClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
use rpkamp\Mailhog\Message\MessageFactory;
use rpkamp\Mailhog\Specification\Specification;
use RuntimeException;
use function array_filter;
use function count;
use function iterator_to_array;
use function json_decode;
use function json_encode;
use function rtrim;
use function sprintf;

class MailhogClient
{
Expand Down Expand Up @@ -38,7 +45,7 @@ public function __construct(HttpClient $client, RequestFactory $requestFactory,
/**
* @return Generator|Message[]
*/
public function findAllMessages(int $limit = 50): Generator
public function findAllMessages(int $limit = 50)
{
$start = 0;
while (true) {
Expand Down
6 changes: 5 additions & 1 deletion src/Message/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace rpkamp\Mailhog\Message;

use function preg_match;
use function stripslashes;
use function trim;

class Contact
{
/**
Expand All @@ -11,7 +15,7 @@ class Contact
public $emailAddress;

/**
* @var null|string
* @var string|null
*/
public $name;

Expand Down
8 changes: 6 additions & 2 deletions src/Message/ContactCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Countable;
use IteratorAggregate;
use Traversable;
use function array_map;
use function count;
use function str_getcsv;
use function trim;

class ContactCollection implements Countable, IteratorAggregate
{
Expand All @@ -31,7 +35,7 @@ public static function fromString(string $contacts): ContactCollection

return new self(
array_map(
function (string $contact) {
static function (string $contact) {
return Contact::fromString($contact);
},
array_map('trim', str_getcsv($contacts))
Expand All @@ -55,7 +59,7 @@ public function getIterator(): Traversable
return new ArrayIterator($this->contacts);
}

public function count()
public function count(): int
{
return count($this->contacts);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use InvalidArgumentException;
use rpkamp\Mailhog\Message\Mime\Attachment;
use function sprintf;

class Message
{
Expand Down Expand Up @@ -48,6 +49,9 @@ class Message
*/
public $attachments;

/**
* @param Attachment[] $attachments
*/
public function __construct(
string $messageId,
Contact $sender,
Expand Down
9 changes: 8 additions & 1 deletion src/Message/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
namespace rpkamp\Mailhog\Message;

use rpkamp\Mailhog\Message\Mime\MimePartCollection;
use function quoted_printable_decode;

class MessageFactory
{
/**
* @param mixed[] $mailhogResponse
*/
public static function fromMailhogResponse(array $mailhogResponse): Message
{
$mimeParts = MimePartCollection::fromMailhogResponse($mailhogResponse['MIME']['Parts'] ?? []);
Expand All @@ -26,7 +30,10 @@ public static function fromMailhogResponse(array $mailhogResponse): Message
);
}

private static function getBodyFrom(array $content)
/**
* @param mixed[] $content
*/
private static function getBodyFrom(array $content): string
{
if (isset($content['Headers']['Content-Transfer-Encoding'][0]) &&
$content['Headers']['Content-Transfer-Encoding'][0] === 'quoted-printable'
Expand Down
13 changes: 11 additions & 2 deletions src/Message/Mime/MimePart.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php
declare(strict_types=1);

namespace rpkamp\Mailhog\Message\Mime;

use RuntimeException;
use function base64_decode;
use function explode;
use function preg_match;
use function quoted_printable_decode;
use function stripos;

class MimePart
{
Expand All @@ -12,7 +18,7 @@ class MimePart
private $contentType;

/**
* @var null|string
* @var string|null
*/
private $contentTransferEncoding;

Expand All @@ -22,7 +28,7 @@ class MimePart
private $isAttachment;

/**
* @var null|string
* @var string|null
*/
private $filename;

Expand All @@ -45,6 +51,9 @@ private function __construct(
$this->body = $body;
}

/**
* @param mixed[] $mimePart
*/
public static function fromMailhogResponse(array $mimePart): MimePart
{
$filename = null;
Expand Down
43 changes: 35 additions & 8 deletions src/Message/Mime/MimePartCollection.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
<?php
declare(strict_types=1);

namespace rpkamp\Mailhog\Message\Mime;

use function array_merge;
use function count;
use function stripos;

class MimePartCollection
{
/**
* @var MimePart[]
*/
private $mimeParts;

/**
* @param MimePart[] $mimeParts
*/
private function __construct(array $mimeParts)
{
$this->mimeParts = $mimeParts;
}

public static function fromMailhogResponse(array $mimeParts)
/**
* @param mixed[] $mimeParts
*/
public static function fromMailhogResponse(array $mimeParts): self
{
return new self(self::flattenParts($mimeParts));
}

protected static function flattenParts(array $mimeParts)
/**
* @param mixed[] $mimeParts
*
* @return mixed[]
*/
protected static function flattenParts(array $mimeParts): array
{
$flattenedParts = [];
foreach ($mimeParts as $mimePart) {
Expand All @@ -34,11 +50,14 @@ protected static function flattenParts(array $mimeParts)
return $flattenedParts;
}

public function isEmpty()
public function isEmpty(): bool
{
return count($this->mimeParts) === 0;
}

/**
* @return Attachment[]
*/
public function getAttachments(): array
{
$attachments = [];
Expand All @@ -57,18 +76,26 @@ public function getAttachments(): array
return $attachments;
}

public function getBody()
public function getBody(): string
{
$textBody = '';
foreach ($this->mimeParts as $mimePart) {
if ($mimePart->isAttachment()) {
continue;
}
if (stripos($mimePart->getContentType(), 'text/html') === 0) {
return $mimePart->getBody();
}
if (stripos($mimePart->getContentType(), 'text/plain') === 0 && !$mimePart->isAttachment()) {
$textBody = $mimePart->getBody();
}

foreach ($this->mimeParts as $mimePart) {
if ($mimePart->isAttachment()) {
continue;
}
if (stripos($mimePart->getContentType(), 'text/plain') === 0) {
return $mimePart->getBody();
}
}

return $textBody;
return '';
}
}
3 changes: 2 additions & 1 deletion src/NoSuchMessageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
namespace rpkamp\Mailhog;

use RuntimeException;
use function sprintf;

class NoSuchMessageException extends RuntimeException
{
public static function forMessageId(string $messageId)
public static function forMessageId(string $messageId): self
{
return new self(
sprintf('No message found with messageId "%s"', $messageId)
Expand Down
2 changes: 2 additions & 0 deletions src/Specification/AndSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace rpkamp\Mailhog\Specification;

use rpkamp\Mailhog\Message\Message;
use function array_slice;
use function count;

final class AndSpecification implements Specification
{
Expand Down
1 change: 1 addition & 0 deletions src/Specification/BodySpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace rpkamp\Mailhog\Specification;

use rpkamp\Mailhog\Message\Message;
use function strpos;

final class BodySpecification implements Specification
{
Expand Down
2 changes: 2 additions & 0 deletions src/Specification/OrSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace rpkamp\Mailhog\Specification;

use rpkamp\Mailhog\Message\Message;
use function array_slice;
use function count;

final class OrSpecification implements Specification
{
Expand Down

0 comments on commit 78da0db

Please sign in to comment.