Skip to content

Commit

Permalink
Adding psalm, have a validation on code coverage, php 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
batenburg committed Jan 10, 2021
1 parent af2c083 commit 7fa7ed1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0

env:
global:
Expand Down Expand Up @@ -62,6 +63,7 @@ jobs:
php: 7.4
script:
- ./vendor/bin/phpcs
- ./vendor/bin/psalm

- stage: Coverage
php: 7.4
Expand All @@ -73,4 +75,4 @@ jobs:
script:
- ./vendor/bin/phpunit -v --coverage-clover ./build/logs/clover.xml
after_script:
- php ./vendor/bin/php-coveralls -v
- php coverage-checker.php clover.xml 100
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.0",
"php-coveralls/php-coveralls": "^2.4",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.3"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 30 additions & 0 deletions coverage-checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
$inputFile = $argv[1];
$percentage = min(100, max(0, (int) $argv[2]));

if (!file_exists($inputFile)) {
throw new InvalidArgumentException('Invalid input file provided');
}

if (!$percentage) {
throw new InvalidArgumentException('An integer checked percentage must be given as second parameter');
}

$xml = new SimpleXMLElement(file_get_contents($inputFile));
$metrics = $xml->xpath('//metrics');
$totalElements = 0;
$checkedElements = 0;

foreach ($metrics as $metric) {
$totalElements += (int) $metric['elements'];
$checkedElements += (int) $metric['coveredelements'];
}

$coverage = ($checkedElements / $totalElements) * 100;

if ($coverage < $percentage) {
echo 'Code coverage is ' . $coverage . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL;
exit(1);
}

echo 'Code coverage is ' . $coverage . '% - OK!' . PHP_EOL;
55 changes: 55 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PropertyNotSetInConstructor>

<PossiblyUndefinedMethod>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyUndefinedMethod>

<PossiblyInvalidArgument>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyInvalidArgument>

<MismatchingDocblockPropertyType>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</MismatchingDocblockPropertyType>

<MixedMethodCall>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</MixedMethodCall>

<InvalidThrow>
<errorLevel type="suppress">
<file name="src/Repository/CacheRepository.php"/>
<file name="tests/Unit/Repository/CacheRepositoryTest.php"/>
</errorLevel>
</InvalidThrow>
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/DependencyInjection/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CacheExtension extends Extension
* @param ContainerBuilder $container
* @throws Exception
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
Expand Down
10 changes: 7 additions & 3 deletions tests/Unit/Repository/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testWhenTheItemIsRetrievedFromTheCacheItWillNotSaveToTheCache():
{
// Setup
$key = 'test.key';
$closure = function () {
$closure = function (): string {
return 'item';
};
$this->mockGetItem(1, [$key]);
Expand All @@ -187,7 +187,7 @@ public function testWhenAnItemIsNotInCacheTheRememberItemWillSaveAndReturnIt():
// Setup
$key = 'test.key';
$expected = 'expected result';
$closure = function () use ($expected) {
$closure = function () use ($expected): string {
return $expected;
};
$this->mockGetItem(2, [$key], [$key]);
Expand All @@ -209,7 +209,7 @@ public function testARememberItemCanBeStoredWithACustomExpiresAfter(): void
// Setup
$key = 'test.key';
$expected = 'expected result';
$closure = function () use ($expected) {
$closure = function () use ($expected): string {
return $expected;
};
$expiresAfterInSeconds = 600;
Expand Down Expand Up @@ -360,6 +360,10 @@ public function fakeFindByIdWithArgument(int $id, string $argument): string
return $id . $argument;
}

/**
* @param int $count
* @param mixed ...$keys
*/
private function mockGetItem(int $count, ...$keys): void
{
$this->cacheAdapter->expects($this->exactly($count))
Expand Down

0 comments on commit 7fa7ed1

Please sign in to comment.