Skip to content

Commit da5a365

Browse files
Fixes some issues introduced by PHPStan (#14)
* Fixes phpstan issues * Apply fixes from StyleCI * Update middleware's documentation
1 parent 1c4beb8 commit da5a365

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ before_script:
3232
- set +H
3333

3434
script:
35-
- vendor/bin/phpunit
35+
- composer analyse
36+
- if [[ $COVERAGE != '1' ]]; then vendor/bin/phpunit; fi
3637
- if [[ $COVERAGE = '1' ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi
3738

3839
after_script:

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ includes:
55
parameters:
66
level: 7
77
ignoreErrors:
8-
- '#Call to static method [a-zA-Z0-9\\_]+\(\) on an unknown class Varnishable.#'
8+
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder::withTrashed().#'
99

src/Events/ModelHasUpdated.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ModelHasUpdated
2222
/**
2323
* Eloquent model object.
2424
*
25-
* @var \Illuminate\Database\Eloquent\Model
25+
* @var mixed
2626
*/
2727
protected $model;
2828

@@ -108,6 +108,6 @@ protected function retrieveModel() :?Model
108108

109109
$this->model = $this->getQuery($model)->find(data_get($this->data, $model->getKeyName()));
110110

111-
return $this->model;
111+
return ($this->model instanceof Model) ? $this->model : null;
112112
}
113113
}

src/Middleware/CacheableByVarnish.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33
namespace RichanFongdasen\Varnishable\Middleware;
44

55
use Closure;
6+
use RichanFongdasen\Varnishable\VarnishableService;
67
use Symfony\Component\HttpFoundation\Request;
78

89
class CacheableByVarnish
910
{
11+
/**
12+
* Varnishable Service Object.
13+
*
14+
* @var \RichanFongdasen\Varnishable\VarnishableService
15+
*/
16+
protected $varnishable;
17+
18+
/**
19+
* CacheableByVarnish Middleware constructor.
20+
*/
21+
public function __construct()
22+
{
23+
$this->varnishable = app(VarnishableService::class);
24+
}
25+
1026
/**
1127
* Handle an incoming request.
1228
*
1329
* @param \Symfony\Component\HttpFoundation\Request $request
1430
* @param \Closure $next
15-
* @param int|null $cacheDuration
31+
* @param int $cacheDuration
1632
*
17-
* @return mixed
33+
* @return \Symfony\Component\HttpFoundation\Response
1834
*/
19-
public function handle(Request $request, Closure $next, $cacheDuration = null)
35+
public function handle(Request $request, Closure $next, int $cacheDuration = 0)
2036
{
21-
\Varnishable::setRequestHeaders($request->headers);
37+
$this->varnishable->setRequestHeaders($request->headers);
2238

23-
if ((int) $cacheDuration > 0) {
24-
\Varnishable::setCacheDuration($cacheDuration);
39+
if ($cacheDuration > 0) {
40+
$this->varnishable->setCacheDuration($cacheDuration);
2541
}
2642

2743
$response = $next($request);
2844

29-
return \Varnishable::manipulate($response);
45+
return $this->varnishable->manipulate($response);
3046
}
3147
}

src/Middleware/UncacheableByVarnish.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,39 @@
33
namespace RichanFongdasen\Varnishable\Middleware;
44

55
use Closure;
6+
use RichanFongdasen\Varnishable\VarnishableService;
67
use Symfony\Component\HttpFoundation\Request;
78

89
class UncacheableByVarnish
910
{
11+
/**
12+
* Varnishable Service Object.
13+
*
14+
* @var \RichanFongdasen\Varnishable\VarnishableService
15+
*/
16+
protected $varnishable;
17+
18+
/**
19+
* UncacheableByVarnish Middleware constructor.
20+
*/
21+
public function __construct()
22+
{
23+
$this->varnishable = app(VarnishableService::class);
24+
}
25+
1026
/**
1127
* Handle an incoming request.
1228
*
1329
* @param \Symfony\Component\HttpFoundation\Request $request
1430
* @param \Closure $next
1531
*
16-
* @return mixed
32+
* @return \Symfony\Component\HttpFoundation\Response
1733
*/
1834
public function handle(Request $request, Closure $next)
1935
{
2036
$response = $next($request);
2137

22-
\Varnishable::addUncacheableHeader($response);
38+
$this->varnishable->addUncacheableHeader($response);
2339

2440
return $response;
2541
}

src/VarnishableObserver.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@
22

33
namespace RichanFongdasen\Varnishable;
44

5+
use Exception;
56
use Illuminate\Database\Eloquent\Model;
67
use RichanFongdasen\Varnishable\Events\ModelHasUpdated;
78

89
class VarnishableObserver
910
{
11+
/**
12+
* Varnishable Service Object.
13+
*
14+
* @var \RichanFongdasen\Varnishable\VarnishableService
15+
*/
16+
protected $varnishable;
17+
18+
/**
19+
* Varnishable Observer constructor.
20+
*/
21+
public function __construct()
22+
{
23+
$this->varnishable = app(VarnishableService::class);
24+
}
25+
1026
/**
1127
* Listening to any saved events.
1228
*
@@ -25,14 +41,16 @@ public function deleted(Model $model) :void
2541
*
2642
* @param \Illuminate\Database\Eloquent\Model $model
2743
*
44+
* @throws Exception
45+
*
2846
* @return void
2947
*/
3048
protected function handleModelInitialization(Model $model) :void
3149
{
3250
$updatedAt = $model->getAttribute('updated_at');
3351

3452
if ($updatedAt !== null) {
35-
\Varnishable::setLastModifiedHeader($updatedAt);
53+
$this->varnishable->setLastModifiedHeader($updatedAt);
3654
}
3755
}
3856

0 commit comments

Comments
 (0)