From a4c913ab38f291e2ee7e400820e656886461ba94 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 11 Apr 2019 00:54:59 +0100 Subject: [PATCH] Add more type hints and return types Fixes #20 --- src/Model/Filter.php | 46 ++++++++++++++--------------- src/Service/Builder.php | 20 +++++++------ src/Service/Partials.php | 6 ++-- tests/Unit/Service/PartialsTest.php | 8 ++--- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/Model/Filter.php b/src/Model/Filter.php index dd6686c..76b14ff 100644 --- a/src/Model/Filter.php +++ b/src/Model/Filter.php @@ -15,7 +15,7 @@ class Filter /** * @return static */ - public static function create() + public static function create(): self { return new static(); } @@ -25,7 +25,7 @@ class Filter * * @return $this */ - public function has($value) + public function has(string $value): self { $this->properties['hasTheWord'] = $value; @@ -37,7 +37,7 @@ class Filter * * @return $this */ - public function hasNot($value) + public function hasNot(string $value): self { $this->properties['doesNotHaveTheWord'] = $value; @@ -49,7 +49,7 @@ class Filter * * @return $this */ - public function from($values) + public function from($values): self { if (!empty($values)) { $this->properties['from'] = collect($values)->map(function ($value) { @@ -65,7 +65,7 @@ class Filter * * @return $this */ - public function to($values) + public function to($values): self { if (!empty($values)) { $this->properties['to'] = collect($values)->map(function ($value) { @@ -81,7 +81,7 @@ class Filter * * @return $this */ - public function subject($values) + public function subject($values): self { $this->properties['subject'] = collect($values)->map(function ($value) { return json_encode($value); @@ -93,7 +93,7 @@ class Filter /** * @return $this */ - public function hasAttachment() + public function hasAttachment(): self { $this->properties['hasAttachment'] = 'true'; @@ -107,7 +107,7 @@ class Filter * * @return $this */ - public function fromList($value) + public function fromList($value): self { $value = collect($value)->implode(self::SEPARATOR); $this->has("list:{$value}"); @@ -118,7 +118,7 @@ class Filter /** * @return $this */ - public function excludeChats() + public function excludeChats(): self { $this->properties['excludeChats'] = 'true'; @@ -130,7 +130,7 @@ class Filter * * @return $this */ - public function label($label) + public function label(string $label): self { $this->properties['label'] = $label; @@ -140,7 +140,7 @@ class Filter /** * @return $this */ - public function archive() + public function archive(): self { $this->properties['shouldArchive'] = 'true'; @@ -152,7 +152,7 @@ class Filter * * @return $this */ - public function labelAndArchive($label) + public function labelAndArchive(string $label): self { $this->label($label)->archive(); @@ -162,7 +162,7 @@ class Filter /** * @return $this */ - public function spam() + public function spam(): self { $this->properties['shouldSpam'] = 'true'; $this->properties['shouldNeverSpam'] = 'false'; @@ -173,7 +173,7 @@ class Filter /** * @return $this */ - public function neverSpam() + public function neverSpam(): self { $this->properties['shouldSpam'] = 'false'; $this->properties['shouldNeverSpam'] = 'true'; @@ -184,7 +184,7 @@ class Filter /** * @return $this */ - public function trash() + public function trash(): self { $this->properties['shouldTrash'] = 'true'; @@ -194,7 +194,7 @@ class Filter /** * @return $this */ - public function read() + public function read(): self { $this->properties['markAsRead'] = 'true'; @@ -204,7 +204,7 @@ class Filter /** * @return $this */ - public function star() + public function star(): self { $this->properties['shouldStar'] = 'true'; @@ -216,7 +216,7 @@ class Filter * * @return $this */ - public function forward($to) + public function forward(string $to): self { $this->properties['forwardTo'] = $to; @@ -226,7 +226,7 @@ class Filter /** * @return $this */ - public function important() + public function important(): self { $this->properties['shouldAlwaysMarkAsImportant'] = 'true'; @@ -236,7 +236,7 @@ class Filter /** * @return $this */ - public function notImportant() + public function notImportant(): self { $this->properties['shouldNeverMarkAsImportant'] = 'true'; @@ -248,7 +248,7 @@ class Filter * * @return $this */ - public function categorise($category) + public function categorise(string $category): self { $this->properties['smartLabelToApply'] = $category; @@ -261,7 +261,7 @@ class Filter * @return array * @deprecated toArray() */ - public function getProperties() + public function getProperties(): array { return $this->properties; } @@ -271,7 +271,7 @@ class Filter * * @return array */ - public function toArray() + public function toArray(): array { return $this->properties; } diff --git a/src/Service/Builder.php b/src/Service/Builder.php index 509fe37..40bc4c2 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -39,7 +39,7 @@ class Builder $this->build(); } - public function __toString() + public function __toString(): string { return $this->build(); } @@ -49,7 +49,7 @@ class Builder * * @return string */ - public function getXml() + public function getXml(): string { return $this->xml; } @@ -59,7 +59,7 @@ class Builder * * @return string */ - private function build() + private function build(): void { $prefix = "" . PHP_EOL . ""; $suffix = ''; @@ -82,11 +82,13 @@ class Builder * * @return string */ - private function buildEntry(Filter $filter) + private function buildEntry(Filter $filter): string { - $entry = collect($filter->getProperties())->map(function ($value, $key) { - return $this->buildProperty($value, $key); - })->implode(PHP_EOL); + $entry = collect($filter->toArray()) + ->map(function ($value, $key): string { + return $this->buildProperty($value, $key); + }) + ->implode(PHP_EOL); return collect(['', $entry, ''])->implode(PHP_EOL); } @@ -99,7 +101,7 @@ class Builder * * @return string */ - private function buildProperty($value, $key) + private function buildProperty($value, $key): string { if (collect(['from', 'to'])->contains($key)) { $value = $this->implode($value); @@ -114,7 +116,7 @@ class Builder /** * Implode values with the appropriate prefix, suffix and separator. */ - private function implode($value, $separator = '|') + private function implode($value, $separator = '|'): string { if (is_string($value)) { return $value; diff --git a/src/Service/Partials.php b/src/Service/Partials.php index 70fe0cb..472c092 100644 --- a/src/Service/Partials.php +++ b/src/Service/Partials.php @@ -14,7 +14,7 @@ class Partials * * @return array The loaded filters. */ - public static function load($directoryName = 'filters') + public static function load($directoryName = 'filters'): array { $files = static::getFilePattern($directoryName); @@ -23,7 +23,7 @@ class Partials return include $filename; }) ->flatten(1) - ->all(); + ->toArray(); } /** @@ -36,7 +36,7 @@ class Partials * * @return string The full path. */ - protected static function getFilePattern($directoryName) + protected static function getFilePattern($directoryName): string { return getcwd() . DIRECTORY_SEPARATOR . $directoryName . DIRECTORY_SEPARATOR . '*.php'; } diff --git a/tests/Unit/Service/PartialsTest.php b/tests/Unit/Service/PartialsTest.php index 802f1bf..d9ab241 100644 --- a/tests/Unit/Service/PartialsTest.php +++ b/tests/Unit/Service/PartialsTest.php @@ -18,9 +18,9 @@ class PartialsTest extends TestCase $this->assertCount(3, $filters); - $this->assertSame('foo@example.com', $filters[0]->getProperties()['from'][0]); - $this->assertSame('bar@example.com', $filters[1]->getProperties()['from'][0]); - $this->assertSame('baz@example.com', $filters[2]->getProperties()['from'][0]); + $this->assertSame('foo@example.com', $filters[0]->toArray()['from'][0]); + $this->assertSame('bar@example.com', $filters[1]->toArray()['from'][0]); + $this->assertSame('baz@example.com', $filters[2]->toArray()['from'][0]); } } @@ -29,7 +29,7 @@ class FakePartials extends Partials /** * {@inheritdoc} */ - protected static function getFilePattern($directoryName) + protected static function getFilePattern($directoryName): string { return __DIR__ . '/../../stubs/filters/*.php'; }