Skip to content

Commit

Permalink
Merge pull request briannesbitt#2885 from briannesbitt/feature/generi…
Browse files Browse the repository at this point in the history
…c-unit-of-unit

Add generic unitOfUnit and unitsInUnit getters
  • Loading branch information
kylekatarnls authored Nov 19, 2023
2 parents e046a1f + 7e6a578 commit 1a71900
Show file tree
Hide file tree
Showing 11 changed files with 1,597 additions and 49 deletions.
96 changes: 96 additions & 0 deletions phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function dumpParameter(string $method, ReflectionParameter $parameter): string
continue;
}

$unitOfUnit = [];
preg_match_all('/\/\/ @'.$tag.'\s+(?<type>'.$pattern.')(?:\s+\$(?<name>\S+)(?:[^\S\n](?<description>.*))?\n|(?:[^\S\n](?<description2>.*))?\n(?<comments>(?:[ \t]+\/\/[^\n]*\n)*)[^\']*\'(?<name2>[^\']+)\')/', $code, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
Expand Down Expand Up @@ -518,15 +519,110 @@ function dumpParameter(string $method, ReflectionParameter $parameter): string
continue;
}

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

continue;
}

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

if ($tag === 'property') {
$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,
];
}
}

ksort($unitOfUnit);

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

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 = $small.'In'.ucfirst($singularBig);
$unitOfUnit[$name] ??= [
'@property-read',
'int',
'$'.$name,
'The number of '.$small.' contained in the current '.$singularBig,
];
}
}

ksort($unitOfUnit);

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

$units = ['microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years', 'decades', 'centuries', 'millennia'];
$unitOfUnit = [
'dayOfYear' => false,
'weeksInYear' => false,
];

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] ??= [
'@method',
'int|static',
$name.'(?int $'.$singularSmall.' = null)',
'Return the value of the '.$singularSmall.' starting from the beginning of the current '.$singularBig.' when called with no parameters, change the current '.$singularSmall.' when called with an integer value',
];
$name = $small.'In'.ucfirst($singularBig);
$unitOfUnit[$name] ??= [
'@method',
'int',
$name.'()',
'Return the number of '.$small.' contained in the current '.$singularBig,
];
}
}

ksort($unitOfUnit);

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

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

0 comments on commit 1a71900

Please sign in to comment.