Skip to content

Commit

Permalink
Fix traits issue in parallel analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 11, 2020
1 parent e39baa7 commit cedc99f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$loop = new StreamSelectLoop();

$container = $inceptionResult->getContainer();

/** @var NodeScopeResolver $nodeScopeResolver */
$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);
$nodeScopeResolver->setAnalysedFiles($inceptionResult->getFiles());

$tcpConector = new TcpConnector($loop);
$tcpConector->connect(sprintf('127.0.0.1:%d', $port))->then(function (ConnectionInterface $connection) use ($container, $identifier): void {
$out = new Encoder($connection);
Expand Down Expand Up @@ -138,19 +143,15 @@ private function runWorker(Container $container, WritableStreamInterface $out, R
/** @var Registry $registry */
$registry = $container->getByType(Registry::class);

/** @var NodeScopeResolver $nodeScopeResolver */
$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);

// todo collectErrors (from Analyser)
$in->on('data', static function (array $json) use ($fileAnalyser, $registry, $nodeScopeResolver, $out): void {
$in->on('data', static function (array $json) use ($fileAnalyser, $registry, $out): void {
$action = $json['action'];
if ($action !== 'analyse') {
return;
}

$internalErrorsCount = 0;
$files = $json['files'];
$nodeScopeResolver->setAnalysedFiles($files);
$errors = [];
$inferrablePropertyTypesFromConstructorHelper = new InferrablePropertyTypesFromConstructorHelper();
foreach ($files as $file) {
Expand Down
63 changes: 63 additions & 0 deletions tests/PHPStan/Parallel/ParallelAnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parallel;

use Nette\Utils\Json;
use PHPUnit\Framework\TestCase;

class ParallelAnalyserIntegrationTest extends TestCase
{

public function testTraitsInDifferentJobAnalysed(): void
{
exec(sprintf(
'%s %s analyse -l 8 -c %s --error-format json %s',
escapeshellarg(PHP_BINARY),
escapeshellarg(__DIR__ . '/../../../bin/phpstan'),
escapeshellarg(__DIR__ . '/parallel-analyser.neon'),
implode(' ', array_map(static function (string $path): string {
return escapeshellarg($path);
}, [
__DIR__ . '/data/trait-definition.php',
__DIR__ . '/data/traits.php',
]))
), $outputLines, $exitCode);
$output = implode("\n", $outputLines);
$this->assertJsonStringEqualsJsonString(Json::encode([
'totals' => [
'errors' => 0,
'file_errors' => 3,
],
'files' => [
sprintf('%s/data/trait-definition.php (in context of class ParallelAnalyserIntegrationTest\\Bar)', __DIR__) => [
'errors' => 1,
'messages' => [
[
'message' => 'Method ParallelAnalyserIntegrationTest\\Bar::doFoo() has no return typehint specified.',
'line' => 8,
'ignorable' => true,
],
],
],
sprintf('%s/data/trait-definition.php (in context of class ParallelAnalyserIntegrationTest\\Foo)', __DIR__) => [
'errors' => 2,
'messages' => [
[
'message' => 'Method ParallelAnalyserIntegrationTest\\Foo::doFoo() has no return typehint specified.',
'line' => 8,
'ignorable' => true,
],
[
'message' => 'Access to an undefined property ParallelAnalyserIntegrationTest\\Foo::$test.',
'line' => 10,
'ignorable' => true,
],
],
],
],
'errors' => [],
]), $output);
$this->assertSame(1, $exitCode);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Parallel/data/trait-definition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ParallelAnalyserIntegrationTest;

trait FooTrait
{

public function doFoo()
{
$this->test = 1;
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Parallel/data/traits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ParallelAnalyserIntegrationTest;

class Foo
{

use FooTrait;

}

class Bar
{

use FooTrait;

/** @var int */
private $test;

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Parallel/parallel-analyser.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
featureToggles:
parallel: true
parallel:
jobSize: 1

0 comments on commit cedc99f

Please sign in to comment.