Skip to content

Commit

Permalink
Add generic unitOfUnit and unitsInUnit getters
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 19, 2023
1 parent e046a1f commit 04faa67
Show file tree
Hide file tree
Showing 9 changed files with 757 additions and 35 deletions.
46 changes: 46 additions & 0 deletions phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function dumpParameter(string $method, ReflectionParameter $parameter): string
}

$deprecated = [];
$unitOfUnit = [];

foreach ($tags as $tag) {
if (\is_array($tag)) {
Expand Down Expand Up @@ -518,13 +519,58 @@ function dumpParameter(string $method, ReflectionParameter $parameter): string
continue;
}

if (
\in_array($tag, ['property', 'property-read'], true) &&
preg_match('/^([a-z]{2,})(In|Of)([A-Z][a-z]+)$/', $vars->name)
) {
$unitOfUnit[$vars->name] = [
'@'.$tag,
$vars->type,
'$'.$variable,
$description ?: '',
];

continue;
}

$autoDocLines[] = [
'@'.$tag,
$vars->type,
'$'.$variable,
$description ?: '',
];
}

if ($tag === 'property-read') {
$units = ['microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years', 'decades', 'centuries', 'millennia'];

foreach ($units as $small) {
array_shift($units);

foreach ($units as $big) {
$singularSmall = Carbon::singularUnit($small);
$singularBig = Carbon::singularUnit($big);
$name = $singularSmall.'Of'.ucfirst($singularBig);
$unitOfUnit[$name] ??= [
'@property',
'int',
'$'.$name,
'The value of the '.$singularSmall.' starting from the beginning of the current '.$singularBig,
];
$name = $small.'In'.ucfirst($singularBig);
$unitOfUnit[$name] ??= [
'@property',
'int',
'$'.$name,
'The number of '.$small.' contained in the current '.$singularBig,
];
}
}

ksort($unitOfUnit);

array_push($autoDocLines, ...array_values($unitOfUnit));
}
}

$fileTemplate = <<<EOF
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ parameters:
message: '#^Variable \$this in isset\(\) is never defined\.$#'
paths:
- src/Carbon/Traits/Mixin.php
-
message: '#^Access to an undefined property Carbon\\CarbonImmutable::\$[a-zA-Z]+\.$#'
paths:
- tests/CarbonImmutable/GettersTest.php
-
message: '#^Cannot access property \$locale on Carbon\\CarbonPeriod\|string\.$#'
paths:
Expand Down
163 changes: 156 additions & 7 deletions src/Carbon/Carbon.php

Large diffs are not rendered by default.

163 changes: 156 additions & 7 deletions src/Carbon/CarbonImmutable.php

Large diffs are not rendered by default.

165 changes: 157 additions & 8 deletions src/Carbon/CarbonInterface.php

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
* @method void setFallbackLocale(string $locale) Set the fallback locale.
* @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
* You should rather use the ->settings() method.
* @method bool setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists.
* Pass 'auto' as locale to use closest language from the current LC_TIME locale.
* @method void setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists.
* Pass 'auto' as locale to use the closest language to the current LC_TIME locale.
* @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
* You should rather consider mid-day is always 12pm, then if you need to test if it's an other
* hour, test it explicitly:
Expand Down Expand Up @@ -221,7 +221,7 @@
* You should rather use the ->settings() method.
* Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants
* are available for quarters, years, decade, centuries, millennia (singular and plural forms).
* @method mixed withTestNow($testNow, $callback) Temporarily sets a static date to be used within the callback.
* @method T withTestNow($testNow, $callback) Temporarily sets a static date to be used within the callback.
* Using setTestNow to set the date, executing the callback, then
* clearing the test instance.
* /!\ Use this method for unit tests only.
Expand Down
6 changes: 3 additions & 3 deletions src/Carbon/FactoryImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
* @method void setFallbackLocale(string $locale) Set the fallback locale.
* @method CarbonImmutable setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
* You should rather use the ->settings() method.
* @method bool setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists.
* Pass 'auto' as locale to use closest language from the current LC_TIME locale.
* @method void setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists.
* Pass 'auto' as locale to use the closest language to the current LC_TIME locale.
* @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
* You should rather consider mid-day is always 12pm, then if you need to test if it's an other
* hour, test it explicitly:
Expand Down Expand Up @@ -220,7 +220,7 @@
* You should rather use the ->settings() method.
* Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants
* are available for quarters, years, decade, centuries, millennia (singular and plural forms).
* @method mixed withTestNow($testNow, $callback) Temporarily sets a static date to be used within the callback.
* @method T withTestNow($testNow, $callback) Temporarily sets a static date to be used within the callback.
* Using setTestNow to set the date, executing the callback, then
* clearing the test instance.
* /!\ Use this method for unit tests only.
Expand Down
Loading

0 comments on commit 04faa67

Please sign in to comment.