Skip to content

Commit

Permalink
Merge pull request #29 from Chris53897/feature/drop-support-php-7.x
Browse files Browse the repository at this point in the history
feat: drop support of php 7.4, add strong types
  • Loading branch information
lorisleiva authored Sep 13, 2022
2 parents d49dd77 + c462102 commit e3e2097
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
php: ['8.0', '8.1']
stability: [ prefer-lowest, prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} tests
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
}
],
"require": {
"php" : "^7.4 || ^8.0"
"php" : "^8.0"
},
"require-dev": {
"phpunit/phpunit" : "^8.5.21 || ^9.5"
"phpunit/phpunit" : "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
Expand Down
21 changes: 14 additions & 7 deletions src/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class CronExpression
public bool $timeFormat24hours;
public array $translations;

/**
* @throws CronParsingException
* @throws TranslationFileMissingException
*/
public function __construct(string $cron, string $locale = 'en', bool $timeFormat24hours = false)
{
$this->raw = $cron;
Expand All @@ -40,7 +44,7 @@ public function getFields(): array
];
}

public function langCountable(string $type, int $number)
public function langCountable(string $type, int $number): array|string
{
$array = $this->translations[$type];

Expand All @@ -53,14 +57,14 @@ public function lang(string $key, array $replacements = [])
{
$translation = $this->getArrayDot($this->translations['fields'], $key);

foreach ($replacements as $key => $value) {
$translation = str_replace(':' . $key, $value, $translation);
foreach ($replacements as $transKey => $value) {
$translation = str_replace(':' . $transKey, $value, $translation);
}

return $translation;
}

protected function ensureLocaleExists(string $fallbackLocale = 'en')
protected function ensureLocaleExists(string $fallbackLocale = 'en'): void
{
if (! is_dir($this->getTranslationDirectory())) {
$this->locale = $fallbackLocale;
Expand All @@ -70,7 +74,7 @@ protected function ensureLocaleExists(string $fallbackLocale = 'en')
/**
* @throws TranslationFileMissingException
*/
protected function loadTranslations()
protected function loadTranslations(): void
{
$this->translations = [
'days' => $this->loadTranslationFile('days'),
Expand All @@ -81,6 +85,9 @@ protected function loadTranslations()
];
}

/**
* @throws TranslationFileMissingException
*/
protected function loadTranslationFile(string $file)
{
$filename = sprintf('%s/%s.php', $this->getTranslationDirectory(), $file);
Expand All @@ -101,8 +108,8 @@ protected function getArrayDot(array $array, string $key)
{
$keys = explode('.', $key);

foreach ($keys as $key) {
$array = $array[$key];
foreach ($keys as $item) {
$array = $array[$item];
}

return $array;
Expand Down
4 changes: 2 additions & 2 deletions src/CronParsingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class CronParsingException extends Exception
{
public function __construct($cron)
public function __construct(string $cron)
{
parent::__construct("Failed to parse the following CRON expression: {$cron}");
parent::__construct("Failed to parse the following CRON expression: $cron");
}
}
9 changes: 6 additions & 3 deletions src/CronTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CronTranslator
'@hourly' => '0 * * * *'
];

/**
* @throws CronParsingException
*/
public static function translate(string $cron, string $locale = 'en', bool $timeFormat24hours = false): string
{
if (isset(self::$extendedMap[$cron])) {
Expand All @@ -25,7 +28,7 @@ public static function translate(string $cron, string $locale = 'en', bool $time
$expression = new CronExpression($cron, $locale, $timeFormat24hours);
$orderedFields = static::orderFields($expression->getFields());

$translations = array_map(function (Field $field) {
$translations = array_map(static function (Field $field) {
return $field->translate();
}, $orderedFields);

Expand All @@ -35,7 +38,7 @@ public static function translate(string $cron, string $locale = 'en', bool $time
}
}

protected static function orderFields(array $fields)
protected static function orderFields(array $fields): array
{
// Group fields by CRON types.
$onces = static::filterType($fields, 'Once');
Expand Down Expand Up @@ -69,7 +72,7 @@ protected static function orderFields(array $fields)

protected static function filterType(array $fields, ...$types): array
{
return array_filter($fields, function (Field $field) use ($types) {
return array_filter($fields, static function (Field $field) use ($types) {
return $field->hasType(...$types);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/CronType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class CronType
{
const TYPES = [
public const TYPES = [
'Every', 'Increment', 'Multiple', 'Once',
];

Expand Down Expand Up @@ -86,6 +86,6 @@ public static function parse(string $expression): CronType

public function hasType(): bool
{
return in_array($this->type, func_get_args());
return in_array($this->type, func_get_args(), true);
}
}
11 changes: 7 additions & 4 deletions src/DaysOfMonthField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class DaysOfMonthField extends Field
{
public int $position = 2;

public function translateEvery()
/**
* @throws CronParsingException
*/
public function translateEvery(): string
{
if ($this->expression->weekday->hasType('Once')) {
return $this->lang('days_of_week.every', [
Expand All @@ -17,7 +20,7 @@ public function translateEvery()
return $this->lang('days_of_month.every');
}

public function translateIncrement()
public function translateIncrement(): string
{
if ($this->getCount() > 1) {
return $this->lang('days_of_month.multiple_per_increment', [
Expand All @@ -31,7 +34,7 @@ public function translateIncrement()
]);
}

public function translateMultiple()
public function translateMultiple(): string
{
return $this->lang('days_of_month.multiple_per_month', [
'count' => $this->getCount(),
Expand Down Expand Up @@ -61,7 +64,7 @@ public function translateOnce(): ?string
]);
}

public function format()
public function format(): string
{
return $this->langCountable('ordinals', $this->getValue());
}
Expand Down
16 changes: 11 additions & 5 deletions src/DaysOfWeekField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class DaysOfWeekField extends Field
{
public int $position = 4;

public function translateEvery()
public function translateEvery(): string
{
return $this->lang('years.every');
}

public function translateIncrement()
public function translateIncrement(): string
{
if ($this->getCount() > 1) {
return $this->lang('days_of_week.multiple_per_increment', [
Expand All @@ -25,24 +25,30 @@ public function translateIncrement()
]);
}

public function translateMultiple()
public function translateMultiple(): string
{
return $this->lang('days_of_week.multiple_days_a_week', [
'count' => $this->getCount(),
]);
}

public function translateOnce()
/**
* @throws CronParsingException
*/
public function translateOnce(): ?string
{
if ($this->expression->day->hasType('Every') && ! $this->expression->day->dropped) {
return; // DaysOfMonthField adapts to "Every Sunday".
return null; // DaysOfMonthField adapts to "Every Sunday".
}

return $this->lang('days_of_week.once_on_day', [
'day' => $this->format()
]);
}

/**
* @throws CronParsingException
*/
public function format(): string
{
$weekday = $this->getValue() === 0 ? 7 : $this->getValue();
Expand Down
19 changes: 10 additions & 9 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

abstract class Field
{
public CronExpression $expression;
public string $rawField;
public CronType $type;
public bool $dropped = false;
public int $position;

public function __construct(CronExpression $expression, string $rawField)
/**
* @throws CronParsingException
*/
public function __construct(public CronExpression $expression, public string $rawField)
{
$this->expression = $expression;
$this->rawField = $rawField;
$this->type = CronType::parse($rawField);
}

public function translate()
public function translate(): ?string
{
$method = 'translate' . $this->type->type;

if (method_exists($this, $method)) {
return $this->{$method}();
}

return null;
}

public function hasType(): bool
Expand All @@ -46,17 +47,17 @@ public function getIncrement(): ?int
return $this->type->increment;
}

public function getTimes()
public function getTimes(): array|string
{
return $this->langCountable('times', $this->getCount());
}

protected function langCountable(string $key, int $value)
protected function langCountable(string $key, int $value): array|string
{
return $this->expression->langCountable($key, $value);
}

protected function lang(string $key, array $replacements = [])
protected function lang(string $key, array $replacements = []): string
{
return $this->expression->lang($key, $replacements);
}
Expand Down
8 changes: 4 additions & 4 deletions src/HoursField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HoursField extends Field
{
public int $position = 1;

public function translateEvery()
public function translateEvery(): string
{
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.once_an_hour');
Expand All @@ -15,7 +15,7 @@ public function translateEvery()
return $this->lang('hours.every');
}

public function translateIncrement()
public function translateIncrement(): string
{
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.times_per_increment', [
Expand All @@ -42,7 +42,7 @@ public function translateIncrement()
]);
}

public function translateMultiple()
public function translateMultiple(): string
{
if ($this->expression->minute->hasType('Once')) {
return $this->lang('hours.times_per_day', [
Expand All @@ -55,7 +55,7 @@ public function translateMultiple()
]);
}

public function translateOnce()
public function translateOnce(): string
{
$minute = $this->expression->minute->hasType('Once')
? $this->expression->minute
Expand Down
6 changes: 3 additions & 3 deletions src/MinutesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class MinutesField extends Field
{
public int $position = 0;

public function translateEvery()
public function translateEvery(): string
{
return $this->lang('minutes.every');
}

public function translateIncrement()
public function translateIncrement(): string
{
if ($this->getCount() > 1) {
return $this->lang('minutes.times_per_increment', [
Expand All @@ -25,7 +25,7 @@ public function translateIncrement()
]);
}

public function translateMultiple()
public function translateMultiple(): string
{
return $this->lang('minutes.multiple', [
'times' => $this->getTimes(),
Expand Down
Loading

0 comments on commit e3e2097

Please sign in to comment.