Skip to content

Commit 17f5bd3

Browse files
authored
Merge pull request #21 from stof/phpstan
Add phpstan
2 parents 9ca930c + 1f310ae commit 17f5bd3

12 files changed

+65
-7
lines changed

.github/workflows/ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ jobs:
1616
php-version: '8.0'
1717
- run: composer validate --strict --no-check-lock
1818

19+
static_analysis:
20+
name: Static analysis
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v2
24+
- uses: shivammathur/setup-php@v2
25+
with:
26+
coverage: none
27+
php-version: '8.0'
28+
- name: Install dependencies
29+
run: composer update --ansi --no-progress --prefer-dist --no-interaction
30+
- run: vendor/bin/phpstan analyze
31+
1932
tests:
2033
name: "Tests on PHP ${{ matrix.php }}${{ matrix.name_suffix }}"
2134
runs-on: ubuntu-latest

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
"symfony/http-kernel": "^4.4.13 || ^5.3 || ^6.0"
2222
},
2323
"require-dev": {
24+
"jangregor/phpstan-prophecy": "^1.0",
2425
"phpspec/prophecy-phpunit": "^2.0",
26+
"phpstan/phpstan": "^1.5",
27+
"phpstan/phpstan-deprecation-rules": "^1.0",
28+
"phpstan/phpstan-phpunit": "^1.0",
29+
"phpstan/phpstan-symfony": "^1.1",
2530
"phpunit/phpunit": "^9.5",
2631
"symfony/phpunit-bridge": "^5.3 || ^6.0"
2732
},

phpstan.neon

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- src/
5+
- tests/
6+
ignoreErrors:
7+
- '#^Method Incenteev\\HashedAssetBundle\\Tests\\[^:]++\:\:test\w++\(\) has no return type specified\.$#'
8+
- '#^Method Incenteev\\HashedAssetBundle\\Tests\\[^:]++\:\:get\w++\(\) has no return type specified\.$#'
9+
10+
includes:
11+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
12+
- vendor/phpstan/phpstan-phpunit/extension.neon
13+
- vendor/phpstan/phpstan-phpunit/rules.neon
14+
- vendor/phpstan/phpstan-symfony/extension.neon
15+
- vendor/phpstan/phpstan-symfony/rules.neon
16+
- vendor/jangregor/phpstan-prophecy/extension.neon

src/Asset/HashingVersionStrategy.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ public function __construct(AssetHasherInterface $hasher, string $format = null)
1616
$this->hasher = $hasher;
1717
}
1818

19+
/**
20+
* @param string $path
21+
*/
1922
public function getVersion($path): string
2023
{
2124
return $this->hasher->computeHash($path);
2225
}
2326

27+
/**
28+
* @param string $path
29+
*/
2430
public function applyVersion($path): string
2531
{
2632
$versionized = sprintf($this->format, ltrim($path, '/'), $this->hasher->computeHash($path));

src/CacheWarmer/AssetFinder.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function __construct(string $webRoot)
1717
$this->webRoot = $webRoot;
1818
}
1919

20+
/**
21+
* @return \Traversable<string>
22+
*/
2023
public function getAssetPaths(): \Traversable
2124
{
2225
$finder = (new Finder())->files()

src/CacheWarmer/HashCacheWarmer.php

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function __construct(AssetFinder $assetFinder, string $cacheFile, AssetHa
3131
$this->fallbackPool = $fallbackPool;
3232
}
3333

34+
/**
35+
* @param string $cacheDir
36+
*
37+
* @return string[]
38+
*/
3439
public function warmUp($cacheDir): array
3540
{
3641
$phpArrayPool = new PhpArrayAdapter($this->cacheFile, $this->fallbackPool);

src/DependencyInjection/IncenteevHashedAssetExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
class IncenteevHashedAssetExtension extends ConfigurableExtension
1111
{
1212
/**
13-
* {@inheritdoc}
13+
* @param array{web_root: string, version_format: string} $config
1414
*/
15-
protected function loadInternal(array $config, ContainerBuilder $container)
15+
protected function loadInternal(array $config, ContainerBuilder $container): void
1616
{
1717
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1818
$loader->load('services.xml');

src/Hashing/CachedHasher.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public function computeHash(string $path): string
2121
$item = $this->cache->getItem(base64_encode(ltrim($path, '/')));
2222

2323
if ($item->isHit()) {
24-
return $item->get();
24+
$cachedHash = $item->get();
25+
26+
if (\is_string($cachedHash)) {
27+
return $cachedHash;
28+
}
2529
}
2630

2731
$hash = $this->hasher->computeHash($path);

src/Hashing/FileHasher.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function computeHash(string $path): string
1919
return '';
2020
}
2121

22-
return substr(sha1_file($fullPath), 0, 7);
22+
$hash = sha1_file($fullPath);
23+
24+
if ($hash === false) {
25+
return '';
26+
}
27+
28+
return substr($hash, 0, 7);
2329
}
2430
}

tests/Asset/HashingVersionStrategyTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testGetVersion()
2424
/**
2525
* @dataProvider getVersionedAssets
2626
*/
27-
public function testApplyVersion($path, $expected, $hash, $format = null)
27+
public function testApplyVersion(string $path, string $expected, string $hash, ?string $format = null)
2828
{
2929
$hasher = $this->prophesize(AssetHasherInterface::class);
3030
$hasher->computeHash($path)->willReturn($hash);

tests/DependencyInjection/IncenteevHashedAssetExtensionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testNonDebugMode()
6565
$this->assertEquals('/var/html/test', $assetFinderDef->getArgument(0));
6666
}
6767

68-
private function assertHasDefinition(ContainerBuilder $containerBuilder, string $id)
68+
private function assertHasDefinition(ContainerBuilder $containerBuilder, string $id): void
6969
{
7070
$this->assertTrue($containerBuilder->hasDefinition($id), sprintf('The container has a `%s` service definition.', $id));
7171
}

tests/Hashing/FileHasherTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FileHasherTest extends TestCase
1010
/**
1111
* @dataProvider getAssetVersions
1212
*/
13-
public function testComputeHash($path, $version)
13+
public function testComputeHash(string $path, string $version)
1414
{
1515
$versionStrategy = new FileHasher(__DIR__.'/fixtures');
1616

0 commit comments

Comments
 (0)