Skip to content

Commit 1d44d96

Browse files
authored
Add Rector (#270)
* Add Rector Signed-off-by: Pushpak Chhajed <[email protected]> * Fix Tests Signed-off-by: Pushpak Chhajed <[email protected]> --------- Signed-off-by: Pushpak Chhajed <[email protected]>
1 parent 1171c69 commit 1d44d96

File tree

69 files changed

+567
-591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+567
-591
lines changed

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"mockery/mockery": "^1.6.12",
2929
"orchestra/testbench": "^8.36.0|^9.15.0|^10.6",
3030
"pestphp/pest": "^2.36.0|^3.8.4",
31-
"phpstan/phpstan": "^2.1.27"
31+
"phpstan/phpstan": "^2.1.27",
32+
"rector/rector": "^2.1"
3233
},
3334
"autoload": {
3435
"psr-4": {
@@ -52,13 +53,15 @@
5253
},
5354
"scripts": {
5455
"lint": [
55-
"vendor/bin/pint",
56-
"vendor/bin/phpstan --memory-limit=-1"
56+
"pint",
57+
"phpstan --memory-limit=-1",
58+
"rector"
5759
],
58-
"test": [
59-
"vendor/bin/pest"
60+
"test": "pest",
61+
"test:lint": [
62+
"pint --test",
63+
"rector --dry-run"
6064
],
61-
"test:lint": "pint --test",
6265
"test:types": "phpstan",
6366
"check": [
6467
"@composer lint",

phpunit.xml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<phpunit colors="true">
33
<testsuites>
44
<testsuite name="Unit">
5-
<directory suffix="Test.php">./tests/Unit</directory>
5+
<directory suffix="Test.php">tests/Unit</directory>
66
</testsuite>
77
<testsuite name="Feature">
8-
<directory suffix="Test.php">./tests/Feature</directory>
8+
<directory suffix="Test.php">tests/Feature</directory>
99
</testsuite>
1010
<testsuite name="Arch">
11-
<file>./tests/ArchTest.php</file>
11+
<file>tests/ArchTest.php</file>
1212
</testsuite>
1313
</testsuites>
1414
<php>

rector.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
8+
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
9+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
10+
11+
return RectorConfig::configure()
12+
->withPaths([
13+
__DIR__.'/src',
14+
__DIR__.'/tests',
15+
])
16+
->withSkip([
17+
ReadOnlyPropertyRector::class,
18+
EncapsedStringsToSprintfRector::class,
19+
DisallowedEmptyRuleFixerRector::class,
20+
BooleanInBooleanNotRuleFixerRector::class,
21+
])
22+
->withPreparedSets(
23+
deadCode: true,
24+
codeQuality: true,
25+
codingStyle: true,
26+
typeDeclarations: true,
27+
earlyReturn: true,
28+
strictBooleans: true,
29+
)->withPhpSets(php81: true);

src/BoostServiceProvider.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function register(): void
4040
];
4141

4242
$cacheKey = 'boost.roster.scan';
43-
$lastModified = max(array_map(fn ($path) => file_exists($path) ? filemtime($path) : 0, $lockFiles));
43+
$lastModified = max(array_map(fn (string $path): int|false => file_exists($path) ? filemtime($path) : 0, $lockFiles));
4444

4545
$cached = cache()->get($cacheKey);
4646
if ($cached && isset($cached['timestamp']) && $cached['timestamp'] >= $lastModified) {
@@ -113,7 +113,12 @@ private function registerRoutes(): void
113113
* } $log */
114114
foreach ($logs as $log) {
115115
$logger->write(
116-
level: self::mapJsTypeToPsr3Level($log['type']),
116+
level: match ($log['type']) {
117+
'warn' => 'warning',
118+
'log', 'table' => 'debug',
119+
'window_error', 'uncaught_error', 'unhandled_rejection' => 'error',
120+
default => $log['type']
121+
},
117122
message: self::buildLogMessageFromData($log['data']),
118123
context: [
119124
'url' => $log['url'],
@@ -165,22 +170,12 @@ private function registerBrowserLogger(): void
165170

166171
private function registerBladeDirectives(BladeCompiler $bladeCompiler): void
167172
{
168-
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
169-
}
170-
171-
private static function mapJsTypeToPsr3Level(string $type): string
172-
{
173-
return match ($type) {
174-
'warn' => 'warning',
175-
'log', 'table' => 'debug',
176-
'window_error', 'uncaught_error', 'unhandled_rejection' => 'error',
177-
default => $type
178-
};
173+
$bladeCompiler->directive('boostJs', fn (): string => '<?php echo '.\Laravel\Boost\Services\BrowserLogger::class.'::getScript(); ?>');
179174
}
180175

181176
private function hookIntoResponses(Router $router): void
182177
{
183-
$this->app->booted(function () use ($router) {
178+
$this->app->booted(function () use ($router): void {
184179
$router->pushMiddlewareToGroup('web', InjectBoost::class);
185180
});
186181
}

src/Concerns/MakesHttpRequests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function client(): PendingRequest
1717
]);
1818

1919
// Disable SSL verification for local development URLs and testing
20-
if (app()->environment(['local', 'testing']) || str_contains(config('boost.hosted.api_url', ''), '.test')) {
21-
$client = $client->withoutVerifying();
20+
if (app()->environment(['local', 'testing']) || str_contains((string) config('boost.hosted.api_url', ''), '.test')) {
21+
return $client->withoutVerifying();
2222
}
2323

2424
return $client;

src/Concerns/ReadsLogs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function getChunkSizeStart(): int
3434

3535
private function getChunkSizeMax(): int
3636
{
37-
return 1 * 1024 * 1024; // 1 MB
37+
return 1024 * 1024; // 1 MB
3838
}
3939

4040
/**
@@ -96,7 +96,7 @@ protected function readLastErrorEntry(string $logFile): ?string
9696

9797
for ($i = count($entries) - 1; $i >= 0; $i--) {
9898
if ($this->isErrorEntry($entries[$i])) {
99-
return trim($entries[$i]);
99+
return trim((string) $entries[$i]);
100100
}
101101
}
102102

src/Console/ExecuteToolCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function handle(): int
4747
try {
4848
/** @var Response $response */
4949
$response = $tool->handle($request); // @phpstan-ignore-line
50-
} catch (Throwable $e) {
51-
$errorResult = Response::error("Tool execution failed (E_THROWABLE): {$e->getMessage()}");
50+
} catch (Throwable $throwable) {
51+
$errorResult = Response::error("Tool execution failed (E_THROWABLE): {$throwable->getMessage()}");
5252

5353
$this->error(json_encode([
5454
'isError' => true,

src/Console/InstallCommand.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private function bootstrap(CodeEnvironmentsDetector $codeEnvironmentsDetector, H
8383
$this->terminal = $terminal;
8484

8585
$this->terminal->initDimensions();
86+
8687
$this->greenTick = $this->green('');
8788
$this->redCross = $this->red('');
8889

@@ -162,10 +163,10 @@ private function outro(): void
162163
{
163164
$label = 'https://boost.laravel.com/installed';
164165

165-
$ideNames = $this->selectedTargetMcpClient->map(fn (McpClient $mcpClient) => 'i:'.$mcpClient->mcpClientName())
166+
$ideNames = $this->selectedTargetMcpClient->map(fn (McpClient $mcpClient): string => 'i:'.$mcpClient->mcpClientName())
166167
->toArray();
167-
$agentNames = $this->selectedTargetAgents->map(fn (Agent $agent) => 'a:'.$agent->agentName())->toArray();
168-
$boostFeatures = $this->selectedBoostFeatures->map(fn ($feature) => 'b:'.$feature)->toArray();
168+
$agentNames = $this->selectedTargetAgents->map(fn (Agent $agent): string => 'a:'.$agent->agentName())->toArray();
169+
$boostFeatures = $this->selectedBoostFeatures->map(fn ($feature): string => 'b:'.$feature)->toArray();
169170

170171
$guidelines = [];
171172
if ($this->shouldInstallAiGuidelines()) {
@@ -211,12 +212,12 @@ protected function determineTestEnforcement(bool $ask = true): bool
211212
$hasMinimumTests = Str::of($process->getOutput())
212213
->trim()
213214
->explode("\n")
214-
->filter(fn ($line) => str_contains($line, '::'))
215+
->filter(fn ($line): bool => str_contains($line, '::'))
215216
->count() >= self::MIN_TEST_COUNT;
216217
}
217218

218219
if (! $hasMinimumTests && $ask) {
219-
$hasMinimumTests = select(
220+
return select(
220221
label: 'Should AI always create tests?',
221222
options: ['Yes', 'No'],
222223
default: 'Yes'
@@ -320,19 +321,17 @@ private function selectCodeEnvironments(string $contractClass, string $label): C
320321
$allEnvironments = $this->codeEnvironmentsDetector->getCodeEnvironments();
321322
$config = $this->getSelectionConfig($contractClass);
322323

323-
$availableEnvironments = $allEnvironments->filter(function (CodeEnvironment $environment) use ($contractClass) {
324-
return $environment instanceof $contractClass;
325-
});
324+
$availableEnvironments = $allEnvironments->filter(fn (CodeEnvironment $environment): bool => $environment instanceof $contractClass);
326325

327326
if ($availableEnvironments->isEmpty()) {
328327
return collect();
329328
}
330329

331-
$options = $availableEnvironments->mapWithKeys(function (CodeEnvironment $environment) use ($config) {
330+
$options = $availableEnvironments->mapWithKeys(function (CodeEnvironment $environment) use ($config): array {
332331
$displayMethod = $config['displayMethod'];
333332
$displayText = $environment->{$displayMethod}();
334333

335-
return [get_class($environment) => $displayText];
334+
return [$environment::class => $displayText];
336335
})->sort();
337336

338337
$detectedClasses = [];
@@ -342,9 +341,9 @@ private function selectCodeEnvironments(string $contractClass, string $label): C
342341
));
343342

344343
foreach ($installedEnvNames as $envKey) {
345-
$matchingEnv = $availableEnvironments->first(fn (CodeEnvironment $env) => strtolower($envKey) === strtolower($env->name()));
344+
$matchingEnv = $availableEnvironments->first(fn (CodeEnvironment $env): bool => strtolower((string) $envKey) === strtolower($env->name()));
346345
if ($matchingEnv) {
347-
$detectedClasses[] = get_class($matchingEnv);
346+
$detectedClasses[] = $matchingEnv::class;
348347
}
349348
}
350349

@@ -354,17 +353,17 @@ private function selectCodeEnvironments(string $contractClass, string $label): C
354353
default: array_unique($detectedClasses),
355354
scroll: $config['scroll'],
356355
required: $config['required'],
357-
hint: empty($detectedClasses) ? '' : sprintf('Auto-detected %s for you',
356+
hint: $detectedClasses === [] ? '' : sprintf('Auto-detected %s for you',
358357
Arr::join(array_map(function ($className) use ($availableEnvironments, $config) {
359-
$env = $availableEnvironments->first(fn ($env) => get_class($env) === $className);
358+
$env = $availableEnvironments->first(fn ($env): bool => $env::class === $className);
360359
$displayMethod = $config['displayMethod'];
361360

362361
return $env->{$displayMethod}();
363362
}, $detectedClasses), ', ', ' & ')
364363
)
365364
))->sort();
366365

367-
return $selectedClasses->map(fn ($className) => $availableEnvironments->first(fn ($env) => get_class($env) === $className));
366+
return $selectedClasses->map(fn ($className) => $availableEnvironments->first(fn ($env): bool => $env::class === $className));
368367
}
369368

370369
private function installGuidelines(): void
@@ -392,7 +391,7 @@ private function installGuidelines(): void
392391
$this->info(sprintf(' Adding %d guidelines to your selected agents', $guidelines->count()));
393392
DisplayHelper::grid(
394393
$guidelines
395-
->map(fn ($guideline, string $key) => $key.($guideline['custom'] ? '*' : ''))
394+
->map(fn ($guideline, string $key): string => $key.($guideline['custom'] ? '*' : ''))
396395
->values()
397396
->sort()
398397
->toArray(),
@@ -408,7 +407,7 @@ private function installGuidelines(): void
408407
/** @var CodeEnvironment $agent */
409408
foreach ($this->selectedTargetAgents as $agent) {
410409
$agentName = $agent->agentName();
411-
$displayAgentName = str_pad($agentName, $longestAgentName);
410+
$displayAgentName = str_pad((string) $agentName, $longestAgentName);
412411
$this->output->write(" {$displayAgentName}... ");
413412
/** @var Agent $agent */
414413
try {
@@ -424,7 +423,7 @@ private function installGuidelines(): void
424423

425424
$this->newLine();
426425

427-
if (count($failed) > 0) {
426+
if ($failed !== []) {
428427
$this->error(sprintf('✗ Failed to install guidelines to %d agent%s:',
429428
count($failed),
430429
count($failed) === 1 ? '' : 's'
@@ -466,6 +465,7 @@ private function installMcpServerConfig(): void
466465

467466
return;
468467
}
468+
469469
$this->newLine();
470470
$this->info(' Installing MCP servers to your selected IDEs');
471471
$this->newLine();
@@ -483,7 +483,7 @@ private function installMcpServerConfig(): void
483483
/** @var McpClient $mcpClient */
484484
foreach ($this->selectedTargetMcpClient as $mcpClient) {
485485
$ideName = $mcpClient->mcpClientName();
486-
$ideDisplay = str_pad($ideName, $longestIdeName);
486+
$ideDisplay = str_pad((string) $ideName, $longestIdeName);
487487
$this->output->write(" {$ideDisplay}... ");
488488
$results = [];
489489

@@ -532,7 +532,7 @@ private function installMcpServerConfig(): void
532532

533533
$this->newLine();
534534

535-
if (count($failed) > 0) {
535+
if ($failed !== []) {
536536
$this->error(sprintf('%s Some MCP servers failed to install:', $this->redCross));
537537
foreach ($failed as $ideName => $errors) {
538538
foreach ($errors as $server => $error) {

src/Install/Assists/Inertia.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ public function __construct(private Roster $roster) {}
1313

1414
public function gte(string $version): bool
1515
{
16-
return
17-
$this->roster->usesVersion(Packages::INERTIA_LARAVEL, $version, '>=') ||
18-
$this->roster->usesVersion(Packages::INERTIA_REACT, $version, '>=') ||
19-
$this->roster->usesVersion(Packages::INERTIA_SVELTE, $version, '>=') ||
20-
$this->roster->usesVersion(Packages::INERTIA_VUE, $version, '>=');
16+
if ($this->roster->usesVersion(Packages::INERTIA_LARAVEL, $version, '>=')) {
17+
return true;
18+
}
19+
20+
if ($this->roster->usesVersion(Packages::INERTIA_REACT, $version, '>=')) {
21+
return true;
22+
}
23+
24+
if ($this->roster->usesVersion(Packages::INERTIA_SVELTE, $version, '>=')) {
25+
return true;
26+
}
27+
28+
return $this->roster->usesVersion(Packages::INERTIA_VUE, $version, '>=');
2129
}
2230

2331
public function hasFormComponent(): bool

0 commit comments

Comments
 (0)