Skip to content

Commit

Permalink
Fix enum method generation issues with docblocks (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinsFrank authored Jul 3, 2024
1 parent 90fe9c2 commit 02ee8d9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dev/DataSource/Mapping/CountryMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function toEnumMapping(array $dataSet): array
{
$countryName = new EnumFile(CountryName::class, KeySorting::class);
$countryAlpha2 = (new EnumFile(CountryAlpha2::class, KeySorting::class))
->addMethod($getSubdivisionsMethod = new EnumMethod('getSubdivisions', 'array', '[]', null));
->addMethod($getSubdivisionsMethod = new EnumMethod('getSubdivisions', 'array', '[]', '/** @return list<CountrySubdivision> */'));
$countryAlpha3 = new EnumFile(CountryAlpha3::class, KeySorting::class);
$countryNumeric = new EnumFile(CountryNumeric::class, KeySorting::class);
$countrySubdivision = (new EnumFile(CountrySubdivision::class, KeySorting::class));
Expand Down
2 changes: 1 addition & 1 deletion dev/DataSource/Mapping/CurrencyMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function toEnumMapping(array $dataSet): array
->addMethod($getCountriesAlpha2Method = new EnumMethod('getCountriesAlpha2', 'array', '[]', '/** @return list<CountryAlpha2> */'));

$countryAlpha2 = (new EnumFile(CountryAlpha2::class, KeySorting::class))
->addMethod($getCurrenciesMethod = new EnumMethod('getCurrenciesAlpha3', 'array', '[]'));
->addMethod($getCurrenciesMethod = new EnumMethod('getCurrenciesAlpha3', 'array', '[]', '/** @return list<CurrencyAlpha3> */'));
foreach ($dataSet as $dataRow) {
if (($dataRow->Ccy ?? null) === null) {
continue;
Expand Down
26 changes: 18 additions & 8 deletions dev/DataTarget/EnumFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,35 @@ public function writeMethods(): void
{
foreach ($this->methods as $method) {
$enumContent = $this->getContent();
$startExistingMethod = mb_strpos($enumContent, ' public function ' . $method->name . '()');
$nextMethodPos = mb_strpos($enumContent, PHP_EOL . ' public function', $startExistingMethod === false ? 0 : $startExistingMethod + 1);
$startExistingMethod = $this->getMethodPos($method, $enumContent);
$nextMethodPos = $this->getMethodPos(null, $enumContent, $startExistingMethod !== null ? $startExistingMethod + 4 + strlen($method->name . '()' . $method->returnType) + ($method->docBlock !== null ? strlen($method->docBlock) + 4 : 0) : 0);
$lastClosingTagPos = false;
if ($nextMethodPos === false) {
if ($nextMethodPos === null) {
$lastClosingTagPos = mb_strrpos($enumContent, PHP_EOL . '}');
}

if ($nextMethodPos === false && $lastClosingTagPos === false) {
if ($nextMethodPos === null && $lastClosingTagPos === false) {
throw new RuntimeException('Couldn\'t locate closing tag');
}

$endPosMethodOrLastClosingTag = ($nextMethodPos !== false ? $nextMethodPos : $lastClosingTagPos);
if ($startExistingMethod !== false) {
$newEnumContent = mb_substr($enumContent, 0, $startExistingMethod) . $method->__toString() . ($nextMethodPos !== false ? PHP_EOL : '') . mb_substr($enumContent, $endPosMethodOrLastClosingTag);
$endPosMethodOrLastClosingTag = ($nextMethodPos !== null ? $nextMethodPos : $lastClosingTagPos);
if ($startExistingMethod !== null) {
$newEnumContent = mb_substr($enumContent, 0, $startExistingMethod) . $method->__toString() . ($nextMethodPos !== null ? PHP_EOL : '') . mb_substr($enumContent, $endPosMethodOrLastClosingTag);
} else {
$newEnumContent = mb_substr($enumContent, 0, $endPosMethodOrLastClosingTag) . $method->__toString() . ($nextMethodPos !== false ? PHP_EOL : '') . mb_substr($enumContent, $endPosMethodOrLastClosingTag);
$newEnumContent = mb_substr($enumContent, 0, $endPosMethodOrLastClosingTag) . $method->__toString() . ($nextMethodPos !== null ? PHP_EOL : '') . mb_substr($enumContent, $endPosMethodOrLastClosingTag);
}

$this->putContent($newEnumContent);
}
}

private function getMethodPos(?EnumMethod $method, string $enumContent, int $offset = 0): ?int
{
$matched = preg_match('/(\n\h*\/\*\*.*\n?(\h*\*.*\n?)*\h*\*\/){0,1}\n\h*(public|private|protected)?\h*(static)?\h*function\h*' . ($method->name ?? '') . '/u', $enumContent, $matches, PREG_OFFSET_CAPTURE, strlen(mb_substr($enumContent, 0, $offset))); // offset does not have multibyte support
if ($matched !== 1 || array_key_exists(0, $matches) === false || array_key_exists(1, $matches[0]) === false) {
return null;
}

return mb_strlen(substr($enumContent, 0, (int) $matches[0][1])); // PREG_OFFSET_CAPTURE returns the number of bytes, and doesn't have multibyte support
}
}
2 changes: 1 addition & 1 deletion dev/DataTarget/EnumMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static function (?bool $carry, array $item) {
$mappingString .= PHP_EOL . $indentingCase . 'default => ' . $this->default;
}

return ($this->docBlock !== null ? (' ' . $this->docBlock . PHP_EOL) : '') . <<<EOD
return PHP_EOL . ($this->docBlock !== null ? (' ' . $this->docBlock . PHP_EOL) : '') . <<<EOD
public function {$this->name}(): {$this->returnType}
{
return match(\$this) {{$mappingString}
Expand Down

0 comments on commit 02ee8d9

Please sign in to comment.