Skip to content

Commit

Permalink
[generator] don't duplicate special-cases list, fixes #523
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Feb 8, 2025
1 parent 6d35e7f commit b4640be
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
19 changes: 0 additions & 19 deletions generator/config/specialCasesFunctions.php

This file was deleted.

3 changes: 2 additions & 1 deletion generator/src/Generator/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Safe\Generator;

use Safe\XmlDocParser\Scanner;
use Safe\XmlDocParser\Method;

use function array_merge;
Expand Down Expand Up @@ -94,7 +95,7 @@ private function getFunctionsNameList(array $functions): array
$functionNames = array_map(function (Method $function) {
return $function->getFunctionName();
}, $functions);
$specialCases = require FileCreator::getSafeRootDir() . '/generator/config/specialCasesFunctions.php';
$specialCases = Scanner::getSpecialCases();
$functionNames = array_merge($functionNames, $specialCases);
natcasesort($functionNames);
return $functionNames;
Expand Down
16 changes: 6 additions & 10 deletions generator/src/XmlDocParser/DocPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function findReferenceDir(): string
return DocPage::findDocDir() . '/doc-en/en/reference';
}

// Ignore function if it was removed before PHP 7.1
// Ignore function if it was removed before PHP 8.1
private function getIsDeprecated(string $file): bool
{
if (preg_match('/&warn\.deprecated\.function-(\d+-\d+-\d+)\.removed-(\d+-\d+-\d+)/', $file, $matches)) {
Expand All @@ -48,25 +48,21 @@ private function getIsDeprecated(string $file): bool
}

public function getErrorType(): ErrorType
{
$returnValuesSection = $this->getReturnValues();
$detectErrorType = require FileCreator::getSafeRootDir() . '/generator/config/detectErrorType.php';
return $detectErrorType($returnValuesSection);
}

private function getReturnValues(): string
{
$file = file_get_contents($this->path);
if ($file === false) {
throw new \RuntimeException('An error occurred while reading '.$this->path);
}
if ($this->getIsDeprecated($file)) {
return "";
return ErrorType::UNKNOWN;
}

// Only evaluate the text inside the `<refsect1 role="returnvalues">...</refsect1>` section of the doc page.
// This minimizes 'false positives', where text such as "returns false when ..." could be matched outside
// the function's dedicated Return Values section.
return $this->extractSection('returnvalues', $file);
$returnDocs = $this->extractSection('returnvalues', $file);
$detectErrorType = require FileCreator::getSafeRootDir() . '/generator/config/detectErrorType.php';
return $detectErrorType($returnDocs);
}

/**
Expand Down
19 changes: 18 additions & 1 deletion generator/src/XmlDocParser/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function getIgnoredFunctions(): array
{
if ($this->ignoredFunctions === null) {
$ignoredFunctions = require FileCreator::getSafeRootDir() . '/generator/config/ignoredFunctions.php';
$specialCaseFunctions = require FileCreator::getSafeRootDir() . '/generator/config/specialCasesFunctions.php';
$specialCaseFunctions = $this->getSpecialCases();

$this->ignoredFunctions = array_merge($ignoredFunctions, $specialCaseFunctions);
}
Expand All @@ -78,6 +78,23 @@ private function getIgnoredModules(): array
return $this->ignoredModules;
}

/**
* Get a list of functions defined in special_cases.php so that we
* can ignore them in the main list.
*
* @return string[]
*/
public static function getSpecialCases(): array
{
$data = file_get_contents(FileCreator::getSafeRootDir() . '/lib/special_cases.php');
if ($data === false) {
throw new \RuntimeException('Unable to read special cases');
}
$matches = [];
preg_match_all('/function\s+([\w_]+)\(/', $data, $matches);
return $matches[1];
}

/**
* @param SplFileInfo[] $paths
*/
Expand Down

0 comments on commit b4640be

Please sign in to comment.