Skip to content

Commit a8253ee

Browse files
committed
Add Rector
Signed-off-by: Pushpak Chhajed <[email protected]>
1 parent 1171c69 commit a8253ee

File tree

70 files changed

+569
-593
lines changed

Some content is hidden

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

70 files changed

+569
-593
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",

rector.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
9+
return RectorConfig::configure()
10+
->withPaths([
11+
__DIR__.'/src',
12+
__DIR__.'/tests',
13+
])
14+
->withSkip([
15+
ReadOnlyPropertyRector::class,
16+
EncapsedStringsToSprintfRector::class,
17+
])
18+
->withPreparedSets(
19+
deadCode: true,
20+
codeQuality: true,
21+
codingStyle: true,
22+
typeDeclarations: true,
23+
earlyReturn: true,
24+
strictBooleans: true,
25+
)->withPhpSets(php81: true);

src/BoostServiceProvider.php

Lines changed: 5 additions & 5 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,7 @@ private function registerRoutes(): void
113113
* } $log */
114114
foreach ($logs as $log) {
115115
$logger->write(
116-
level: self::mapJsTypeToPsr3Level($log['type']),
116+
level: $this->mapJsTypeToPsr3Level($log['type']),
117117
message: self::buildLogMessageFromData($log['data']),
118118
context: [
119119
'url' => $log['url'],
@@ -165,10 +165,10 @@ private function registerBrowserLogger(): void
165165

166166
private function registerBladeDirectives(BladeCompiler $bladeCompiler): void
167167
{
168-
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
168+
$bladeCompiler->directive('boostJs', fn (): string => '<?php echo '.\Laravel\Boost\Services\BrowserLogger::class.'::getScript(); ?>');
169169
}
170170

171-
private static function mapJsTypeToPsr3Level(string $type): string
171+
private function mapJsTypeToPsr3Level(string $type): string
172172
{
173173
return match ($type) {
174174
'warn' => 'warning',
@@ -180,7 +180,7 @@ private static function mapJsTypeToPsr3Level(string $type): string
180180

181181
private function hookIntoResponses(Router $router): void
182182
{
183-
$this->app->booted(function () use ($router) {
183+
$this->app->booted(function () use ($router): void {
184184
$router->pushMiddlewareToGroup('web', InjectBoost::class);
185185
});
186186
}

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: 3 additions & 3 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

@@ -139,7 +139,7 @@ private function scanLogChunkForEntries(string $logFile, int $chunkSize): array
139139

140140
// Split by beginning-of-entry look-ahead (PSR-3 timestamp pattern).
141141
$entries = preg_split($this->getEntrySplitRegex(), $content, -1, PREG_SPLIT_NO_EMPTY);
142-
if (! $entries) {
142+
if ($entries === [] || $entries === false) {
143143
return [];
144144
}
145145

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

src/Install/Cli/DisplayHelper.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ class DisplayHelper
5151
*/
5252
public static function datatable(array $data, int $maxWidth = 80): void
5353
{
54-
if (! $data) {
54+
if ($data === []) {
5555
return;
5656
}
5757

5858
$columnWidths = self::calculateColumnWidths($data);
59-
$columnWidths = array_map(fn ($width) => $width + self::CELL_PADDING, $columnWidths);
59+
$columnWidths = array_map(fn (int $width): int => $width + self::CELL_PADDING, $columnWidths);
6060

6161
[$leftChar, $rightChar, $joinChar] = self::getBorderChars(self::BORDER_TOP);
6262
echo self::buildBorder($columnWidths, $leftChar, $rightChar, $joinChar).PHP_EOL;
@@ -69,6 +69,7 @@ public static function datatable(array $data, int $maxWidth = 80): void
6969
[$leftChar, $rightChar, $joinChar] = self::getBorderChars(self::BORDER_MIDDLE);
7070
echo self::buildBorder($columnWidths, $leftChar, $rightChar, $joinChar).PHP_EOL;
7171
}
72+
7273
$rowCount++;
7374
}
7475

@@ -81,7 +82,7 @@ public static function datatable(array $data, int $maxWidth = 80): void
8182
*/
8283
public static function grid(array $items, int $maxWidth = 80): void
8384
{
84-
if (empty($items)) {
85+
if ($items === []) {
8586
return;
8687
}
8788

@@ -104,6 +105,7 @@ public static function grid(array $items, int $maxWidth = 80): void
104105
[$leftChar, $rightChar, $joinChar] = self::getBorderChars(self::BORDER_MIDDLE);
105106
echo self::SPACE.self::buildBorder($cellWidths, $leftChar, $rightChar, $joinChar).PHP_EOL;
106107
}
108+
107109
$rowCount++;
108110
}
109111

@@ -150,9 +152,8 @@ private static function buildBorder(array $widths, string $leftChar, string $rig
150152
$border .= $joinChar;
151153
}
152154
}
153-
$border .= $rightChar;
154155

155-
return $border;
156+
return $border.$rightChar;
156157
}
157158

158159
/**
@@ -181,18 +182,16 @@ private static function buildGridRow(array $row, int $cellWidth, int $cellsPerRo
181182
$line = self::UNICODE_VERTICAL;
182183

183184
$cells = array_map(
184-
fn ($index) => self::formatGridCell($row[$index] ?? '', $cellWidth),
185+
fn (int $index): string => self::formatGridCell($row[$index] ?? '', $cellWidth),
185186
range(0, $cellsPerRow - 1)
186187
);
187188

188-
$line .= implode(self::UNICODE_VERTICAL, $cells).self::UNICODE_VERTICAL;
189-
190-
return $line;
189+
return $line.(implode(self::UNICODE_VERTICAL, $cells).self::UNICODE_VERTICAL);
191190
}
192191

193192
private static function formatGridCell(string $item, int $cellWidth): string
194193
{
195-
if (! $item) {
194+
if ($item === '' || $item === '0') {
196195
return str_repeat(self::SPACE, $cellWidth);
197196
}
198197

0 commit comments

Comments
 (0)