Skip to content

Commit

Permalink
Introduce typed properties (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 24, 2023
1 parent 88a1d39 commit e24c430
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Phan\Issue;

return [
'target_php_version' => '7.1',
'target_php_version' => '7.4',
'backward_compatibility_checks' => false,
'exclude_analysis_directory_list' => [
'vendor/',
Expand Down
51 changes: 30 additions & 21 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,49 @@
declare(strict_types=1);

$header = <<<'EOF'
Copyright 2020 Alexey Kopytko <alexey@kopytko.com>
Copyright 2020 Alexey Kopytko <alexey@kopytko.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
EOF;
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
EOF;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'header_comment' => ['comment_type' => 'PHPDoc', 'header' => $header, 'separate' => 'bottom', 'location' => 'after_open'],
'@PER-CS' => true,
'@PER-CS:risky' => true,

'@Symfony:risky' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit75Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'@PHPUnit100Migration:risky' => true,
'@PHP74Migration' => true,

'native_constant_invocation' => [
'strict' => false,
'scope' => 'namespaced',
],
'native_function_invocation' => [
'include' => ['@internal'],
'scope' => 'namespaced',
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],

'ordered_class_elements' => false,
'phpdoc_to_comment' => false,
'strict_comparison' => true,
'comment_to_phpdoc' => true,
'native_function_invocation' => ['include' => ['@internal'], 'scope' => 'namespaced'],
'php_unit_test_case_static_method_calls' => false,
'yoda_style' => true,
'array_indentation' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ PHPUNIT=vendor/bin/phpunit
PHPUNIT_COVERAGE_CLOVER=--coverage-clover=build/logs/clover.xml
PHPUNIT_GROUP=default
PHPUNIT_ARGS=--coverage-xml=build/logs/coverage-xml --log-junit=build/logs/junit.xml $(PHPUNIT_COVERAGE_CLOVER)
export XDEBUG_MODE=coverage

# Phan
PHAN=vendor/bin/phan
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<directory name="src" />
<directory name="tests/Examples" />
</projectFiles>

<issueHandlers>
<PropertyNotSetInConstructor errorLevel="suppress" />
</issueHandlers>
</psalm>
16 changes: 7 additions & 9 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,31 @@

namespace Later;

use Throwable;

/**
* Deferred: a wrapper object.
*
* @template T
*
* @template-implements Interfaces\Deferred<T>
*
* @psalm-suppress PropertyNotSetInConstructor
*
* @internal
* @final
*/
final class Deferred implements Interfaces\Deferred
class Deferred implements Interfaces\Deferred
{
/**
* @var ?iterable<T>
*/
private $input;
private ?iterable $input;

/**
* @var T
*/
private $output;

/**
* @var ?\Throwable
*/
private $error;
private ?Throwable $error;

/**
* @param iterable<T> $input
Expand Down Expand Up @@ -75,7 +73,7 @@ public function get()

break;
}
} catch (\Throwable $e) {
} catch (Throwable $e) {
$this->error = $e;

throw $e;
Expand Down
3 changes: 2 additions & 1 deletion src/Immediate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
* @template-implements Interfaces\Deferred<T>
*
* @internal
* @final
*/
final class Immediate implements Interfaces\Deferred
class Immediate implements Interfaces\Deferred
{
/**
* @var T
Expand Down
10 changes: 6 additions & 4 deletions tests/DeferredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace Tests\Later;

use Later\Deferred;
use ReflectionClass;
use InvalidArgumentException;

/**
* @covers \Later\Deferred
Expand Down Expand Up @@ -93,18 +95,18 @@ public function testThrowsSame(): void
try {
$this->assertDeferredSame(1, $later);
$this->fail('Should have thrown');
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
$this->assertSame(__CLASS__, $e->getMessage());
}

try {
$this->assertDeferredSame(1, $later);
} catch (\InvalidArgumentException $e2) {
} catch (InvalidArgumentException $e2) {
$this->assertSame($e, $e2);
}

// Make sure the input is discarded
$reflectionClass = new \ReflectionClass($later);
$reflectionClass = new ReflectionClass($later);
$property = $reflectionClass->getProperty('input');
$property->setAccessible(true);
$this->assertNull($property->getValue($later));
Expand All @@ -116,7 +118,7 @@ public function testThrowsSame(): void
private function generatorThrows(bool $throw = false): iterable
{
if ($throw) {
throw new \InvalidArgumentException(__CLASS__);
throw new InvalidArgumentException(__CLASS__);
}

yield 1;
Expand Down
3 changes: 2 additions & 1 deletion tests/Examples/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Later\Interfaces\Deferred;

use function Later\lazy;
use function usleep;

/**
* An object which used to do a lot all at once.
Expand All @@ -44,7 +45,7 @@ private function makeDependency(int $number)
$factorial = 1;

for ($i = 1; $i <= $number; ++$i) {
\usleep(100);
usleep(100);
$factorial *= $i;
}

Expand Down
14 changes: 8 additions & 6 deletions tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Tests\Later\Examples\DeepThought;
use Tests\Later\Examples\HyperIntelligentMice;

use function microtime;

/**
* @coversNothing
*
Expand Down Expand Up @@ -75,21 +77,21 @@ public function testMockCalledOnce(): void

public function testCalculator(): void
{
$start = \microtime(true);
$start = microtime(true);
$calculator = new Calculator(10);
$initTime = \microtime(true) - $start;
$initTime = microtime(true) - $start;

$start = \microtime(true);
$start = microtime(true);
$result = (string) $calculator;
$calcTime = \microtime(true) - $start;
$calcTime = microtime(true) - $start;

$this->assertStringStartsWith('3628800', $result);

$this->assertGreaterThan($initTime, $calcTime);

$start = \microtime(true);
$start = microtime(true);
$result = (string) $calculator;
$nextCalcTime = \microtime(true) - $start;
$nextCalcTime = microtime(true) - $start;

$this->assertLessThan($calcTime, $nextCalcTime);
}
Expand Down

0 comments on commit e24c430

Please sign in to comment.