Skip to content

Commit eed7738

Browse files
authored
Merge pull request #590 from FriendsOfSymfony/phpstan-levels
Phpstan levels
2 parents 5fb0c38 + 50de1fa commit eed7738

19 files changed

+92
-59
lines changed

Diff for: .github/workflows/static.yml

+2-27
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ on:
88

99
jobs:
1010
phpstan-src:
11-
name: PHPStan src
11+
name: PHPStan
1212
runs-on: ubuntu-latest
1313

1414
steps:
1515
- name: Checkout code
1616
uses: actions/checkout@v4
1717

1818
- name: Pull in optional dependencies
19-
run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^4.2 symfony/process
19+
run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^4.2 symfony/process mockery/mockery phpstan/phpstan-mockery guzzlehttp/guzzle php-http/mock-client
2020

2121
- name: Cache Vendor
2222
id: cache-vendor
@@ -30,31 +30,6 @@ jobs:
3030
with:
3131
args: analyze --no-progress
3232

33-
phpstan-tests:
34-
name: PHPStan tests
35-
runs-on: ubuntu-latest
36-
env:
37-
REQUIRE_DEV: "true"
38-
39-
steps:
40-
- name: Checkout code
41-
uses: actions/checkout@v4
42-
43-
- name: Pull in optional dependencies
44-
run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^4.2
45-
46-
- name: Cache Vendor
47-
id: cache-vendor
48-
uses: actions/cache@v4
49-
with:
50-
path: vendor
51-
key: ${{ runner.os }}-vendor
52-
53-
- name: PHPStan
54-
uses: docker://oskarstark/phpstan-ga
55-
with:
56-
args: analyze --no-progress -c phpstan.tests.neon.dist
57-
5833
php-cs-fixer:
5934
name: PHP-CS-Fixer
6035
runs-on: ubuntu-latest

Diff for: doc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,5 @@ doctest:
154154
"results in $(BUILDDIR)/doctest/output.txt."
155155

156156
spelling:
157-
$(SPHINXBUILD) -b spelling $(ALLSPHINXOPTS) -w $(BUILDDIR)/spelling/output.txt $(BUILDDIR)/spelling
157+
$(SPHINXBUILD) -b spelling $(ALLSPHINXOPTS) -w $(BUILDDIR)/spelling/output.txt $(BUILDDIR)/spelling
158158
@echo "Spelling report generated in $(BUILDDIR)/spelling/output.txt"

Diff for: phpstan.neon.dist

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
parameters:
2-
level: 1
2+
level: 4
33
paths:
44
- src
5+
- tests
6+
7+
treatPhpDocTypesAsCertain: false

Diff for: phpstan.tests.neon.dist

-4
This file was deleted.

Diff for: src/ProxyClient/Cloudflare.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function invalidateTags(array $tags): static
8181
sprintf(self::API_ENDPOINT.'/zones/%s/purge_cache', $this->options['zone_identifier']),
8282
[],
8383
false,
84-
$this->json_encode(['tags' => $tags])
84+
json_encode(['tags' => $tags], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)
8585
);
8686

8787
return $this;
@@ -115,7 +115,7 @@ public function clear(): static
115115
sprintf(self::API_ENDPOINT.'/zones/%s/purge_cache', $this->options['zone_identifier']),
116116
['Accept' => 'application/json'],
117117
false,
118-
$this->json_encode(['purge_everything' => true])
118+
json_encode(['purge_everything' => true], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)
119119
);
120120

121121
return $this;
@@ -130,7 +130,7 @@ public function flush(): int
130130
sprintf(self::API_ENDPOINT.'/zones/%s/purge_cache', $this->options['zone_identifier']),
131131
[],
132132
false,
133-
$this->json_encode(['files' => $urlChunk])
133+
json_encode(['files' => $urlChunk], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)
134134
);
135135
}
136136
$this->purgeByUrlsData = [];
@@ -165,14 +165,4 @@ protected function configureOptions(): OptionsResolver
165165

166166
return $resolver;
167167
}
168-
169-
private function json_encode(array $data): string
170-
{
171-
$json = json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
172-
if (false === $json) {
173-
throw new \InvalidArgumentException(sprintf('Cannot encode "$data": %s', json_last_error_msg()));
174-
}
175-
176-
return $json;
177-
}
178168
}

Diff for: src/ProxyClient/MultiplexerClient.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function clear(): static
133133
* Invoke the given $method on all available ProxyClients implementing the
134134
* given $interface.
135135
*
136-
* @param string $interface The FQN of the interface
136+
* @param class-string $interface The FQN of the interface
137137
* @param string $method The method to invoke
138138
* @param array<mixed> $arguments The arguments to be passed to the method
139139
*/
@@ -155,7 +155,9 @@ private function getProxyClients(string $interface): array
155155
{
156156
return array_filter(
157157
$this->proxyClients,
158-
static function ($proxyClient) use ($interface) {
158+
static function (ProxyClient $proxyClient) use ($interface) {
159+
// https://github.com/phpstan/phpstan/issues/8464
160+
// @phpstan-ignore-next-line
159161
return is_subclass_of($proxyClient, $interface);
160162
}
161163
);

Diff for: src/SymfonyCache/PurgeTagsListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function handlePurgeTags(CacheEvent $event): void
118118
if (1 === $reflection->getMethod('all')->getNumberOfParameters()) {
119119
$headers = $request->headers->all($this->tagsHeader);
120120
} else {
121-
$headers = $request->headers->get($this->tagsHeader, '', false);
121+
$headers = $request->headers->get($this->tagsHeader, '');
122122
}
123123

124124
$tags = $this->tagsParser->parseTagsHeaderValue($headers);

Diff for: src/Test/EventDispatchingHttpCacheTestCase.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ public function testPreStoreResponseNoStore()
251251
$httpCache = $this->getHttpCachePartialMock();
252252
$testListener = new TestListener($this, $httpCache, $request);
253253
$testListener->preStoreResponse = $preStoreResponse;
254+
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
254255
$httpCache->addSubscriber($testListener);
255256

256257
$store = $this->createMock(StoreInterface::class);
@@ -279,8 +280,8 @@ public function testPreInvalidateCalled(): void
279280

280281
$httpCache = $this->getHttpCachePartialMock(['pass']);
281282
$testListener = new TestListener($this, $httpCache, $request);
282-
$httpCache->addSubscriber($testListener);
283283
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
284+
$httpCache->addSubscriber($testListener);
284285
$httpCache
285286
->method('pass')
286287
->with($request)
@@ -307,8 +308,8 @@ public function testPreInvalidateReturnEarly(): void
307308
$httpCache = $this->getHttpCachePartialMock(['pass']);
308309
$testListener = new TestListener($this, $httpCache, $request);
309310
$testListener->preInvalidateResponse = $response;
310-
$httpCache->addSubscriber($testListener);
311311
$this->assertTrue(method_exists($httpCache, 'addSubscriber'));
312+
$httpCache->addSubscriber($testListener);
312313
$httpCache
313314
->expects($this->never())
314315
->method('pass')
@@ -327,6 +328,7 @@ public function testAddListener(): void
327328

328329
$httpCache = $this->getHttpCachePartialMock(['lookup']);
329330
$simpleListener = new SimpleListener($this, $httpCache, $request);
331+
$this->assertTrue(method_exists($httpCache, 'addListener'));
330332
$httpCache->addListener(Events::PRE_HANDLE, [$simpleListener, 'callback']);
331333

332334
$httpCache

Diff for: src/Test/WebServerSubscriber.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace FOS\HttpCache\Test;
1313

14-
use PHPUnit\Event\Code\TestMethod;
1514
use PHPUnit\Event\TestRunner\ExecutionStarted;
1615
use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber;
1716
use PHPUnit\Event\TestSuite\TestSuite;
@@ -52,8 +51,6 @@ private function hasTestsWithGroup(TestSuite $testSuite, string $group): bool
5251
continue;
5352
}
5453

55-
assert($test instanceof TestMethod);
56-
5754
foreach ($test->metadata()->isGroup() as $testGroup) {
5855
assert($testGroup instanceof Group);
5956

@@ -83,7 +80,7 @@ public function startPhpWebServer(): int
8380

8481
$this->waitFor($this->getHostName(), (int) $this->getPort(), 2000);
8582

86-
return $output[0];
83+
return (int) $output[0];
8784
}
8885

8986
public function getHostName(): string

Diff for: tests/Functional/ProxyClient/NginxProxyClientTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testPurgeContentType(): void
3535
{
3636
$this->markTestSkipped('Not working with nginx, it can only purge one type');
3737

38-
$this->assertPurgeContentType($this->getProxyClient());
38+
// $this->assertPurgeContentType($this->getProxyClient());
3939
}
4040

4141
public function testPurgeSeparateLocationHost(): void
@@ -57,6 +57,6 @@ public function testRefreshContentType(): void
5757
{
5858
$this->markTestSkipped('TODO: is nginx mixing up variants?');
5959

60-
$this->assertRefreshContentType($this->getProxyClient());
60+
// $this->assertRefreshContentType($this->getProxyClient());
6161
}
6262
}

Diff for: tests/Functional/Symfony/EventDispatchingHttpCacheTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function testEventListeners(): void
4444
->shouldReceive('handle')
4545
->andReturn($expectedResponse)
4646
->getMock();
47+
// https://github.com/phpstan/phpstan-mockery/issues/8
48+
/** @phpstan-ignore-next-line */
4749
$store = \Mockery::mock(StoreInterface::class)
4850
->shouldReceive('lookup')->andReturn(null)->times(1)
4951
->shouldReceive('write')->times(1)

Diff for: tests/Unit/CacheInvalidatorTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function testSupportsInvalid(): void
7878
public function testInvalidatePath(): void
7979
{
8080
/** @var MockInterface&PurgeCapable $purge */
81+
// https://github.com/phpstan/phpstan-mockery/issues/8
82+
/** @phpstan-ignore-next-line */
8183
$purge = \Mockery::mock(PurgeCapable::class)
8284
->shouldReceive('purge')->once()->with('/my/route', [])
8385
->shouldReceive('purge')->once()->with('/my/route', ['X-Test-Header' => 'xyz'])
@@ -97,6 +99,8 @@ public function testRefreshPath(): void
9799
{
98100
$headers = ['X' => 'Y'];
99101
/** @var MockInterface&RefreshCapable $refresh */
102+
// https://github.com/phpstan/phpstan-mockery/issues/8
103+
/** @phpstan-ignore-next-line */
100104
$refresh = \Mockery::mock(RefreshCapable::class)
101105
->shouldReceive('refresh')->once()->with('/my/route', $headers)
102106
->shouldReceive('flush')->never()
@@ -190,6 +194,8 @@ public function testProxyClientExceptionsAreLogged(): void
190194

191195
$unreachableException = ProxyUnreachableException::proxyUnreachable($clientException);
192196

197+
// https://github.com/phpstan/phpstan-mockery/issues/8
198+
/** @phpstan-ignore-next-line */
193199
$response = \Mockery::mock(ResponseInterface::class)
194200
->shouldReceive('getStatusCode')->andReturn(403)
195201
->shouldReceive('getReasonPhrase')->andReturn('Forbidden')
@@ -206,6 +212,8 @@ public function testProxyClientExceptionsAreLogged(): void
206212

207213
$cacheInvalidator = new CacheInvalidator($proxyClient);
208214

215+
// https://github.com/phpstan/phpstan-mockery/issues/8
216+
/** @phpstan-ignore-next-line */
209217
$logger = \Mockery::mock(LoggerInterface::class)
210218
->shouldReceive('log')->once()
211219
->with(

Diff for: tests/Unit/ResponseTaggerTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function testGetTagsHeaderValue(): void
4848

4949
public function testTagResponseReplace(): void
5050
{
51+
// https://github.com/phpstan/phpstan-mockery/issues/8
52+
/** @phpstan-ignore-next-line */
5153
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
5254
->shouldReceive('getTagsHeaderValue')
5355
->with(['tag-1', 'tag-2'])
@@ -73,6 +75,8 @@ public function testTagResponseReplace(): void
7375

7476
public function testTagResponseAdd(): void
7577
{
78+
// https://github.com/phpstan/phpstan-mockery/issues/8
79+
/** @phpstan-ignore-next-line */
7680
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
7781
->shouldReceive('getTagsHeaderValue')
7882
->with(['tag-1', 'tag-2'])
@@ -105,6 +109,8 @@ public function testTagResponseNoTags(): void
105109

106110
$tagger = new ResponseTagger(['header_formatter' => $headerFormatter]);
107111

112+
// https://github.com/phpstan/phpstan-mockery/issues/8
113+
/** @phpstan-ignore-next-line */
108114
$response = \Mockery::mock(ResponseInterface::class)
109115
->shouldReceive('withHeader')->never()
110116
->shouldReceive('withAddedHeader')->never()

Diff for: tests/Unit/SymfonyCache/CacheAwareTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\Tests\Unit\SymfonyCache;
13+
14+
use FOS\HttpCache\SymfonyCache\HttpCacheAware;
15+
use FOS\HttpCache\SymfonyCache\HttpCacheProvider;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
class CacheAwareTest extends TestCase
20+
{
21+
private HttpKernelInterface $cacheKernel;
22+
23+
protected function setUp(): void
24+
{
25+
$this->cacheKernel = $this->createMock(HttpKernelInterface::class);
26+
}
27+
28+
public function testCacheGetterSetter(): void
29+
{
30+
$aware = new AppHttpCacheAware();
31+
$aware->setHttpCache($this->cacheKernel);
32+
$this->assertSame($this->cacheKernel, $aware->getHttpCache());
33+
}
34+
}
35+
36+
class AppHttpCacheAware implements HttpCacheProvider
37+
{
38+
use HttpCacheAware;
39+
}

Diff for: tests/Unit/SymfonyCache/KernelDispatcherTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public function testFlush(): void
3333
->with($this->callback(function (Request $request) {
3434
// Test if the Symfony request contains the relevant information
3535
// from the PSR-7 request
36-
$valid = true;
37-
$valid = $valid && 'PURGETAGS' === $request->getMethod();
36+
$valid = 'PURGETAGS' === $request->getMethod();
3837
$valid = $valid && 'foobar' === $request->headers->get('content-type');
3938
$valid = $valid && 'foo,bar,stuff' === $request->headers->get('x-cache-tags');
4039
$valid = $valid && ['query' => 'string', 'more' => 'stuff'] === $request->query->all();

Diff for: tests/Unit/SymfonyCache/PurgeListenerTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ public function testInvalidConfiguration(): void
187187

188188
private function getKernelMock(StoreInterface $store): MockInterface&CacheInvalidation
189189
{
190+
// https://github.com/phpstan/phpstan-mockery/issues/8
191+
/* @phpstan-ignore-next-line */
190192
return \Mockery::mock(CacheInvalidation::class)
191193
->shouldReceive('getStore')
192194
->once()
@@ -196,6 +198,8 @@ private function getKernelMock(StoreInterface $store): MockInterface&CacheInvali
196198

197199
private function getUnusedKernelMock(): CacheInvalidation&MockInterface
198200
{
201+
// https://github.com/phpstan/phpstan-mockery/issues/8
202+
/* @phpstan-ignore-next-line */
199203
return \Mockery::mock(CacheInvalidation::class)
200204
->shouldNotReceive('getStore')
201205
->getMock();

Diff for: tests/Unit/SymfonyCache/PurgeTagsListenerTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public function testInvalidConfiguration(): void
180180

181181
private function getKernelMock(StoreInterface $store): CacheInvalidation&MockInterface
182182
{
183+
// https://github.com/phpstan/phpstan-mockery/issues/8
184+
/* @phpstan-ignore-next-line */
183185
return \Mockery::mock(CacheInvalidation::class)
184186
->shouldReceive('getStore')
185187
->once()
@@ -189,6 +191,8 @@ private function getKernelMock(StoreInterface $store): CacheInvalidation&MockInt
189191

190192
private function getUnusedKernelMock(): CacheInvalidation&MockInterface
191193
{
194+
// https://github.com/phpstan/phpstan-mockery/issues/8
195+
/* @phpstan-ignore-next-line */
192196
return \Mockery::mock(CacheInvalidation::class)
193197
->shouldNotReceive('getStore')
194198
->getMock();

Diff for: tests/Unit/Test/PHPUnit/IsCacheHitConstraintTestCase.php

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function setUp(): void
2626

2727
public function testMatches(): void
2828
{
29+
// https://github.com/phpstan/phpstan-mockery/issues/8
30+
/** @phpstan-ignore-next-line */
2931
$response = $this->getResponseMock()
3032
->shouldReceive('hasHeader')->with('cache-header')->andReturn(true)
3133
->shouldReceive('getHeaderLine')->with('cache-header')->once()->andReturn('MISS')
@@ -44,6 +46,8 @@ public function testMatchesThrowsExceptionIfHeaderIsMissing(): void
4446
$this->expectException(\RuntimeException::class);
4547
$this->expectExceptionMessage('Response has no "cache-header" header');
4648

49+
// https://github.com/phpstan/phpstan-mockery/issues/8
50+
/** @phpstan-ignore-next-line */
4751
$response = $this->getResponseMock()
4852
->shouldReceive('hasHeader')->with('cache-header')->once()->andReturn(false)
4953
->shouldReceive('getStatusCode')->andReturn(200)

0 commit comments

Comments
 (0)