Skip to content

Commit

Permalink
[dev] function-info command
Browse files Browse the repository at this point in the history
I'm trying to figure out why some functions claim to take a `resource` when they actually take a `FTP\Connection` or a `GdImage` -- this CLI command helps with debugging.

Example:
```
$ php ./generator/safe.php function-info ftp_alloc
Params:
  ftp
    ParameterType: FTP\Connection
    SignatureType:
    DocBlockType:  resource
  size
    ParameterType: int
    SignatureType: int
    DocBlockType:  int
  response
    ParameterType: string
    SignatureType: ?string
    DocBlockType:  string|null

/**
 * Sends an ALLO command to the remote FTP server to
 * allocate space for a file to be uploaded.
 *
 * @param resource $ftp An FTP\Connection instance.
 * @param int $size The number of bytes to allocate.
 * @param string|null $response A textual representation of the servers response will be returned by
 * reference in response if a variable is provided.
 * @throws FtpException
 *
 */
function ftp_alloc($ftp, int $size, ?string &$response = null): void
{
    error_clear_last();
    $safeResult = \ftp_alloc($ftp, $size, $response);
    if ($safeResult === false) {
        throw FtpException::createFromPhpError();
    }
}
```
  • Loading branch information
shish committed Jan 30, 2025
1 parent bd4b5e7 commit 5801da4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions generator/safe.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Safe\GenerateCommand;
use Safe\ScanObjectsCommand;
use Safe\DeprecateCommand;
use Safe\FunctionInfoCommand;
use Symfony\Component\Console\Application;

$application = new Application();
$application->addCommands([new GenerateCommand()]);
$application->addCommands([new ScanObjectsCommand()]);
$application->addCommands([new DeprecateCommand()]);
$application->addCommands([new FunctionInfoCommand()]);

$application->run();
2 changes: 1 addition & 1 deletion generator/src/DocPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function loadAndResolveFile(): \SimpleXMLElement
.'<!DOCTYPE refentry SYSTEM "'.$path.'">'
.\substr($content, $strpos+1);

echo 'Loading '.$this->path."\n";
// echo 'Loading '.$this->path."\n";
$elem = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_DTDLOAD | LIBXML_NOENT);
if ($elem === false) {
throw new \RuntimeException('Invalid XML file for '.$this->path);
Expand Down
46 changes: 46 additions & 0 deletions generator/src/FunctionInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Safe;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class FunctionInfoCommand extends Command
{
protected function configure(): void
{
$this
->setName('function-info')
->setDescription('Displays parsed info about a function.')
->addArgument('function', InputArgument::REQUIRED, 'The function name to display info about.')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$scanner = new Scanner(__DIR__ . '/../doc/doc-en/en/reference/');
$res = $scanner->getMethods($scanner->getFunctionsPaths(), $output);

foreach ($res->methods as $function) {
$name = $function->getFunctionName();
if ($name == $input->getArgument("function")) {
$output->writeln("Params: ");
foreach ($function->getParams() as $param) {
$output->writeln(" " . $param->getParameterName());
$output->writeln(" ParameterType: " . $param->getParameterType());
$output->writeln(" SignatureType: " . $param->getSignatureType());
$output->writeln(" DocBlockType: " . $param->getDocBlockType());
}
$writePhpFunction = new WritePhpFunction($function);
$output->writeln($writePhpFunction->getPhpFunctionalFunction());
break;
}
}

return 0;
}
}
2 changes: 1 addition & 1 deletion generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$paths = $scanner->getFunctionsPaths();

$res = $scanner->getMethods($paths);
$res = $scanner->getMethods($paths, $output);
$functions = $res->methods;
$overloadedFunctions = $res->overloadedFunctions;

Expand Down
2 changes: 1 addition & 1 deletion generator/src/ScanObjectsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$paths = $scanner->getMethodsPaths();

$res = $scanner->getMethods($paths);
$res = $scanner->getMethods($paths, $output);

foreach ($res->methods as $function) {
$name = $function->getFunctionName();
Expand Down
3 changes: 2 additions & 1 deletion generator/src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function iterator_to_array;
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Output\OutputInterface;
use SplFileInfo;

class Scanner
Expand Down Expand Up @@ -78,7 +79,7 @@ private function getIgnoredModules(): array
/**
* @param SplFileInfo[] $paths
*/
public function getMethods(array $paths): ScannerResponse
public function getMethods(array $paths, OutputInterface $output): ScannerResponse
{
/** @var Method[] $functions */
$functions = [];
Expand Down

0 comments on commit 5801da4

Please sign in to comment.