Skip to content

Commit

Permalink
Improve code for phpstand level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 2, 2024
1 parent 33db8b2 commit c9e0b95
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 159 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
analysis:
build: ./
working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpstan analyze --level=4 ./src"
command: bash -c "composer install && ./vendor/bin/phpstan analyze ./src"
volumes:
- ./:/project
standards:
Expand Down
9 changes: 9 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 6
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
uncheckedExceptionClasses:
- Error
16 changes: 10 additions & 6 deletions src/AbstractZodiac.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Intervention\Zodiac;

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use InvalidArgumentException;

abstract class AbstractZodiac
{
Expand All @@ -15,33 +17,34 @@ abstract class AbstractZodiac
*
* @var string
*/
public $name;
public string $name;

/**
* HTML code of zodiac sign
*
* @var string
*/
public $html;
public string $html;

/**
* Start of zodiac sign
*
* @var array
* @var array{month: int, day: int}
*/
public $start;
public array $start;

/**
* End of zodiac sign
*
* @var array
* @var array{month: int, day: int}
*/
public $end;
public array $end;

/**
* Determine if given date matches the current zodiac sign
*
* @param Carbon $date
* @throws InvalidFormatException
* @return bool
*/
public function match(Carbon $date): bool
Expand All @@ -67,6 +70,7 @@ public function match(Carbon $date): bool
/**
* Get localized name of zodiac sign
*
* @throws InvalidArgumentException
* @return string
*/
public function localized(?string $locale = null): ?string
Expand Down
22 changes: 18 additions & 4 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Intervention\Zodiac\Exceptions\NotReadableException;
use InvalidArgumentException;

class Calculator
{
Expand All @@ -15,6 +17,8 @@ class Calculator
* Get zodiac for given date
*
* @param mixed $date
* @throws InvalidArgumentException
* @throws NotReadableException
* @return AbstractZodiac
*/
public static function make($date): AbstractZodiac
Expand All @@ -26,6 +30,8 @@ public static function make($date): AbstractZodiac
* Get zodiac for given date
*
* @param mixed $date
* @throws InvalidArgumentException
* @throws NotReadableException
* @return AbstractZodiac
*/
public function getZodiac($date): AbstractZodiac
Expand All @@ -38,17 +44,25 @@ public function getZodiac($date): AbstractZodiac
}
}

throw new Exceptions\NotReadableException(
throw new NotReadableException(
'Unable to create zodiac from value (' . $date . ')'
);
}

protected function normalizeDate($date): Carbon
/**
* Normalze given date to Carbon object
*
* @param mixed $date
* @throws InvalidArgumentException
* @throws NotReadableException
* @return Carbon
*/
protected function normalizeDate(mixed $date): Carbon
{
try {
return Carbon::parse($date);
} catch (InvalidFormatException) {
throw new Exceptions\NotReadableException(
throw new NotReadableException(
'Unable to create zodiac from value (' . $date . ')'
);
}
Expand All @@ -57,7 +71,7 @@ protected function normalizeDate($date): Carbon
/**
* Returns array of all zodiac classnames
*
* @return array
* @return array<string>
*/
private function getZodiacClassnames(): array
{
Expand Down
5 changes: 5 additions & 0 deletions src/Laravel/ZodiacBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Contracts\Foundation\Application;
use Intervention\Zodiac\AbstractZodiac;
use Intervention\Zodiac\Calculator;
use Intervention\Zodiac\Exceptions\NotReadableException;
use InvalidArgumentException;

class ZodiacBridge
{
Expand All @@ -26,6 +28,8 @@ public function __construct(Application $app)
* Make zodiac from input date
*
* @param mixed $date
* @throws NotReadableException
* @throws InvalidArgumentException
* @return AbstractZodiac
*/
public function make($date): AbstractZodiac
Expand All @@ -36,6 +40,7 @@ public function make($date): AbstractZodiac
/**
* Return calculator with Laravel Translator
*
* @throws InvalidArgumentException
* @return Calculator
*/
private function getTranslatableCalculator(): Calculator
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/ZodiacServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function register()
/**
* Get the services provided by the provider.
*
* @return array
* @return array<string>
*/
public function provides()
{
Expand Down
18 changes: 17 additions & 1 deletion src/Traits/CanTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use Intervention\Zodiac\AbstractZodiac;
use Intervention\Zodiac\Calculator;
use InvalidArgumentException;

trait CanTranslate
{
Expand All @@ -17,13 +20,26 @@ trait CanTranslate
*/
protected $translator;

public function setTranslator(Translator $translator): self
/**
* Set translator
*
* @param Translator $translator
* @return Calculator|AbstractZodiac
*/
public function setTranslator(Translator $translator): Calculator|AbstractZodiac
{
$this->translator = $translator;

return $this;
}

/**
* Create translator
*
* @param null|string $locale
* @throws InvalidArgumentException
* @return Translator
*/
public function getTranslator(?string $locale = null): Translator
{
if (is_a($this->translator, Translator::class)) {
Expand Down
16 changes: 4 additions & 12 deletions src/Zodiacs/Aquarius.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,21 @@ class Aquarius extends AbstractZodiac
{
/**
* Name of zodiac sign
*
* @var string
*/
public $name = 'aquarius';
public string $name = 'aquarius';

/**
* HTML code of zodiac sign
*
* @var string
*/
public $html = '&#9810;';
public string $html = '&#9810;';

/**
* Start day of zodiac sign
*
* @var array
*/
public $start = ['month' => '1', 'day' => '21'];
public array $start = ['month' => 1, 'day' => 21];

/**
* End day of zodiac sign
*
* @var array
*/
public $end = ['month' => '2', 'day' => '19'];
public array $end = ['month' => 2, 'day' => 19];
}
14 changes: 4 additions & 10 deletions src/Zodiacs/Aries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ class Aries extends AbstractZodiac
{
/**
* Name of zodiac sign
*
* @var string
*/
public $name = 'aries';
public string $name = 'aries';

/**
* HTML code of zodiac sign
*
* @var string
*/
public $html = '&#9800;';
public string $html = '&#9800;';

/**
* Start day of zodiac sign
*
* @var array
*/
public $start = ['month' => '3', 'day' => '21'];
public array $start = ['month' => 3, 'day' => 21];

/**
* End day of zodiac sign
*
* @var array
*/
public $end = ['month' => '4', 'day' => '20'];
public array $end = ['month' => 4, 'day' => 20];
}
16 changes: 4 additions & 12 deletions src/Zodiacs/Cancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,21 @@ class Cancer extends AbstractZodiac
{
/**
* Name of zodiac sign
*
* @var string
*/
public $name = 'cancer';
public string $name = 'cancer';

/**
* HTML code of zodiac sign
*
* @var string
*/
public $html = '&#9803;';
public string $html = '&#9803;';

/**
* Start day of zodiac sign
*
* @var array
*/
public $start = ['month' => '6', 'day' => '22'];
public array $start = ['month' => 6, 'day' => 22];

/**
* End day of zodiac sign
*
* @var array
*/
public $end = ['month' => '7', 'day' => '22'];
public array $end = ['month' => 7, 'day' => 22];
}
21 changes: 7 additions & 14 deletions src/Zodiacs/Capricorn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,38 @@
namespace Intervention\Zodiac\Zodiacs;

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Intervention\Zodiac\AbstractZodiac;

class Capricorn extends AbstractZodiac
{
/**
* Name of zodiac sign
*
* @var string
*/
public $name = 'capricorn';
public string $name = 'capricorn';

/**
* HTML code of zodiac sign
*
* @var string
*/
public $html = '&#9809;';
public string $html = '&#9809;';

/**
* Start day of zodiac sign
*
* @var array
*/
public $start = ['month' => '12', 'day' => '22'];
public array $start = ['month' => 12, 'day' => 22];

/**
* End day of zodiac sign
*
* @var array
*/
public $end = ['month' => '1', 'day' => '20'];
public array $end = ['month' => 1, 'day' => 20];

/**
* Determine if given date matches Capricorn
*
* Since Capricorn extends over two different
* years we need some special logic
* Capricorn extends over two different years so we need some special logic
*
* @param Carbon $date
* @throws InvalidFormatException
* @return bool
*/
public function match(Carbon $date): bool
Expand Down

0 comments on commit c9e0b95

Please sign in to comment.