-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from xepozz/stubs-generator
Add stubs generator
- Loading branch information
Showing
10 changed files
with
23,203 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
generate-stubs: | ||
cd generator/stub; make generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stubs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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] ?? []), | ||
]; | ||
} | ||
} | ||
|
||
$patches = require './patches.php'; | ||
|
||
$result = array_merge($stubs, $patches); | ||
|
||
file_put_contents( | ||
$destination, | ||
'<?php' . PHP_EOL . PHP_EOL . 'return ' . var_export($result, true) . ';' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Example: | ||
* 'header_remove' => [ | ||
* 'signatureArguments' => '?string $name = null', | ||
* 'arguments' => '$name', | ||
* ], | ||
*/ | ||
return [ | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.