Skip to content

Commit f035129

Browse files
committed
[+]: allow to use different stubs
1 parent f35b22c commit f035129

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ This is a experiment! Lets check and fix the php documentation automatically.
55

66
```bash
77
git clone https://github.com/php/doc-en.git
8+
git clone https://github.com/php/php-src.git // optional: by default we use the PhpStorm Stubs
89
git clone https://github.com/voku/php-doc-fixer.git
910
cd php-doc-fixer/
1011
composer update --prefer-dist
11-
php bin/phpdocfixer run [--auto-fix="true"] [--remove-array-value-info="true"] ../doc-en/reference
12+
php bin/phpdocfixer run [--auto-fix="true"] [--remove-array-value-info="true"] [--stubs-path="../php-src/"] [--stubs-file-extension=".stub.php"] ../doc-en/reference/
1213
```

src/voku/PhpDocFixer/CliCommand/PhpDocFixerCommand.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public function configure(): void
4646
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
4747
'Automatically convert e.g. int[] into array. (false or true)',
4848
'false'
49+
)->addOption(
50+
'stubs-path',
51+
null,
52+
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
53+
'Overwrite the source of the stubs, by default we use PhpStorm Stubs via composer.',
54+
''
55+
)->addOption(
56+
'stubs-file-extension',
57+
null,
58+
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
59+
'Overwrite the default file extension for stubs.',
60+
'.php'
4961
);
5062
}
5163

@@ -58,6 +70,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
5870

5971
$autoFix = $input->getOption('auto-fix') !== 'false';
6072
$removeArrayValueInfo = $input->getOption('remove-array-value-info') !== 'false';
73+
$stubsPath = $input->getOption('stubs-path');
74+
$stubsFileExtension = $input->getOption('stubs-file-extension');
6175

6276
if (!$realPath || !\file_exists($realPath)) {
6377
$output->writeln('-------------------------------');
@@ -70,38 +84,41 @@ public function execute(InputInterface $input, OutputInterface $output): int
7084
$xmlReader = new \voku\PhpDocFixer\XmlDocs\XmlReader($realPath);
7185
$xmlDocInfo = $xmlReader->parse();
7286

73-
$phpStormStubsPath = __DIR__ . '/../../../../vendor/jetbrains/phpstorm-stubs/';
74-
$phpTypesFromPhpStormStubs = new \voku\PhpDocFixer\PhpStormStubs\PhpStormStubsReader(
75-
$phpStormStubsPath,
76-
$removeArrayValueInfo
87+
if (!$stubsPath) {
88+
$stubsPath = __DIR__ . '/../../../../vendor/jetbrains/phpstorm-stubs/';
89+
}
90+
$phpTypesFromStubs = new \voku\PhpDocFixer\PhpStubs\PhpStubsReader(
91+
$stubsPath,
92+
$removeArrayValueInfo,
93+
$stubsFileExtension
7794
);
78-
$phpStormStubsInfo = $phpTypesFromPhpStormStubs->parse();
95+
$stubsInfo = $phpTypesFromStubs->parse();
7996

8097
$errors = [];
8198
foreach ($xmlDocInfo as $functionName_or_classAndMethodName => $types) {
82-
if (!isset($phpStormStubsInfo[$functionName_or_classAndMethodName])) {
83-
// TODO: error in phpstorm-stubs ?
99+
if (!isset($stubsInfo[$functionName_or_classAndMethodName])) {
100+
// TODO: error in stubs?
84101
//\var_dump($functionName_or_classAndMethodName); exit;
85102
continue;
86103
}
87104

88105
if (
89-
($phpStormStubsInfo[$functionName_or_classAndMethodName]['return'] ?? []) !== ($types['return'] ?? [])
106+
($stubsInfo[$functionName_or_classAndMethodName]['return'] ?? []) !== ($types['return'] ?? [])
90107
||
91-
($phpStormStubsInfo[$functionName_or_classAndMethodName]['params'] ?? []) !== ($types['params'] ?? [])
108+
($stubsInfo[$functionName_or_classAndMethodName]['params'] ?? []) !== ($types['params'] ?? [])
92109
) {
93110
$pathTmp = $types['absoluteFilePath'];
94111
unset($types['absoluteFilePath']);
95112

96113
$errors[$functionName_or_classAndMethodName] = [
97-
'phpStubTypes' => $phpStormStubsInfo[$functionName_or_classAndMethodName],
114+
'phpStubTypes' => $stubsInfo[$functionName_or_classAndMethodName],
98115
'phpDocTypes' => $types,
99116
'path' => $pathTmp,
100117
];
101118

102119
if ($autoFix) {
103120
$xmlFixer = new \voku\PhpDocFixer\XmlDocs\XmlWriter($pathTmp);
104-
$xmlFixer->fix($phpStormStubsInfo[$functionName_or_classAndMethodName]);
121+
$xmlFixer->fix($stubsInfo[$functionName_or_classAndMethodName]);
105122
}
106123
}
107124
}

src/voku/PhpDocFixer/PhpStormStubs/PhpStormStubsReader.php renamed to src/voku/PhpDocFixer/PhpStubs/PhpStubsReader.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
declare(strict_types=1);
44

5-
namespace voku\PhpDocFixer\PhpStormStubs;
5+
namespace voku\PhpDocFixer\PhpStubs;
66

7-
final class PhpStormStubsReader
7+
final class PhpStubsReader
88
{
99
private string $path;
1010

1111
private bool $removeArrayValueInfo;
1212

13-
public function __construct(string $path, bool $removeArrayValueInfo = false)
13+
private string $stubsFileExtension;
14+
15+
public function __construct(
16+
string $path,
17+
bool $removeArrayValueInfo = false,
18+
string $stubsFileExtension = '.php'
19+
)
1420
{
1521
$this->path = $path;
1622
$this->removeArrayValueInfo = $removeArrayValueInfo;
23+
$this->stubsFileExtension = $stubsFileExtension;
1724
}
1825

1926
/**
@@ -23,7 +30,12 @@ public function __construct(string $path, bool $removeArrayValueInfo = false)
2330
*/
2431
public function parse(): array
2532
{
26-
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles($this->path);
33+
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(
34+
$this->path,
35+
[],
36+
[],
37+
[$this->stubsFileExtension]
38+
);
2739

2840
$return = [];
2941
$functionInfo = $phpCode->getFunctionsInfo();

src/voku/PhpDocFixer/XmlDocs/XmlWriter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ private function replaceTypes(
104104
$returnUnionNewCount = \count($returnUnionNew);
105105

106106
if ($returnUnionNewCount === 0) {
107-
// TODO: error in phpstorm-stubs ?
107+
// TODO: error in stubs?
108108
} else {
109109
$returnUnionTypeFound = $xmlParser->findOneOrFalse('type.union') !== false;
110110

111111
if ($returnUnionTypeFound && $returnUnionNewCount === 1) {
112-
// TODO: error in phpstorm-stubs ?
112+
// TODO: error in stubs?
113113
} else {
114114
if ($returnUnionTypeFound) {
115115
$xml = (string) \preg_replace('#<type class="union">.*</type><methodname>#Usi', '###NEW_TYPE###<methodname>', $xml);
@@ -143,14 +143,14 @@ private function replaceTypes(
143143
$paramTypesNewCount = \count($paramTypesNew);
144144

145145
if ($paramTypesNewCount === 0) {
146-
// TODO: error in phpstorm-stubs ?
146+
// TODO: error in stubs?
147147
continue;
148148
}
149149

150150
$paramUnionTypeFound = $param->findOneOrFalse('type.union') !== false;
151151

152152
if ($paramUnionTypeFound && $paramTypesNewCount === 1) {
153-
// TODO: error in phpstorm-stubs ?
153+
// TODO: error in stubs?
154154
continue;
155155
}
156156

tests/CheckerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
final class CheckerTest extends \PHPUnit\Framework\TestCase
1111
{
12-
public static function testPhpStormStubsReader(): void
12+
public static function testPhpStubsReader(): void
1313
{
14-
$phpStormStubsPath = __DIR__ . '/../vendor/jetbrains/phpstorm-stubs/mbstring/';
15-
$phpTypesFromPhpStormStubs = new \voku\PhpDocFixer\PhpStormStubs\PhpStormStubsReader($phpStormStubsPath);
16-
$phpStormStubsInfo = $phpTypesFromPhpStormStubs->parse();
14+
$PhpStubsPath = __DIR__ . '/../vendor/jetbrains/phpstorm-stubs/mbstring/';
15+
$phpTypesFromPhpStubs = new \voku\PhpDocFixer\PhpStubs\PhpStubsReader($PhpStubsPath);
16+
$PhpStubsInfo = $phpTypesFromPhpStubs->parse();
1717

1818
$expected = [
1919
'return' => 'int|false',
@@ -25,7 +25,7 @@ public static function testPhpStormStubsReader(): void
2525
],
2626
];
2727

28-
static::assertSame($expected, $phpStormStubsInfo['mb_strpos']);
28+
static::assertSame($expected, $PhpStubsInfo['mb_strpos']);
2929
}
3030

3131
public static function testPhpDocXmlReader(): void

0 commit comments

Comments
 (0)