Skip to content

Commit 3b2de1e

Browse files
Switched from GoaopParserReflection to BetterReflection
Raised php version to 8.2 Added option to include code for resolving but ignored in stub generation
1 parent e03b0cf commit 3b2de1e

31 files changed

+2962
-2395
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The main purpose for this tool is to generate stub-files from php classes to hav
55
completion for your IDE when encrypting your library with e.g.
66
[the ioncube encoder](http://www.ioncube.com/php_encoder.php).
77

8-
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%207.3-8892BF.svg)](https://php.net/)
8+
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%208.2-8892BF.svg)](https://php.net/)
99
[![License](https://img.shields.io/packagist/l/setasign/php-stub-generator.svg)](https://packagist.org/packages/setasign/php-stub-generator)
1010

1111
## Installation
@@ -75,12 +75,19 @@ class PhpStubGenerator
7575
* @var bool
7676
*/
7777
public static $addClassConstantsVisibility = false;
78+
79+
/**
80+
* If false the interface \Stringable won't be filtered out (the generated stubs require PHP >= 8.0).
81+
*
82+
* Within the cli tool can be set with the option "--includeStringable"
83+
*
84+
* @var bool
85+
*/
86+
public static bool $includeStringable = false;
7887
}
7988
```
8089

81-
## Drawbacks / TODOs
82-
- Traits are not supported yet and probably won't be because of bugs like [this](https://bugs.php.net/bug.php?id=69180).
83-
The actual reflection api doesn't give enough information to rebuild the conflict resolution block.
84-
Additionally the "declaring class" of imported trait methods is the importing class and not like expected the trait.
90+
## Drawbacks
8591
- Calculated constants or constants that use other constants like \_\_DIR\_\_ will be filled with the values of the
8692
runtime environment.
93+
- Global Functions and Constants are currently ignored

bin/php-stub-generator

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,23 @@ declare(strict_types=1);
55

66
use setasign\PhpStubGenerator\PhpStubGenerator;
77
use setasign\PhpStubGenerator\Reader\AllFiles;
8+
use setasign\PhpStubGenerator\Reader\SingleFile;
89
use Symfony\Component\Console\Application;
910
use Symfony\Component\Console\Input\InputArgument;
1011
use Symfony\Component\Console\Input\InputInterface;
1112
use Symfony\Component\Console\Input\InputOption;
1213
use Symfony\Component\Console\Output\OutputInterface;
1314

14-
if (version_compare('7.2', PHP_VERSION, '>=')) {
15-
fwrite(
16-
STDERR,
17-
sprintf(
18-
'This version of php-stub-generator is supported on PHP 7.2.' . PHP_EOL .
19-
'You are using PHP %s (%s).' . PHP_EOL,
20-
PHP_VERSION,
21-
PHP_BINARY
22-
)
23-
);
24-
25-
die(1);
26-
}
27-
2815
if (!ini_get('date.timezone')) {
2916
ini_set('date.timezone', 'UTC');
3017
}
3118

32-
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
33-
if (file_exists($file)) {
34-
define('PHP_STUB_GENERATOR_COMPOSER_INSTALL', $file);
35-
break;
19+
if (!defined('PHP_STUB_GENERATOR_COMPOSER_INSTALL')) {
20+
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
21+
if (file_exists($file)) {
22+
define('PHP_STUB_GENERATOR_COMPOSER_INSTALL', $file);
23+
break;
24+
}
3625
}
3726
}
3827

@@ -47,39 +36,51 @@ if (!defined('PHP_STUB_GENERATOR_COMPOSER_INSTALL')) {
4736
die(1);
4837
}
4938

50-
/** @noinspection PhpIncludeInspection */
5139
require PHP_STUB_GENERATOR_COMPOSER_INSTALL;
5240

53-
/** @noinspection PhpUnhandledExceptionInspection */
54-
(new Application('setasign php-stub-generator', 'v1.0.0-dev'))
41+
(new Application('setasign php-stub-generator', 'v2.0.0'))
5542
->register('generate')
5643
->setDescription('Build the stub-file')
5744
->addArgument(
5845
'source',
5946
InputArgument::REQUIRED,
6047
'The root directory of your library'
6148
)
62-
->addArgument(
63-
'output',
64-
InputArgument::REQUIRED,
65-
'The output file'
66-
)
6749
->addOption(
6850
'exclude',
6951
null,
7052
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
7153
'Exclude any directories'
7254
)
55+
->addOption(
56+
'resolvingSource',
57+
null,
58+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
59+
'Additional directory to resolve dependencies but without generating own stubs'
60+
)
61+
->addArgument(
62+
'output',
63+
InputArgument::REQUIRED,
64+
'The output file'
65+
)
7366
->addOption(
7467
'addClassConstantsVisibility',
75-
false,
68+
null,
7669
InputOption::VALUE_NONE,
7770
'If enabled all generated class constants get a visibility (the generated stubs require PHP >= 7.1)'
7871
)
72+
->addOption(
73+
'includeStringable',
74+
null,
75+
InputOption::VALUE_NONE,
76+
'If enabled the interface \Stringable won\'t be filtered out (the generated stubs require PHP >= 8.0)'
77+
)
7978
->setCode(function (
8079
InputInterface $input,
8180
OutputInterface $output
8281
) {
82+
gc_disable();
83+
// $start = microtime(true);
8384
$sourceDirectory = $input->getArgument('source');
8485
$outputPath = $input->getArgument('output');
8586
$excludes = $input->getOption('exclude');
@@ -94,13 +95,39 @@ require PHP_STUB_GENERATOR_COMPOSER_INSTALL;
9495
}
9596

9697
$generator = new PhpStubGenerator();
97-
$generator->addSource(
98-
'setapdf-core',
99-
new AllFiles($sourceDirectory, $excludes)
100-
);
98+
if (is_file($sourceDirectory)) {
99+
$generator->addSource(
100+
'source',
101+
new SingleFile($sourceDirectory)
102+
);
103+
} else {
104+
$generator->addSource(
105+
'source',
106+
new AllFiles($sourceDirectory, $excludes)
107+
);
108+
}
109+
110+
$additionalSources = $input->getOption('resolvingSource');
111+
if (is_array($additionalSources)) {
112+
foreach ($additionalSources as $k => $source) {
113+
if (is_file($source)) {
114+
$generator->addResolvingSource(
115+
'rs' . $k,
116+
new SingleFile($source)
117+
);
118+
} else {
119+
$generator->addResolvingSource(
120+
'rs' . $k,
121+
new AllFiles($source)
122+
);
123+
}
124+
}
125+
}
126+
101127
$stubs = $generator->generate();
102128
file_put_contents($outputPath, $stubs, LOCK_EX);
103129
$output->write('The stubs were successfully generated to: ' . realpath($outputPath) . PHP_EOL);
130+
// $output->write('Time: ' . microtime(true) - $start . PHP_EOL);
104131
})
105132
->getApplication()
106133
->run();

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
],
1212

1313
"require": {
14-
"php": "^7.4 || ^8.0",
14+
"php": "^8.2",
1515
"ext-pcre": "*",
1616
"ext-mbstring": "*",
1717

18-
"goaop/parser-reflection": "^2.1",
19-
"symfony/console": "^4.0"
18+
"roave/better-reflection": "^6.42",
19+
"symfony/console": "^6.0"
2020
},
2121

2222
"require-dev": {
23-
"bamarni/composer-bin-plugin": "^1.8"
23+
"bamarni/composer-bin-plugin": "^1.8",
24+
25+
"phpunit/phpunit": "^11.0",
26+
"php-defer/php-defer": "^5.0"
2427
},
2528

2629
"autoload": {

0 commit comments

Comments
 (0)