Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PSalm level 2 (#508)
  • Loading branch information
Slamdunk committed Aug 17, 2020
1 parent 5bed557 commit 1ac67ec
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 141 deletions.
2 changes: 1 addition & 1 deletion psalm.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
35 changes: 13 additions & 22 deletions src/Coverage/CoverageMerger.php
Expand Up @@ -7,16 +7,16 @@
use RuntimeException;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData;
use SplFileObject;

use function array_map;
use function array_slice;
use function assert;
use function extension_loaded;
use function file_exists;
use function filesize;
use function function_exists;
use function ini_get;
use function is_array;
use function is_file;
use function unlink;

use const PHP_SAPI;
Expand Down Expand Up @@ -44,32 +44,20 @@ private function addCoverage(CodeCoverage $coverage): void
$this->limitCoverageTests($this->coverage);
}

/**
* Returns coverage object from file.
*
* @param SplFileObject $coverageFile coverage file
*/
private function getCoverageObject(SplFileObject $coverageFile): CodeCoverage
{
return include $coverageFile->getRealPath();
}

/**
* Adds the coverage contained in $coverageFile and deletes the file afterwards.
*
* @param string $coverageFile Code coverage file
*
* @throws RuntimeException When coverage file is empty.
*/
public function addCoverageFromFile(?string $coverageFile = null): void
public function addCoverageFromFile(?string $coverageFile): void
{
if ($coverageFile === null || ! file_exists($coverageFile)) {
if ($coverageFile === null || ! is_file($coverageFile)) {
return;
}

$file = new SplFileObject($coverageFile);

if ($file->getSize() === 0) {
if (filesize($coverageFile) === 0) {
$extra = 'This means a PHPUnit process has crashed.';

$xdebug = function_exists('xdebug_get_code_coverage');
Expand All @@ -81,11 +69,12 @@ public function addCoverageFromFile(?string $coverageFile = null): void
}

throw new RuntimeException(
"Coverage file {$file->getRealPath()} is empty. " . $extra
"Coverage file $coverageFile is empty. " . $extra
);
}

$this->addCoverage($this->getCoverageObject($file));
/** @psalm-suppress UnresolvableInclude **/
$this->addCoverage(include $coverageFile);

unlink($coverageFile);
}
Expand Down Expand Up @@ -114,15 +103,17 @@ private function limitCoverageTests(CodeCoverage $coverage): void
return;
}

$testLimit = $this->test_limit;
$data = $coverage->getData(true);
$newData = array_map(
function (array $lines): array {
return array_map(function ($value) {
static function (array $lines) use ($testLimit): array {
/** @psalm-suppress MissingClosureReturnType **/
return array_map(static function ($value) use ($testLimit) {
if (! is_array($value)) {
return $value;
}

return array_slice($value, 0, $this->test_limit);
return array_slice($value, 0, $testLimit);
}, $lines);
},
$data->lineCoverage(),
Expand Down
4 changes: 2 additions & 2 deletions src/Logging/JUnit/Reader.php
Expand Up @@ -269,8 +269,8 @@ protected function getMessages(string $type): array
foreach ($suites as $suite) {
$messages = array_merge(
$messages,
array_reduce($suite->cases, static function ($result, TestCase $case) use ($type): array {
return array_merge($result, array_reduce($case->$type, static function ($msgs, $msg): array {
array_reduce($suite->cases, static function (array $result, TestCase $case) use ($type): array {
return array_merge($result, array_reduce($case->$type, static function (array $msgs, array $msg): array {
$msgs[] = $msg['text'];

return $msgs;
Expand Down
3 changes: 3 additions & 0 deletions src/Parser/Parser.php
Expand Up @@ -58,7 +58,10 @@ public function __construct(string $srcPath)

$this->path = $srcPath;
$declaredClasses = get_declared_classes();

/** @psalm-suppress UnresolvableInclude **/
require_once $this->path;

$class = $this->getClassName($this->path, $declaredClasses);
if ($class === null) {
throw new NoClassInFileException();
Expand Down
8 changes: 4 additions & 4 deletions src/Runners/PHPUnit/BaseRunner.php
Expand Up @@ -49,11 +49,11 @@ abstract class BaseRunner implements RunnerInterface
/** @var OutputInterface */
protected $output;

public function __construct(Options $opts, OutputInterface $output)
public function __construct(Options $options, OutputInterface $output)
{
$this->options = $opts;
$this->options = $options;
$this->interpreter = new LogInterpreter();
$this->printer = new ResultPrinter($this->interpreter, $output);
$this->printer = new ResultPrinter($this->interpreter, $output, $options);
$this->output = $output;
}

Expand Down Expand Up @@ -164,6 +164,6 @@ final protected function initialize(): void
{
$this->initCoverage();
$this->load(new SuiteLoader($this->options));
$this->printer->start($this->options);
$this->printer->start();
}
}
4 changes: 2 additions & 2 deletions src/Runners/PHPUnit/BaseWrapperRunner.php
Expand Up @@ -13,10 +13,10 @@ abstract class BaseWrapperRunner extends BaseRunner
private const PHPUNIT_ERRORS = 2;

/** @var resource[] */
protected $streams;
protected $streams = [];

/** @var resource[] */
protected $modified;
protected $modified = [];

final protected function beforeLoadChecks(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Runners/PHPUnit/EmptyRunnerStub.php
Expand Up @@ -10,7 +10,7 @@ final class EmptyRunnerStub extends BaseRunner

public function run(): void
{
$this->printer->start($this->options);
$this->printer->start();
$this->output->write(self::OUTPUT);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Runners/PHPUnit/ExecutableTest.php
Expand Up @@ -27,14 +27,14 @@ abstract class ExecutableTest
* A path to the temp file created
* for this test.
*
* @var string
* @var string|null
*/
protected $temp;

/**
* Path where the coveragereport is stored.
*
* @var string
* @var string|null
*/
protected $coverageFileName;

Expand Down

0 comments on commit 1ac67ec

Please sign in to comment.