Skip to content

Commit

Permalink
Merge pull request #21 from stof/phpstan
Browse files Browse the repository at this point in the history
Add phpstan
  • Loading branch information
stof committed Mar 30, 2022
2 parents 9ca930c + 1f310ae commit 17f5bd3
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 7 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ jobs:
php-version: '8.0'
- run: composer validate --strict --no-check-lock

static_analysis:
name: Static analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.0'
- name: Install dependencies
run: composer update --ansi --no-progress --prefer-dist --no-interaction
- run: vendor/bin/phpstan analyze

tests:
name: "Tests on PHP ${{ matrix.php }}${{ matrix.name_suffix }}"
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
"symfony/http-kernel": "^4.4.13 || ^5.3 || ^6.0"
},
"require-dev": {
"jangregor/phpstan-prophecy": "^1.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.5",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-symfony": "^1.1",
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^5.3 || ^6.0"
},
Expand Down
16 changes: 16 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
level: max
paths:
- src/
- tests/
ignoreErrors:
- '#^Method Incenteev\\HashedAssetBundle\\Tests\\[^:]++\:\:test\w++\(\) has no return type specified\.$#'
- '#^Method Incenteev\\HashedAssetBundle\\Tests\\[^:]++\:\:get\w++\(\) has no return type specified\.$#'

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
- vendor/jangregor/phpstan-prophecy/extension.neon
6 changes: 6 additions & 0 deletions src/Asset/HashingVersionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public function __construct(AssetHasherInterface $hasher, string $format = null)
$this->hasher = $hasher;
}

/**
* @param string $path
*/
public function getVersion($path): string
{
return $this->hasher->computeHash($path);
}

/**
* @param string $path
*/
public function applyVersion($path): string
{
$versionized = sprintf($this->format, ltrim($path, '/'), $this->hasher->computeHash($path));
Expand Down
3 changes: 3 additions & 0 deletions src/CacheWarmer/AssetFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function __construct(string $webRoot)
$this->webRoot = $webRoot;
}

/**
* @return \Traversable<string>
*/
public function getAssetPaths(): \Traversable
{
$finder = (new Finder())->files()
Expand Down
5 changes: 5 additions & 0 deletions src/CacheWarmer/HashCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct(AssetFinder $assetFinder, string $cacheFile, AssetHa
$this->fallbackPool = $fallbackPool;
}

/**
* @param string $cacheDir
*
* @return string[]
*/
public function warmUp($cacheDir): array
{
$phpArrayPool = new PhpArrayAdapter($this->cacheFile, $this->fallbackPool);
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/IncenteevHashedAssetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class IncenteevHashedAssetExtension extends ConfigurableExtension
{
/**
* {@inheritdoc}
* @param array{web_root: string, version_format: string} $config
*/
protected function loadInternal(array $config, ContainerBuilder $container)
protected function loadInternal(array $config, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
6 changes: 5 additions & 1 deletion src/Hashing/CachedHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public function computeHash(string $path): string
$item = $this->cache->getItem(base64_encode(ltrim($path, '/')));

if ($item->isHit()) {
return $item->get();
$cachedHash = $item->get();

if (\is_string($cachedHash)) {
return $cachedHash;
}
}

$hash = $this->hasher->computeHash($path);
Expand Down
8 changes: 7 additions & 1 deletion src/Hashing/FileHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function computeHash(string $path): string
return '';
}

return substr(sha1_file($fullPath), 0, 7);
$hash = sha1_file($fullPath);

if ($hash === false) {
return '';
}

return substr($hash, 0, 7);
}
}
2 changes: 1 addition & 1 deletion tests/Asset/HashingVersionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testGetVersion()
/**
* @dataProvider getVersionedAssets
*/
public function testApplyVersion($path, $expected, $hash, $format = null)
public function testApplyVersion(string $path, string $expected, string $hash, ?string $format = null)
{
$hasher = $this->prophesize(AssetHasherInterface::class);
$hasher->computeHash($path)->willReturn($hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testNonDebugMode()
$this->assertEquals('/var/html/test', $assetFinderDef->getArgument(0));
}

private function assertHasDefinition(ContainerBuilder $containerBuilder, string $id)
private function assertHasDefinition(ContainerBuilder $containerBuilder, string $id): void
{
$this->assertTrue($containerBuilder->hasDefinition($id), sprintf('The container has a `%s` service definition.', $id));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Hashing/FileHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FileHasherTest extends TestCase
/**
* @dataProvider getAssetVersions
*/
public function testComputeHash($path, $version)
public function testComputeHash(string $path, string $version)
{
$versionStrategy = new FileHasher(__DIR__.'/fixtures');

Expand Down

0 comments on commit 17f5bd3

Please sign in to comment.