Skip to content

Commit

Permalink
Add missing iterable type hints (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Jul 27, 2020
1 parent 14a1146 commit ce9268f
Show file tree
Hide file tree
Showing 43 changed files with 323 additions and 191 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ jobs:
run: "vendor/bin/phpstan analyse --no-progress --error-format=checkstyle | cs2pr"

- name: "Run PSalm for type-coverage"
run: "vendor/bin/psalm --no-progress --shepherd --show-info=false --stats"
run: |
vendor/bin/psalm --no-progress --show-info=false > /dev/null || true
vendor/bin/psalm --no-progress --shepherd --show-info=false --stats
compile-phar:
name: "Compile Phar"
Expand Down
41 changes: 32 additions & 9 deletions bin/phpunit-sqlite-wrapper.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
<?php
if (!isset($argv[1])) {

declare(strict_types=1);

if (! isset($argv[1])) {
fwrite(STDERR, 'First parameter for sqlite database file required.');
exit(1);
}

$db = new PDO('sqlite:' . $argv[1]);

if (file_exists($dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) {
// git working copy
require_once $dir;
} elseif (file_exists($dir = dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'autoload.php')) {
// Composer installation
require_once $dir;
$composerAutoloadFiles = [
dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'autoload.php',
dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
];

foreach ($composerAutoloadFiles as $file) {
if (file_exists($file)) {
require_once $file;
define('PHPUNIT_COMPOSER_INSTALL', $file);

break;
}
}

while ($test = $db->query('SELECT id, command FROM tests WHERE reserved_by_process_id IS NULL ORDER BY file_name LIMIT 1')->fetch()) {
$statement = $db->prepare('UPDATE tests SET reserved_by_process_id = :procId WHERE id = :id AND reserved_by_process_id IS NULL');
$selectQuery = '
SELECT id, command
FROM tests
WHERE reserved_by_process_id IS NULL
ORDER BY file_name
LIMIT 1
';
$reserveTest = '
UPDATE tests
SET reserved_by_process_id = :procId
WHERE id = :id AND reserved_by_process_id IS NULL
';

while (($test = $db->query($selectQuery)->fetch()) !== false) {
$statement = $db->prepare($reserveTest);
$statement->execute([
':procId' => getmypid(),
':id' => $test['id'],
Expand Down
46 changes: 26 additions & 20 deletions bin/phpunit-wrapper.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

declare(strict_types=1);

echo "Worker starting\n";

$composerAutoloadFiles = [
dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'autoload.php',
dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
];

foreach ($composerAutoloadFiles as $file) {
Expand All @@ -16,18 +19,19 @@
}
}

$commands = fopen('php://stdin', 'r');
$lastExitCode = 0;
$rand = rand ( 0 , 999999 );
$uniqueTestToken = getenv("UNIQUE_TEST_TOKEN") ?: "no_unique_test_token";
$testToken = getenv("TEST_TOKEN") ?: "no_test_token";
$filename = "paratest_t-{$testToken}_ut-{$uniqueTestToken}_r-{$rand}.log";
$path = sys_get_temp_dir()."/".$filename;
$loggingEnabled = getenv("PT_LOGGING_ENABLE");
$logInfo = function(string $info) use ($path, $loggingEnabled){
if(!$loggingEnabled){
$commands = fopen('php://stdin', 'r');
$lastExitCode = 0;
$rand = mt_rand(0, 999999);
$uniqueTestToken = getenv('UNIQUE_TEST_TOKEN') ?: 'no_unique_test_token';
$testToken = getenv('TEST_TOKEN') ?: 'no_test_token';
$filename = "paratest_t-{$testToken}_ut-{$uniqueTestToken}_r-{$rand}.log";
$path = sys_get_temp_dir() . '/' . $filename;
$loggingEnabled = getenv('PT_LOGGING_ENABLE');
$logInfo = static function (string $info) use ($path, $loggingEnabled): void {
if (! $loggingEnabled) {
return;
}

file_put_contents($path, $info, FILE_APPEND | LOCK_EX);
};

Expand All @@ -37,33 +41,35 @@
if (feof($commands)) {
exit($lastExitCode);
}
$command = (fgets($commands));

$command = fgets($commands);
if ($command === false) {
exit($lastExitCode);
}

$command = rtrim($command);
if ($command === 'EXIT') {
echo "EXITED\n";
exit($lastExitCode);
}

$arguments = unserialize($command);
$command = implode(' ', $arguments);
$command = implode(' ', $arguments);
echo "Executing: $command\n";

$info = [];
$info[] = "Time: ".(new \DateTime())->format(DateTime::RFC3339);
$info[] = "Iteration: $i";
$info[] = "Command: $command";
$info[] = PHP_EOL;
$infoText = implode(PHP_EOL,$info).PHP_EOL;
$info = [];
$info[] = 'Time: ' . (new DateTime())->format(DateTime::RFC3339);
$info[] = "Iteration: $i";
$info[] = "Command: $command";
$info[] = PHP_EOL;
$infoText = implode(PHP_EOL, $info) . PHP_EOL;
$logInfo($infoText);

$_SERVER['argv'] = $arguments;

ob_start();
$lastExitCode = PHPUnit\TextUI\Command::main(false);
$infoText = ob_get_clean();
$infoText = ob_get_clean();
$logInfo($infoText);

echo "FINISHED\n";
Expand Down
10 changes: 0 additions & 10 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@

<rule ref="Doctrine"/>

<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification">
<severity>0</severity>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification">
<severity>0</severity>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification">
<severity>0</severity>
</rule>

<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition">
<severity>0</severity>
</rule>
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ includes:

parameters:
tipsOfTheDay: false
checkMissingIterableValueType: false
bootstrapFiles:
- test/constants.php
paths:
Expand All @@ -22,6 +21,10 @@ parameters:
path: src/Runners/PHPUnit/SqliteRunner.php

# Known fixtures
-
message: "#^Property ParaTest\\\\Runners\\\\PHPUnit\\\\ExecutableTest\\:\\:\\$process type has no value type specified in iterable type Symfony\\\\Component\\\\Process\\\\Process\\.$#"
count: 1
path: src/Runners/PHPUnit/ExecutableTest.php
-
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with false will always evaluate to false\\.$#"
count: 1
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Testers/PHPUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function displayHelp(InputInterface $input, OutputInterface $output):
}

/**
* @return array
* @return array<string, string|string[]>
*
* @throws RuntimeException
*/
Expand Down Expand Up @@ -228,7 +228,7 @@ protected function scopedRequire(string $file): void
/**
* Return whether or not code coverage information should be collected.
*
* @param array $options
* @param array<string, string> $options
*/
protected function hasCoverage(array $options): bool
{
Expand All @@ -245,7 +245,7 @@ protected function hasCoverage(array $options): bool
/**
* Fetch the path to the bootstrap file.
*
* @param array $options
* @param array<string, string> $options
*/
protected function getBootstrapFile(InputInterface $input, array $options): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Testers/Tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract public function execute(InputInterface $input, OutputInterface $output)
/**
* Returns non-empty options.
*
* @return array
* @return array<string, string>
*/
protected function getOptions(InputInterface $input): array
{
Expand Down
56 changes: 30 additions & 26 deletions src/Logging/JUnit/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Reader extends MetaProvider
/** @var string */
protected $logFile;

/** @var array */
/** @var array<string, int|string> */
protected static $defaultSuite = [
'name' => '',
'file' => '',
Expand Down Expand Up @@ -90,7 +90,7 @@ public function getSuites(): array
* characters: .,F,E
* TODO: Update this, skipped was added in phpunit.
*
* @return array
* @return string[]
*/
public function getFeedback(): array
{
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function init(): void
/**
* Uses an array of testcase nodes to build a suite.
*
* @param array $nodeArray an array of SimpleXMLElement nodes representing testcase elements
* @param SimpleXMLElement[] $nodeArray an array of SimpleXMLElement nodes representing testcase elements
*/
protected function initSuiteFromCases(array $nodeArray): void
{
Expand All @@ -157,8 +157,8 @@ protected function initSuiteFromCases(array $nodeArray): void
* Creates and adds a TestSuite based on the given
* suite properties and collection of test cases.
*
* @param array $properties
* @param array $testCases
* @param array<string, int|string|float> $properties
* @param TestCase[] $testCases
*/
protected function addSuite(array $properties, array $testCases): void
{
Expand All @@ -170,35 +170,39 @@ protected function addSuite(array $properties, array $testCases): void
/**
* Fold an array of testcase nodes into a suite array.
*
* @param array $nodeArray an array of testcase nodes
* @param array $testCases an array reference. Individual testcases will be placed here.
* @param SimpleXMLElement[] $nodeArray an array of testcase nodes
* @param TestCase[] $testCases an array reference. Individual testcases will be placed here.
*
* @return mixed
* @return array<string, int|string>
*/
protected function caseNodesToSuiteProperties(array $nodeArray, array &$testCases = [])
protected function caseNodesToSuiteProperties(array $nodeArray, array &$testCases = []): array
{
$cb = [TestCase::class, 'caseFromNode'];

return array_reduce($nodeArray, static function ($result, $c) use (&$testCases, $cb) {
$testCases[] = call_user_func_array($cb, [$c]);
$result['name'] = (string) $c['class'];
$result['file'] = (string) $c['file'];
++$result['tests'];
$result['assertions'] += (int) $c['assertions'];
$result['failures'] += count($c->xpath('failure'));
$result['errors'] += count($c->xpath('error'));
$result['skipped'] += count($c->xpath('skipped'));
$result['time'] += (float) $c['time'];

return $result;
}, static::$defaultSuite);
return array_reduce(
$nodeArray,
static function (array $result, SimpleXMLElement $c) use (&$testCases, $cb): array {
$testCases[] = call_user_func_array($cb, [$c]);
$result['name'] = (string) $c['class'];
$result['file'] = (string) $c['file'];
++$result['tests'];
$result['assertions'] += (int) $c['assertions'];
$result['failures'] += count($c->xpath('failure'));
$result['errors'] += count($c->xpath('error'));
$result['skipped'] += count($c->xpath('skipped'));
$result['time'] += (float) $c['time'];

return $result;
},
static::$defaultSuite
);
}

/**
* Return a collection of testcase nodes
* from the xml document.
*
* @return array
* @return array<string, SimpleXMLElement[]>
*/
protected function getCaseNodes(): array
{
Expand Down Expand Up @@ -249,7 +253,7 @@ protected function getNumericValue(string $property)
/**
* Return messages for a given type.
*
* @return array
* @return string[]
*/
protected function getMessages(string $type): array
{
Expand All @@ -258,8 +262,8 @@ protected function getMessages(string $type): array
foreach ($suites as $suite) {
$messages = array_merge(
$messages,
array_reduce($suite->cases, static function ($result, $case) use ($type) {
return array_merge($result, array_reduce($case->$type, static function ($msgs, $msg) {
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 {
$msgs[] = $msg['text'];

return $msgs;
Expand Down
20 changes: 4 additions & 16 deletions src/Logging/JUnit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,16 @@ class TestCase
/** @var string|float (a stringified float, from phpunit XML output) */
public $time;

/**
* List of failures in this test case.
*
* @var array
*/
/** @var array<int, array{type: string, text: string}> */
public $failures = [];

/**
* List of errors in this test case.
*
* @var array
*/
/** @var array<int, array{type: string, text: string}> */
public $errors = [];

/**
* List of warnings in this test case.
*
* @var array
*/
/** @var array<int, array{type: string, text: string}> */
public $warnings = [];

/** @var array */
/** @var array<int, array{type: string, text: string}> */
public $skipped = [];

public function __construct(
Expand Down
4 changes: 2 additions & 2 deletions src/Logging/JUnit/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TestSuite
/**
* Cases belonging to this suite.
*
* @var array
* @var TestCase[]
*/
public $cases = [];

Expand Down Expand Up @@ -75,7 +75,7 @@ public function __construct(
* Create a TestSuite from an associative
* array.
*
* @param array $arr
* @param array<string, int|string|float> $arr
*
* @return TestSuite
*/
Expand Down
Loading

0 comments on commit ce9268f

Please sign in to comment.