Skip to content

Commit

Permalink
phpstan adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
ottosmops committed Mar 24, 2024
1 parent a465c80 commit 3b30383
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 170 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"laravel/framework": "^9"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^7"
"phpunit/phpunit": "^9",
"orchestra/testbench": "^7",
"larastan/larastan": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- vendor/larastan/larastan/extension.neon

parameters:
paths:
- src
level: 6
checkGenericClassInNonGenericObjectType: false
11 changes: 6 additions & 5 deletions src/AntondateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ottosmops\Antondate;

use Illuminate\Support\ServiceProvider;
use Ottosmops\Antondate\ValueObjects\AntonDate;

class AntondateServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -34,17 +35,17 @@ public function register(): void
//$this->mergeConfigFrom(__DIR__.'/../config/antondate.php', 'antondate');

// Register the service the package provides.
$this->app->singleton('antondate', function ($app) {
return new Antondate;
});
//$this->app->singleton('antondate', function ($app) {
// return new AntonDate;
//});
}

/**
* Get the services provided by the provider.
*
* @return array
* @return array<string>
*/
public function provides()
public function provides() : array
{
return ['antondate'];
}
Expand Down
16 changes: 8 additions & 8 deletions src/Casts/AntonDateEndCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class AntonDateEndCast implements CastsAttributes
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Anton\ValueObjects\AntonDate;
* @param array<string|int|bool> $attributes
* @return AntonDate;
*/
public function get($model, $key, $value, $attributes)
public function get($model, $key, $value, $attributes) : AntonDate
{
return AntonDate::createFromString(
$attributes['date_end'] ?? '0000',
Expand All @@ -30,17 +30,17 @@ public function get($model, $key, $value, $attributes)
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param AntonDate $value
* @param array $attributes
* @return array
* @param ?AntonDate $value
* @param array<string|int|bool> $attributes
* @return array<string|int|bool>
*/
public function set($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes) : array
{
/*
* We'll need this to handle nullable columns
*/
if (is_null($value)) {
return;
return [];
}

if (!$value instanceof AntonDate) {
Expand Down
20 changes: 10 additions & 10 deletions src/Casts/AntonDateIntervalCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class AntonDateIntervalCast implements CastsAttributes
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @param array<string|int|bool> $attributes
* @return AntonDateInterval;
*/
public function get($model, $key, $value, $attributes)
public function get($model, $key, $value, $attributes) : AntonDateInterval
{
$anton_date_interval = new AntonDateInterval(
AntonDate::createFromString($attributes['date_start'], $attributes['date_start_ca']),
AntonDate::createFromString($attributes['date_end'],$attributes['date_end_ca'])
AntonDate::createFromString((string) $attributes['date_start'], (bool) $attributes['date_start_ca']),
AntonDate::createFromString((string) $attributes['date_end'], (bool) $attributes['date_end_ca'])
);

return $anton_date_interval;
Expand All @@ -31,13 +31,13 @@ public function get($model, $key, $value, $attributes)
/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $model
* @param string $key
* @param Anton\AntonDateInterval $value
* @param array $attributes
* @return array
* @param AntonDateInterval $value
* @param array<string|int|bool> $attributes
* @return array<string|int|bool>
*/
public function set($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes) : array
{
if (!$value instanceof AntonDateInterval) {
throw new InvalidArgumentException(
Expand Down
17 changes: 9 additions & 8 deletions src/Casts/AntonDateStartCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ottosmops\Antondate\Casts;

use InvalidArgumentException;
use Illuminate\Database\Eloquent\Model;
use Ottosmops\Antondate\ValueObjects\AntonDate;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

Expand All @@ -11,13 +12,13 @@ class AntonDateStartCast implements CastsAttributes
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @param array<string|int|bool> $attributes
* @return AntonDate;
*/
public function get($model, $key, $value, $attributes)
public function get(mixed $model, string $key, mixed $value, array $attributes) : AntonDate
{
return AntonDate::createFromString(
$attributes['date_start'] ?? '0000',
Expand All @@ -30,17 +31,17 @@ public function get($model, $key, $value, $attributes)
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param AntonDate $value
* @param array $attributes
* @return array
* @param ?AntonDate $value
* @param array<string|int|bool> $attributes
* @return array<string|int|bool>
*/
public function set($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes) : array
{
/*
* We'll need this to handle nullable columns
*/
if (is_null($value)) {
return;
return [];
}

if (!$value instanceof AntonDate) {
Expand Down
39 changes: 19 additions & 20 deletions src/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public static function renderDate(
return $html;
}

public static function formatDate($str, $nullable = false)
public static function formatDate(string $string, bool $nullable = false): ?string
{
if ('0000-00-00 00:00:00' != $str
&& '-0001-11-30 00:00:00' != $str
&& null != $str
&& '0000-00-00' != $str
&& '-0001' != $str) {
//$html = $str .' :: ';
$html = AntonDate::compose($str)->formatted();
if ('0000-00-00 00:00:00' != $string
&& '-0001-11-30 00:00:00' != $string
&& null != $string
&& '0000-00-00' != $string
&& '-0001' != $string) {
//$html = $string .' :: ';
$html = AntonDate::compose($string)->formatted();
} else {
if (!$nullable) {
$html = trans('messages.no_date');
Expand Down Expand Up @@ -120,35 +120,35 @@ public static function antonDate2carbonDate($date)
/**
* [year description]
* @param string $date 2003-01-03
* @return string 2003
* @return ?string 2003
*/
public static function year($date)
public static function year(string $date) : ?string
{
if (self::checkIsoDate($date)) {
return date("Y", strtotime($date));
}
return false;
return null;
}

public static function cleanDate($datestring)
public static function cleanDate(string $datestring): string
{
$datestring = preg_replace('/(-00$)/', '', $datestring);
$datestring = preg_replace('/(-00$)/', '', $datestring);
return $datestring;
}

public static function composeDate($year = 0, $month = 0, $day = 0)
public static function composeDate(int $year = 0, int $month = 0, int $day = 0): string
{
$date = '';

$year = ($year >= 1 && $year <= 2100) ? $year : 0;
$date .= str_pad($year, 4, "0", STR_PAD_LEFT);
$date .= str_pad((string) $year, 4, "0", STR_PAD_LEFT);

$month = ($month >= 1 && $month <= 12) ? $month : 0;
$date .= '-' . str_pad($month, 2, "0", STR_PAD_LEFT);
$date .= '-' . str_pad((string) $month, 2, "0", STR_PAD_LEFT);

$day = ($day >= 1 && $day <= 31) ? $day : 0;
$date .= '-' . str_pad($day, 2, "0", STR_PAD_LEFT);
$date .= '-' . str_pad((string) $day, 2, "0", STR_PAD_LEFT);

return $date;
}
Expand All @@ -165,16 +165,15 @@ public static function composeDate($year = 0, $month = 0, $day = 0)
* @author ak
* @version 1.0 | 2014-01-15
*
* @param string $isodate zu ueberpruefendes ISO Datum
* @param string $isoDate zu ueberpruefendes ISO Datum
* @return bool
*
*/
public static function checkIsoDate($isoDate)
public static function checkIsoDate(string $isoDate) : bool
{
$return = false;

if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $isoDate, $parts)) {
if (checkdate($parts[2], $parts[3], $parts[1])) {
if (checkdate((int) $parts[2], (int) $parts[3], (int) $parts[1])) {
$return = true;
}
}
Expand Down
Loading

0 comments on commit 3b30383

Please sign in to comment.