Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 11, 2024
1 parent 8272630 commit c4d341d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
7 changes: 1 addition & 6 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
parameters:
level: 6
level: 10
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
uncheckedExceptionClasses:
- Error
ignoreErrors:
-
identifier: trait.unused
34 changes: 28 additions & 6 deletions src/AbstractZodiac.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Intervention\Zodiac\Exceptions\RuntimeException;
use Intervention\Zodiac\Interfaces\TranslatableInterface;
use Intervention\Zodiac\Interfaces\ZodiacInterface;
use InvalidArgumentException;
Expand All @@ -26,49 +27,70 @@ abstract class AbstractZodiac implements ZodiacInterface, TranslatableInterface
*
* @see ZodiacInterface::start()
* @throws InvalidFormatException
* @throws RuntimeException
*/
public function start(): Carbon

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 32 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::start() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.
{
return Carbon::create(
$date = Carbon::create(
month: $this->startMonth,
day: $this->startDay
);

if ($date === false) {

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 39 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.
throw new RuntimeException('Unable to create end date of zodiac sign.');
}

return $date;

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 43 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::start() should return Carbon\Carbon but returns Carbon\Carbon|null.
}

/**
* {@inheritdoc}
*
* @see ZodiacInterface::end()
* @throws InvalidFormatException
* @throws RuntimeException
*/
public function end(): Carbon

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.

Check failure on line 53 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::end() has Intervention\Zodiac\Exceptions\RuntimeException in PHPDoc @throws tag but it's not thrown.
{
return Carbon::create(
$date = Carbon::create(
month: $this->endMonth,
day: $this->endDay,
hour: 23,
minute: 59,
second: 59
);

if ($date === false) {

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.

Check failure on line 63 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Strict comparison using === between Carbon\Carbon|null and false will always evaluate to false.
throw new RuntimeException('Unable to create end date of zodiac sign.');
}

return $date;

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.3

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.

Check failure on line 67 in src/AbstractZodiac.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.4

Method Intervention\Zodiac\AbstractZodiac::end() should return Carbon\Carbon but returns Carbon\Carbon|null.
}

/**
* {@inheritdoc}
*
* @see ZodiacInterface::localized()
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function localized(?string $locale = null): ?string
{
$translator = $this->translator($locale);
$key = "zodiacs.{$this->name}";

if ($translator->has($key)) {
return $translator->get($key);
$translated = match ($translator->has($key)) {
true => $translator->get($key),
false => $translator->get("zodiacs::{$key}"),
};

if (is_array($translated)) {
throw new RuntimeException(
'Unable to get translated name from array, should be string.'
);
}

// return packages default message
return $translator->get("zodiacs::{$key}");
return $translated;
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Carbon\Exceptions\InvalidFormatException;
use DateTimeInterface;
use Intervention\Zodiac\Exceptions\NotReadableException;
use Intervention\Zodiac\Exceptions\RuntimeException;
use Intervention\Zodiac\Interfaces\ZodiacInterface;
use Intervention\Zodiac\Interfaces\CalculatorInterface;
use Intervention\Zodiac\Interfaces\TranslatableInterface;
Expand All @@ -17,7 +18,10 @@ class Calculator implements CalculatorInterface, TranslatableInterface
{
use Traits\CanTranslate;

protected const ZODIAC_CLASSNAMES = [
/**
* @var array<string>
*/
protected array $zodiac_classnames = [
Zodiacs\Aquarius::class,
Zodiacs\Aries::class,
Zodiacs\Cancer::class,
Expand All @@ -35,15 +39,16 @@ class Calculator implements CalculatorInterface, TranslatableInterface
/**
* {@inheritdoc}
*
* @throws NotReadableException
* @see ZodiacInterface::zodiac()
* @throws NotReadableException
* @throws RuntimeException
*/
public static function zodiac(int|string|DateTimeInterface $date): ZodiacInterface
{
$calculator = new self();
$date = $calculator->normalizeDate($date);

foreach ($calculator::ZODIAC_CLASSNAMES as $classname) {
foreach ($calculator->zodiac_classnames as $classname) {
try {
$zodiac = new $classname();
if (
Expand All @@ -70,6 +75,7 @@ public static function zodiac(int|string|DateTimeInterface $date): ZodiacInterfa
*
* @see self::zodiac()
* @throws NotReadableException
* @throws RuntimeException
*/
public static function make(int|string|DateTimeInterface $date): ZodiacInterface
{
Expand Down
33 changes: 28 additions & 5 deletions src/Laravel/ZodiacBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Intervention\Zodiac\Laravel;

use DateTimeInterface;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Translation\Translator;
use Intervention\Zodiac\Calculator;
use Intervention\Zodiac\Exceptions\NotReadableException;
use Intervention\Zodiac\Exceptions\RuntimeException;
use Intervention\Zodiac\Interfaces\ZodiacInterface;
use InvalidArgumentException;
use ReflectionException;
Expand All @@ -21,16 +25,35 @@ public function __construct(protected Application $app)
/**
* Create zodiac from input date
*
* @param mixed $date
* @param int|string|DateTimeInterface $date
* @throws NotReadableException
* @throws InvalidArgumentException
* @throws ReflectionException
* @throws RuntimeException
* @throws BindingResolutionException
* @return ZodiacInterface
*/
public function make(mixed $date): ZodiacInterface
public function make(int|string|DateTimeInterface $date): ZodiacInterface
{
return (new Calculator())
->setTranslator($this->app['translator'])
->zodiac($date);
$calculator = new Calculator();
$calculator->setTranslator($this->translator());

return $calculator->zodiac($date);
}

/**
* @return Translator
* @throws BindingResolutionException
* @throws RuntimeException
*/
private function translator(): Translator
{
$translator = $this->app->make(Translator::class);

if (!($translator instanceof Translator)) {
throw new RuntimeException('Unable to resolve translator from Laravel application.');
}

return $translator;
}
}
3 changes: 2 additions & 1 deletion src/Laravel/ZodiacServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Zodiac\Laravel;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class ZodiacServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -33,7 +34,7 @@ public function boot()
*/
public function register()
{
$this->app->singleton('zodiac', function ($app) {
$this->app->singleton('zodiac', function (Application $app) {
return new ZodiacBridge($app);
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/ZodiacComparableDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Intervention\Zodiac\Exceptions\RuntimeException;
use Intervention\Zodiac\Interfaces\ZodiacInterface;
use Intervention\Zodiac\Zodiacs\Capricorn;

Expand All @@ -27,6 +28,7 @@ public function __construct(protected Carbon $date)
*
* @param ZodiacInterface $zodiac
* @throws InvalidFormatException
* @throws RuntimeException
* @return bool
*/
public function isZodiac(ZodiacInterface $zodiac): bool
Expand Down

0 comments on commit c4d341d

Please sign in to comment.