Skip to content

Commit ef512fb

Browse files
committed
[dev] function-info command
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(); } } ```
1 parent bd4b5e7 commit ef512fb

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

generator/safe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Safe\GenerateCommand;
88
use Safe\ScanObjectsCommand;
99
use Safe\DeprecateCommand;
10+
use Safe\FunctionInfoCommand;
1011
use Symfony\Component\Console\Application;
1112

1213
$application = new Application();
1314
$application->addCommands([new GenerateCommand()]);
1415
$application->addCommands([new ScanObjectsCommand()]);
1516
$application->addCommands([new DeprecateCommand()]);
17+
$application->addCommands([new FunctionInfoCommand()]);
1618

1719
$application->run();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Safe;
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class FunctionInfoCommand extends Command
13+
{
14+
protected function configure(): void
15+
{
16+
$this
17+
->setName('function-info')
18+
->setDescription('Displays parsed info about a function.')
19+
->addArgument('function', InputArgument::REQUIRED, 'The function name to display info about.')
20+
;
21+
}
22+
23+
protected function execute(InputInterface $input, OutputInterface $output): int
24+
{
25+
$scanner = new Scanner(__DIR__ . '/../doc/doc-en/en/reference/');
26+
$res = $scanner->getMethods($scanner->getFunctionsPaths(), $output);
27+
28+
foreach ($res->methods as $function) {
29+
$name = $function->getFunctionName();
30+
if ($name == $input->getArgument("function")) {
31+
$output->writeln("Params: ");
32+
foreach ($function->getParams() as $param) {
33+
$output->writeln(" " . $param->getParameterName());
34+
$output->writeln(" ParameterType: " . $param->getParameterType());
35+
$output->writeln(" SignatureType: " . $param->getSignatureType());
36+
$output->writeln(" DocBlockType: " . $param->getDocBlockType());
37+
}
38+
$writePhpFunction = new WritePhpFunction($function);
39+
$output->writeln($writePhpFunction->getPhpFunctionalFunction());
40+
break;
41+
}
42+
}
43+
44+
return 0;
45+
}
46+
}

generator/src/GenerateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2929

3030
$paths = $scanner->getFunctionsPaths();
3131

32-
$res = $scanner->getMethods($paths);
32+
$res = $scanner->getMethods($paths, $output);
3333
$functions = $res->methods;
3434
$overloadedFunctions = $res->overloadedFunctions;
3535

generator/src/ScanObjectsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2424

2525
$paths = $scanner->getMethodsPaths();
2626

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

2929
foreach ($res->methods as $function) {
3030
$name = $function->getFunctionName();

generator/src/Scanner.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use function iterator_to_array;
99
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
1010
use Symfony\Component\Finder\Finder;
11+
use Symfony\Component\Console\Output\OutputInterface;
1112
use SplFileInfo;
1213

1314
class Scanner
@@ -78,7 +79,7 @@ private function getIgnoredModules(): array
7879
/**
7980
* @param SplFileInfo[] $paths
8081
*/
81-
public function getMethods(array $paths): ScannerResponse
82+
public function getMethods(array $paths, OutputInterface $output): ScannerResponse
8283
{
8384
/** @var Method[] $functions */
8485
$functions = [];

0 commit comments

Comments
 (0)