Skip to content

Commit 507d233

Browse files
committed
refactor: Improve error handling and type usage in pipeline traits
- Update NEON files to reflect additional errors and type coverage - Add 'typePerfect.narrowPublicClassMethodParamType' NEON file for error tracking - Modify pipeline traits to use 'get' method for better encapsulation - Enhance PHPStan configuration for stricter type checking
1 parent 69265b7 commit 507d233

File tree

8 files changed

+25
-12
lines changed

8 files changed

+25
-12
lines changed

baselines/loader.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# total 10 errors
1+
# total 11 errors
22
includes:
33
- disallowed.function.neon
44
- function.alreadyNarrowedType.neon
55
- method.notFound.neon
66
- nullCoalesce.variable.neon
77
- trait.unused.neon
88
- typeCoverage.paramTypeCoverage.neon
9+
- typePerfect.narrowPublicClassMethodParamType.neon
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Parameters should have "Guanguans\\LaravelApiResponse\\Pipes\\CastDataPipe" types as the only types passed to this method$#'
7+
count: 1
8+
path: ../src/ApiResponse.php

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"phpstan/phpstan-webmozart-assert": "^2.0",
6565
"povils/phpmnd": "^3.6",
6666
"rector/rector": "^2.0",
67+
"rector/type-perfect": "^2.0",
6768
"shipmonk/composer-dependency-analyser": "^1.8",
6869
"shipmonk/phpstan-baseline-per-identifier": "^2.1",
6970
"spatie/pest-plugin-snapshots": "^1.1 || ^2.0",

phpstan.neon

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ parameters:
4646
param_type: 100
4747
property_type: 100
4848
constant_type: 100
49+
type_perfect:
50+
narrow_param: true
51+
narrow_return: true
52+
null_over_false: true
53+
no_mixed: true
54+
no_mixed_property: true
55+
no_mixed_caller: true
4956
disallowedFunctionCalls:
5057
-
5158
function: 'env()'
@@ -70,4 +77,5 @@ parameters:
7077
- identifier: missingType.iterableValue
7178
- identifier: new.static
7279
- identifier: offsetAccess.nonArray
73-
- identifier: return.type
80+
- identifier: return.type
81+
- identifier: typePerfect.noMixedMethodCaller

src/Concerns/HasExceptionPipes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ private function spliceExceptionPipes(string $findExceptionPipe, array $exceptio
9191
if (0 === $idx) {
9292
$this->exceptionPipes->unshift(...$exceptionPipes);
9393
} else {
94-
$this->exceptionPipes->splice($idx, 1, [...$exceptionPipes, $this->exceptionPipes[$idx]]);
94+
$this->exceptionPipes->splice($idx, 1, [...$exceptionPipes, $this->exceptionPipes->get($idx)]);
9595
}
9696
} elseif ($this->exceptionPipes->count() - 1 === $idx) {
9797
$this->exceptionPipes->push(...$exceptionPipes);
9898
} else {
99-
$this->exceptionPipes->splice($idx, 1, [$this->exceptionPipes[$idx], ...$exceptionPipes]);
99+
$this->exceptionPipes->splice($idx, 1, [$this->exceptionPipes->get($idx), ...$exceptionPipes]);
100100
}
101101

102102
return $this;

src/Concerns/HasPipes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ private function splicePipes(string $findPipe, array $pipes, bool $before): self
9696
if (0 === $idx) {
9797
$this->pipes->unshift(...$pipes);
9898
} else {
99-
$this->pipes->splice($idx, 1, [...$pipes, $this->pipes[$idx]]);
99+
$this->pipes->splice($idx, 1, [...$pipes, $this->pipes->get($idx)]);
100100
}
101101
} elseif ($this->pipes->count() - 1 === $idx) {
102102
$this->pipes->push(...$pipes);
103103
} else {
104-
$this->pipes->splice($idx, 1, [$this->pipes[$idx], ...$pipes]);
104+
$this->pipes->splice($idx, 1, [$this->pipes->get($idx), ...$pipes]);
105105
}
106106

107107
return $this;

src/Pipes/IterableDataPipe.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function handle(array $structure, \Closure $next): JsonResponse
4141

4242
private function dataFor(mixed $data): mixed
4343
{
44-
// return is_iterable($data) && !\is_array($data) ? iterator_to_array($data) : $data;
4544
return $data instanceof \Traversable ? iterator_to_array($data) : $data;
4645
}
4746
}

src/Support/Traits/WithPipeArgs.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ trait WithPipeArgs
1717
{
1818
public static function with(mixed ...$args): string
1919
{
20-
if ([] === $args) {
21-
return static::class;
22-
}
23-
24-
return static::class.':'.implode(',', $args);
20+
return [] === $args ? static::class : static::class.':'.implode(',', $args);
2521
}
2622
}

0 commit comments

Comments
 (0)