Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic unitOfUnit and unitsInUnit getters #2885

Merged
merged 5 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading