Skip to content

feat(chore): better display of unsafe methods in scan-objects command #699

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

Merged
merged 2 commits into from
May 16, 2025
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
34 changes: 23 additions & 11 deletions generator/src/Commands/ScanObjectsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace Safe\Commands;

use Safe\Domain\MethodDefinition;
use Safe\XmlDocParser\Method;
use Safe\XmlDocParser\Scanner;
use Safe\XmlDocParser\DocPage;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class ScanObjectsCommand extends Command
final class ScanObjectsCommand extends Command
{
private SymfonyStyle $io;
private Scanner $scanner;

protected function configure(): void
{
$this
Expand All @@ -20,20 +26,26 @@ protected function configure(): void
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$scanner = new Scanner(DocPage::referenceDir());
$this->io = new SymfonyStyle($input, $output);
$this->scanner = new Scanner(DocPage::referenceDir());
}

$paths = $scanner->getMethodsPaths();
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io->title($this->getDescription());

$res = $scanner->getMethods($paths, [], $output);
$result = $this->scanner->getMethods($this->scanner->getMethodsPaths(), [], $output);

foreach ($res->methods as $function) {
$name = $function->getFunctionName();
$output->writeln('Found method '.$name);
}
$this->io->table(
['Method', 'Error Type', 'Safe'],
\array_merge(
\array_map(static fn(Method $method) => [$method->getFunctionName(), $method->getErrorType()->name, '✅'], $result->methods),
\array_map(static fn(MethodDefinition $method) => [$method->name, $method->errorType->name, '❌'], $result->overloadedFunctions),
),
);

$output->writeln('These methods are overloaded: '.\implode(', ', $res->overloadedFunctions));
return 0;
return self::SUCCESS;
}
}
21 changes: 21 additions & 0 deletions generator/src/Domain/MethodDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Safe\Domain;

use Safe\XmlDocParser\ErrorType;

final class MethodDefinition implements \Stringable
{
public function __construct(
public readonly string $name,
public readonly ErrorType $errorType,
) {
}

public function __toString(): string
{
return $this->name;
}
}
13 changes: 6 additions & 7 deletions generator/src/XmlDocParser/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Safe\XmlDocParser;

use Safe\Domain\MethodDefinition;
use function array_merge;
use function iterator_to_array;
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
Expand Down Expand Up @@ -115,7 +116,7 @@ public function getMethods(array $paths, array $pastFunctionNames, OutputInterfa
{
/** @var Method[] $functions */
$functions = [];
/** @var string[] $overloadedFunctions */
/** @var MethodDefinition[] $overloadedFunctions */
$overloadedFunctions = [];

$phpStanFunctionMapReader = new PhpStanFunctionMapReader();
Expand Down Expand Up @@ -149,12 +150,9 @@ public function getMethods(array $paths, array $pastFunctionNames, OutputInterfa
if ($errorType !== ErrorType::UNKNOWN || $wasThisFunctionUnsafe) {
$functionObjects = $docPage->getMethodSynopsis();
if (count($functionObjects) > 1) {
$overloadedFunctions = array_merge($overloadedFunctions, \array_map(function ($functionObject) {
return $functionObject->methodname->__toString();
}, $functionObjects));
$overloadedFunctions = \array_filter($overloadedFunctions, function (string $functionName) use ($ignoredFunctions) {
return !isset($ignoredFunctions[$functionName]);
});
$overloadedFunctions = \array_merge($overloadedFunctions, \array_map(static fn(\SimpleXMLElement $functionObject) => new MethodDefinition((string) $functionObject->methodname, $errorType), $functionObjects));
$overloadedFunctions = \array_filter($overloadedFunctions, static fn(MethodDefinition $method) => !\array_key_exists($method->name, $ignoredFunctions));

continue;
}
$rootEntity = $docPage->loadAndResolveFile();
Expand All @@ -167,6 +165,7 @@ public function getMethods(array $paths, array $pastFunctionNames, OutputInterfa
}
}
}

$progressBar->finish();
$output->writeln("");

Expand Down
4 changes: 3 additions & 1 deletion generator/src/XmlDocParser/ScannerResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Safe\XmlDocParser;

use Safe\Domain\MethodDefinition;

class ScannerResponse
{
/**
* @param Method[] $methods
* @param string[] $overloadedFunctions
* @param MethodDefinition[] $overloadedFunctions
*/
public function __construct(
public readonly array $methods,
Expand Down