Skip to content

Commit df3cc77

Browse files
committed
refactor(composer): Update IDE JSON generation script
- Change the command for generating IDE JSON in composer.json to use a static method. - Implement the generateIdeJson method in ComposerScripts to create a structured ide.json file for IDE support. - The change improves maintainability and IDE integration for the project.
1 parent bffa848 commit df3cc77

File tree

6 files changed

+91
-109
lines changed

6 files changed

+91
-109
lines changed

.php-cs-fixer.php

-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@
248248
], true)
249249
),
250250
__DIR__.'/composer-updater',
251-
__DIR__.'/generate-ide-json',
252-
__DIR__.'/platform-lint',
253251
])
254252
)
255253
->setRiskyAllowed(true)

composer-dependency-analyser.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
->addPathsToExclude([
2525
__DIR__.'/tests',
2626
__DIR__.'/src/Foundation/Rectors',
27+
__DIR__.'/src/Foundation/Support/ComposerScripts.php',
2728
])
2829
->ignoreUnknownClasses([
2930
// SensitiveParameter::class,

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"symfony/var-dumper": "^6.0 || ^7.0",
116116
"symfony/yaml": "^6.0 || ^7.0",
117117
"symplify/phpstan-extensions": "^12.0",
118-
"symplify/phpstan-rules": "^14.4",
118+
"symplify/phpstan-rules": "^14.5",
119119
"tomasvotruba/class-leak": "^2.0",
120120
"tomasvotruba/type-coverage": "^2.0",
121121
"yamadashy/phpstan-friendly-formatter": "^1.2"
@@ -219,7 +219,7 @@
219219
"composer-updater": "@php ./composer-updater --highest-php-binary=/opt/homebrew/opt/[email protected]/bin/php --except-packages=pestphp/pest-plugin-laravel --ansi",
220220
"composer-updater-dry-run": "@composer-updater --dry-run",
221221
"composer-validate": "@composer validate --check-lock --strict --ansi -v",
222-
"generate-ide-json": "@php generate-ide-json",
222+
"generate-ide-json": "Guanguans\\Notify\\Foundation\\Support\\ComposerScripts::generateIdeJson",
223223
"infection": "@php ./vendor/bin/infection --configuration=infection.json --show-mutations --threads=8 --git-diff-base=origin/main --ansi -v",
224224
"infection-dirty": "@infection --git-diff-filter=AM",
225225
"json-lint": "@php ./vendor/bin/jsonlint *.json .*rc",

generate-ide-json

-99
This file was deleted.

rector.php

-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@
7171
__DIR__.'/tests',
7272
...glob(__DIR__.'/{*,.*}.php', \GLOB_BRACE),
7373
__DIR__.'/composer-updater',
74-
__DIR__.'/generate-ide-json',
75-
__DIR__.'/platform-lint',
7674
])
7775
->withRootFiles()
7876
// ->withSkipPath(__DIR__.'/tests.php')

src/Foundation/Support/ComposerScripts.php

+88-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace Guanguans\Notify\Foundation\Support;
1515

1616
use Composer\Script\Event;
17+
use Guanguans\Notify\Foundation\Message;
18+
use Illuminate\Support\Collection;
1719
use Rector\Config\RectorConfig;
1820
use Rector\DependencyInjection\LazyContainerFactory;
1921
use Symfony\Component\Finder\Finder;
@@ -27,7 +29,89 @@
2729
final class ComposerScripts
2830
{
2931
/**
32+
* @noinspection PhpDuplicatedCharacterInStrFunctionCallInspection
3033
* @noinspection PhpUnused
34+
*
35+
* @throws \JsonException
36+
* @throws \ReflectionException
37+
*
38+
* @return int<0, 0>
39+
*/
40+
public static function generateIdeJson(Event $event): int
41+
{
42+
self::requireAutoload($event);
43+
44+
collect(
45+
Finder::create()
46+
->in(__DIR__.'/../../../src')
47+
->exclude('Foundation')
48+
->path('/Messages/')
49+
->name([
50+
'Message.php',
51+
'*Message.php',
52+
])
53+
->sortByName()
54+
->files()
55+
)
56+
->mapWithKeys(static function (SplFileInfo $fileInfo): array {
57+
$class = \sprintf(
58+
'\\Guanguans\\Notify\\%s',
59+
str_replace('/', '\\', rtrim($fileInfo->getRelativePathname(), '.php'))
60+
);
61+
62+
return [$class => new \ReflectionClass($class)];
63+
})
64+
->filter(static fn (\ReflectionClass $reflectionClass): bool => $reflectionClass->isSubclassOf(Message::class))
65+
->map(static function (\ReflectionClass $reflectionClass, string $class): array {
66+
$defined = Utils::definedFor($class);
67+
68+
asort($defined);
69+
70+
return array_values($defined);
71+
})
72+
->map(static fn (array $defined, string $class): array => [
73+
'complete' => 'staticStrings',
74+
'condition' => [
75+
[
76+
'classFqn' => [
77+
$class,
78+
],
79+
'newClassFqn' => [
80+
$class,
81+
],
82+
'methodNames' => [
83+
'make',
84+
],
85+
'parameters' => [
86+
1,
87+
],
88+
'place' => 'arrayKey',
89+
],
90+
],
91+
'options' => [
92+
'strings' => $defined,
93+
],
94+
])
95+
->pipe(static fn (Collection $completions): Collection => collect([
96+
'$schema' => 'https://laravel-ide.com/schema/laravel-ide-v2.json',
97+
'completions' => $completions->values()->all(),
98+
]))
99+
->tap(static function (Collection $ide): void {
100+
file_put_contents(
101+
__DIR__.'/../../../ide.json',
102+
json_encode($ide, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)
103+
);
104+
});
105+
106+
$event->getIO()->write('<info>Generate ide.json successfully.</info>');
107+
108+
return 0;
109+
}
110+
111+
/**
112+
* @noinspection PhpUnused
113+
*
114+
* @return int<0, 0>
31115
*/
32116
public static function platformLint(Event $event): int
33117
{
@@ -91,27 +175,27 @@ public static function platformLint(Event $event): int
91175
$composerContents = file_get_contents(__DIR__.'/../../../composer.json');
92176

93177
if (!str_contains($composerContents, $platformsDescriptionContents)) {
94-
$event->getIO()->writeError("The description of composer.json must contain: \n```\n$platformsDescriptionContents\n```");
178+
$event->getIO()->writeError("<error>The description of composer.json must contain: \n```\n$platformsDescriptionContents\n```</error>");
95179

96180
exit(1);
97181
}
98182

99183
if (!str_contains($composerContents, $platformsKeywordContents)) {
100-
$event->getIO()->writeError("The keywords of composer.json must contain: \n```\n$platformsKeywordContents\n```");
184+
$event->getIO()->writeError("<error>The keywords of composer.json must contain: \n```\n$platformsKeywordContents\n```</error>");
101185

102186
exit(1);
103187
}
104188

105189
$readmeContents = file_get_contents(__DIR__.'/../../../README.md');
106190

107191
if (!str_contains($readmeContents, $platformsDescriptionContents)) {
108-
$event->getIO()->writeError("The description of README.md must contain: \n```\n$platformsDescriptionContents\n```");
192+
$event->getIO()->writeError("<error>The description of README.md must contain: \n```\n$platformsDescriptionContents\n```</error>");
109193

110194
exit(1);
111195
}
112196

113197
if (!str_contains($readmeContents, $platformsLinkContents)) {
114-
$event->getIO()->writeError("The links of README.md must contain: \n```\n$platformsLinkContents\n```");
198+
$event->getIO()->writeError("<error>The links of README.md must contain: \n```\n$platformsLinkContents\n```</error>");
115199

116200
exit(1);
117201
}

0 commit comments

Comments
 (0)