Skip to content

Commit

Permalink
Add stubs generator add generate all stubs from https://github.com/Je…
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 27, 2024
1 parent e128925 commit 5b333b0
Show file tree
Hide file tree
Showing 8 changed files with 23,158 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generate-stubs:
cd generator/stub; make generate
1 change: 1 addition & 0 deletions generator/stub/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stubs/
8 changes: 8 additions & 0 deletions generator/stub/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
clone:
git clone https://github.com/JetBrains/phpstorm-stubs stubs
clean:
rm -rf stubs
generator:
php generator.php

generate: clean clone generator
52 changes: 52 additions & 0 deletions generator/stub/generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

$folder = './stubs';
$destination = dirname(__DIR__, 2) . '/src/stubs.php';

$phpFiles = new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folder)
),
'/.*\.php$/',
RegexIterator::GET_MATCH
);

$removeAttributesList = implode('|', [
'LanguageLevelTypeAware',
'LanguageAware',
'ElementAvailable',
'PhpStormStubsElementAvailable',
'PhpVersionAware',
'TypeContract',
'ExpectedValues',
'\\\\SensitiveParameter',
]);

$stubs = [];
foreach ($phpFiles as $file) {
$path = $file[0];
$contents = file_get_contents($path);
if (!preg_match_all('/^function (\w+)\(.*\)/m', $contents, $matches, PREG_SET_ORDER)) {
continue;
}

foreach ($matches as $match) {
$functionLine = preg_replace('/#\[(?:' . $removeAttributesList . ')(?:\(.+\)|)] /', '', $match[0]);
//$functionLine = 'function headers_sent(string &$filename, int &$line): bool';
preg_match_all('/(\$\w+)/', $functionLine, $arguments,);
preg_match('/\((.+)\)/', $functionLine, $signatureArguments);

$stubs[$match[1]] = [
'signatureArguments' => $signatureArguments[1] ?? '',
'arguments' => implode(', ', $arguments[0] ?? []),
];
}
}


file_put_contents(
$destination,
'<?php' . PHP_EOL . PHP_EOL . 'return ' . var_export($stubs, true) . ';'
);
7 changes: 4 additions & 3 deletions src/Mocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public function generate(array $mocks): string
}
}
}
$stubs = require __DIR__ . '/stubs.php';

$outputs = [];
$mockerConfigClassName = MockerState::class;
foreach ($mocks as $namespace => $functions) {
$innerOutputsString = $this->generateFunction($functions);
$innerOutputsString = $this->generateFunction($functions, $stubs);

$outputs[] = <<<PHP
namespace {$namespace} {
Expand Down Expand Up @@ -101,9 +103,8 @@ private function normalizeMocks(array $mocks): array
return $result;
}

private function generateFunction(mixed $groupedMocks): string
private function generateFunction(array $groupedMocks, array $stubs): string
{
$stubs = require __DIR__ . '/stubs.php';
$innerOutputs = [];
foreach ($groupedMocks as $functionName => $_) {
$signatureArguments = $stubs[$functionName]['signatureArguments'] ?? '...$arguments';
Expand Down
Loading

0 comments on commit 5b333b0

Please sign in to comment.