Skip to content

Commit f3bedcb

Browse files
committed
feat: Adiciona resumo das correções do PHPStan e atualiza namespaces para PivotPHP
1 parent 268e816 commit f3bedcb

15 files changed

+74
-69
lines changed

PHPSTAN_FIXES_SUMMARY.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PHPStan Fixes Summary
2+
3+
This document summarizes the changes made to fix PHPStan errors in the pivotphp-cycle-orm project.
4+
5+
## Issues Fixed
6+
7+
1. **Private/Protected Property Access**: Fixed direct access to private properties of `PivotPHP\Core\Http\Request`
8+
2. **Type Mismatch**: Resolved issues with `CycleRequest` vs `Request` type compatibility
9+
3. **Namespace References**: Updated old `Helix` namespace references to `PivotPHP\Core`
10+
11+
## Files Modified
12+
13+
### 1. `/src/Health/HealthCheckMiddleware.php`
14+
- Replaced direct property access (`$req->path`, `$req->pathCallable`) with public method `$req->getPathCallable()`
15+
- Replaced direct query property access (`$req->query`) with public method `$req->get('detailed', false)`
16+
- Fixed code style issue (removed space before semicolon)
17+
18+
### 2. `/src/Middleware/CycleMiddleware.php`
19+
- Changed middleware to pass the original `Request` object instead of `CycleRequest` wrapper
20+
- Added `cycleRequest` as an attribute on the original request for compatibility
21+
- This ensures type compatibility with the middleware chain
22+
23+
### 3. `/src/Middleware/TransactionMiddleware.php`
24+
- Removed `CycleRequest` type hints, now only accepts `Request`
25+
- Removed unused `CycleRequest` import
26+
- Updated `getRouteInfo()` method to use public methods `$req->getMethod()` and `$req->getPathCallable()`
27+
- Simplified middleware logic by removing CycleRequest instance checks
28+
29+
### 4. `/phpstan.neon`
30+
- Removed obsolete `Helix\Http\Request` ignore pattern
31+
- Updated `universalObjectCratesClasses` from `Helix\*` to `PivotPHP\Core\*` namespaces
32+
33+
## Result
34+
35+
All PHPStan errors have been resolved. The code now:
36+
- Passes PHPStan analysis at Level 9 (highest level)
37+
- Adheres to PSR-12 coding standards
38+
- Properly uses public API methods instead of accessing private properties
39+
- Has consistent type handling throughout the middleware chain

phpstan.neon

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ parameters:
77
- tests
88
bootstrapFiles:
99
- vendor/autoload.php
10-
ignoreErrors:
11-
- '#Parameter \#1 of callable .* expects Helix\\Http\\Request, .* given#'
1210

1311
# PivotPHP specific ignores
1412
universalObjectCratesClasses:
15-
- Helix\Http\Request
16-
- Helix\Http\Response
17-
- Helix\Core\Application
18-
19-
# Stub files for better type inference
20-
# stubFiles:
21-
# - ../pivotphp-core/src/Http/Request.php
13+
- PivotPHP\Core\Http\Request
14+
- PivotPHP\Core\Http\Response
15+
- PivotPHP\Core\Core\Application

src/CycleServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ private function registerMigrator(): void
373373
'cycle.migrator',
374374
function () {
375375
// Retorna um migrator básico ou mock para desenvolvimento
376-
return new class() {
376+
return new class () {
377377
/**
378378
* @return array{pending: array<int, mixed>, executed: array<int, mixed>}
379379
*/

src/Exceptions/CycleException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
namespace PivotPHP\CycleORM\Exceptions;
44

5-
class CycleORMException extends \Exception {}
5+
class CycleORMException extends \Exception
6+
{
7+
}

src/Health/HealthCheckMiddleware.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,8 @@ public function __construct(Application $app)
2020

2121
public function handle(Request $req, Response $res, callable $next): void
2222
{
23-
// Compatível com PivotPHP: prioriza getPathInfo(), depois path, depois pathCallable
24-
if (method_exists($req, 'getPathInfo') && is_callable([$req, 'getPathInfo'])) {
25-
$path = $req->getPathInfo();
26-
} elseif (property_exists($req, 'path')) {
27-
$path = $req->path;
28-
} elseif (property_exists($req, 'pathCallable')) {
29-
$path = $req->pathCallable;
30-
} else {
31-
$path = null;
32-
}
23+
// Use the public method getPathCallable() to get the actual request path
24+
$path = $req->getPathCallable();
3325

3426
// Verificar se é uma requisição de health check
3527
if ('/health/cycle' === $path || '/health' === $path) {
@@ -43,7 +35,7 @@ public function handle(Request $req, Response $res, callable $next): void
4335

4436
private function handleHealthCheck(Request $req, Response $res): void
4537
{
46-
$detailed = (is_array($req->query) && isset($req->query['detailed'])) ? $req->query['detailed'] : false;
38+
$detailed = $req->get('detailed', false);
4739

4840
if ($detailed) {
4941
$health = CycleHealthCheck::detailedCheck($this->app);
@@ -56,7 +48,6 @@ private function handleHealthCheck(Request $req, Response $res): void
5648
$res->status($statusCode)
5749
->header('Content-Type', 'application/json')
5850
->header('Cache-Control', 'no-cache, no-store, must-revalidate')
59-
->json($health)
60-
;
51+
->json($health);
6152
}
6253
}

src/Helpers/CycleHelpers.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public static function applyFilters(object $query, array $filters, array $allowe
8484
)
8585
) {
8686
$query = $query->where($field, '>=', $matches[1])
87-
->where($field, '<=', $matches[2])
88-
;
87+
->where($field, '<=', $matches[2]);
8988
} else {
9089
$query = $query->where($field, $value);
9190
}

src/Middleware/CycleMiddleware.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public function handle(Request $req, Response $res, callable $next): void
6666
$cycleRequest->db = $db;
6767
}
6868

69-
// Passa o CycleRequest wrapper para o próximo handler
70-
$next($cycleRequest, $res);
69+
// Pass the original request to maintain compatibility with the next handler
70+
// The CycleRequest wrapper can be accessed through the original request's attributes
71+
$req->setAttribute('cycleRequest', $cycleRequest);
72+
$next($req, $res);
7173
}
7274
}

src/Middleware/TransactionMiddleware.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PivotPHP\CycleORM\Middleware;
44

55
use PivotPHP\Core\Core\Application;
6-
use PivotPHP\CycleORM\Http\CycleRequest;
76
use PivotPHP\Core\Http\Request;
87
use PivotPHP\Core\Http\Response;
98

@@ -21,7 +20,7 @@ public function __construct(Application $app)
2120
*
2221
* @param callable(Request, Response):void $next função next do PivotPHP
2322
*/
24-
public function __invoke(CycleRequest|Request $req, Response $res, callable $next): void
23+
public function __invoke(Request $req, Response $res, callable $next): void
2524
{
2625
$this->handle($req, $res, $next);
2726
}
@@ -31,7 +30,7 @@ public function __invoke(CycleRequest|Request $req, Response $res, callable $nex
3130
*
3231
* @param callable(Request, Response):void $next função next do PivotPHP, recebe Request e Response
3332
*/
34-
public function handle(CycleRequest|Request $req, Response $res, callable $next): void
33+
public function handle(Request $req, Response $res, callable $next): void
3534
{
3635
// Use sempre o container PSR-11 para buscar serviços
3736
if (method_exists($this->app, 'getContainer')) {
@@ -55,13 +54,8 @@ public function handle(CycleRequest|Request $req, Response $res, callable $next)
5554
$transactionStarted = true;
5655
$this->logDebug('Transaction started for route: ' . $this->getRouteInfo($req));
5756

58-
// If we have a CycleRequest, pass the original request to the next middleware
59-
// to ensure compatibility with the route handler
60-
if ($req instanceof CycleRequest) {
61-
$next($req->getOriginalRequest(), $res);
62-
} else {
63-
$next($req, $res);
64-
}
57+
// Pass the request to the next middleware
58+
$next($req, $res);
6559

6660
// Commit apenas se há mudanças
6761
if (is_object($em) && method_exists($em, 'commitTransaction')) {
@@ -85,20 +79,10 @@ public function handle(CycleRequest|Request $req, Response $res, callable $next)
8579
}
8680
}
8781

88-
private function getRouteInfo(CycleRequest|Request $req): string
82+
private function getRouteInfo(Request $req): string
8983
{
90-
$method = property_exists($req, 'method')
91-
&& (is_string($req->method) || is_numeric($req->method))
92-
? (string) $req->method
93-
: 'Unknown';
94-
95-
$uri = property_exists($req, 'pathCallable')
96-
&& (is_string($req->pathCallable) || is_numeric($req->pathCallable))
97-
? (string) $req->pathCallable
98-
: (property_exists($req, 'path')
99-
&& (is_string($req->path) || is_numeric($req->path))
100-
? (string) $req->path
101-
: 'Unknown');
84+
$method = $req->getMethod();
85+
$uri = $req->getPathCallable();
10286

10387
return "{$method} {$uri}";
10488
}

tests/Feature/CycleRequestTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function testMethodForwarding(): void
3838
$this->originalRequest
3939
->expects($this->once())
4040
->method('getMethod')
41-
->willReturn('POST')
42-
;
41+
->willReturn('POST');
4342

4443
// Call should be forwarded to original request
4544
$result = $this->cycleRequest->getMethod();

tests/Feature/MonitoringTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private function createApplication(): Application
292292
$container->bind(
293293
'config',
294294
function () {
295-
return new class() {
295+
return new class () {
296296
public function get(string $key, mixed $default = null): mixed
297297
{
298298
return match ($key) {

0 commit comments

Comments
 (0)