Skip to content

Commit

Permalink
Merge pull request #63 from samsonasik/apply-php74
Browse files Browse the repository at this point in the history
Apply PHP 7.4 syntax and typed property
  • Loading branch information
Ocramius committed Sep 9, 2022
2 parents 44abfcf + 8d9bf8e commit 8d352d8
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 104 deletions.
24 changes: 12 additions & 12 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1614,13 +1614,13 @@
<code>$matches</code>
<code>$matches</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="6">
<code>static function ($matches) {</code>
<code>static function ($matches) {</code>
<code>static function ($matches) {</code>
<code>static function ($matches) {</code>
<code>static function ($matches) {</code>
<code>static function ($matches) {</code>
<MissingClosureReturnType occurrences="7">
<code>static fn($matches)</code>
<code>static fn($matches)</code>
<code>static fn($matches)</code>
<code>static fn($matches)</code>
<code>static fn($matches)</code>
<code>static fn($matches)</code>
</MissingClosureReturnType>
<MixedArgument occurrences="7">
<code>$matches[1]</code>
Expand Down Expand Up @@ -2522,11 +2522,11 @@
<code>$value</code>
<code>$value</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="4">
<code>function ($container) use ($filter) {</code>
<code>function ($container) use ($filter) {</code>
<code>function ($value) {</code>
<code>function ($value) {</code>
<MissingClosureReturnType occurrences="6">
<code>static fn($container)</code>
<code>static fn($container)</code>
<code>static fn($value)</code>
<code>static fn($value)</code>
</MissingClosureReturnType>
<MissingReturnType occurrences="1">
<code>testFactoryConfiguresPluginManagerUnderServiceManagerV2</code>
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ protected static function applyFilterOnlyToStringableValuesAndStringableArrayVal
}
return $callback((string) $value);
}
return $callback(array_map(fn($item) => is_scalar($item) ? (string) $item : $item, $value));
return $callback(array_map(static fn($item) => is_scalar($item) ? (string) $item : $item, $value));
}
}
11 changes: 3 additions & 8 deletions src/DataUnitFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function is_numeric;
use function log;
use function number_format;
use function pow;
use function sprintf;
use function strtolower;

Expand All @@ -25,10 +24,8 @@ final class DataUnitFormatter extends AbstractFilter

/**
* A list of all possible filter modes:
*
* @var array
*/
private static $modes = [
private static array $modes = [
self::MODE_BINARY,
self::MODE_DECIMAL,
];
Expand All @@ -37,10 +34,8 @@ final class DataUnitFormatter extends AbstractFilter
* A list of standardized binary prefix formats for decimal and binary mode
*
* @link https://en.wikipedia.org/wiki/Binary_prefix
*
* @var array
*/
private static $standardizedPrefixes = [
private static array $standardizedPrefixes = [
// binary IEC units:
self::MODE_BINARY => ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'],
// decimal SI units:
Expand Down Expand Up @@ -230,7 +225,7 @@ public function filter($value)
}

// return formatted value:
$result = $amount / pow($base, $power);
$result = $amount / $base ** $power;
$formatted = number_format($result, $this->getPrecision());
return $this->formatAmount($formatted, $prefix);
}
Expand Down
24 changes: 6 additions & 18 deletions src/Word/SeparatorToCamelCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,15 @@ private function filterNormalizedValue($value)
if (! extension_loaded('mbstring')) {
$replacements = [
// @codingStandardsIgnoreStart
static function ($matches) {
return strtoupper($matches[2]);
},
static function ($matches) {
return strtoupper($matches[1]);
},
static fn($matches) => strtoupper($matches[2]),
static fn($matches) => strtoupper($matches[1]),
// @codingStandardsIgnoreEnd
];
} else {
$replacements = [
// @codingStandardsIgnoreStart
static function ($matches) {
return mb_strtoupper($matches[2], 'UTF-8');
},
static function ($matches) {
return mb_strtoupper($matches[1], 'UTF-8');
},
static fn($matches) => mb_strtoupper($matches[2], 'UTF-8'),
static fn($matches) => mb_strtoupper($matches[1], 'UTF-8'),
// @codingStandardsIgnoreEnd
];
}
Expand All @@ -71,12 +63,8 @@ static function ($matches) {
];
$replacements = [
// @codingStandardsIgnoreStart
static function ($matches) {
return strtoupper($matches[2]);
},
static function ($matches) {
return strtoupper($matches[1]);
},
static fn($matches) => strtoupper($matches[2]),
static fn($matches) => strtoupper($matches[1]),
// @codingStandardsIgnoreEnd
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Word/Service/SeparatorToSeparatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SeparatorToSeparatorFactory implements FactoryInterface
*
* @param null|array
*/
private $creationOptions = [];
private array $creationOptions = [];

public function __construct($creationOptions = null)
{
Expand Down
9 changes: 6 additions & 3 deletions test/Compress/TarLoadArchiveTarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class TarLoadArchiveTarTest extends TestCase
{
public function testArchiveTarNotLoaded(): void
{
set_error_handler(function () {
set_error_handler(
static fn() =>
// PEAR class uses deprecated constructor, which emits a deprecation error
return true;
}, E_DEPRECATED);
true,
E_DEPRECATED
);

if (class_exists('Archive_Tar')) {
restore_error_handler();
$this->markTestSkipped('PEAR Archive_Tar is present; skipping test that expects its absence');
Expand Down
42 changes: 20 additions & 22 deletions test/DataUnitFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Laminas\Filter\Exception;
use PHPUnit\Framework\TestCase;

use function pow;

class DataUnitFormatterTest extends TestCase
{
/**
Expand Down Expand Up @@ -86,16 +84,16 @@ public static function decimalBytesTestProvider()
return [
[0, '0 B'],
[1, '1.00 B'],
[pow(1000, 1), '1.00 kB'],
[pow(1500, 1), '1.50 kB'],
[pow(1000, 2), '1.00 MB'],
[pow(1000, 3), '1.00 GB'],
[pow(1000, 4), '1.00 TB'],
[pow(1000, 5), '1.00 PB'],
[pow(1000, 6), '1.00 EB'],
[pow(1000, 7), '1.00 ZB'],
[pow(1000, 8), '1.00 YB'],
[pow(1000, 9), pow(1000, 9) . ' B'],
[1000 ** 1, '1.00 kB'],
[1500 ** 1, '1.50 kB'],
[1000 ** 2, '1.00 MB'],
[1000 ** 3, '1.00 GB'],
[1000 ** 4, '1.00 TB'],
[1000 ** 5, '1.00 PB'],
[1000 ** 6, '1.00 EB'],
[1000 ** 7, '1.00 ZB'],
[1000 ** 8, '1.00 YB'],
[1000 ** 9, 1000 ** 9 . ' B'],
];
}

Expand All @@ -104,16 +102,16 @@ public static function binaryBytesTestProvider()
return [
[0, '0 B'],
[1, '1.00 B'],
[pow(1024, 1), '1.00 KiB'],
[pow(1536, 1), '1.50 KiB'],
[pow(1024, 2), '1.00 MiB'],
[pow(1024, 3), '1.00 GiB'],
[pow(1024, 4), '1.00 TiB'],
[pow(1024, 5), '1.00 PiB'],
[pow(1024, 6), '1.00 EiB'],
[pow(1024, 7), '1.00 ZiB'],
[pow(1024, 8), '1.00 YiB'],
[pow(1024, 9), pow(1024, 9) . ' B'],
[1024 ** 1, '1.00 KiB'],
[1536 ** 1, '1.50 KiB'],
[1024 ** 2, '1.00 MiB'],
[1024 ** 3, '1.00 GiB'],
[1024 ** 4, '1.00 TiB'],
[1024 ** 5, '1.00 PiB'],
[1024 ** 6, '1.00 EiB'],
[1024 ** 7, '1.00 ZiB'],
[1024 ** 8, '1.00 YiB'],
[1024 ** 9, 1024 ** 9 . ' B'],
];
}
}
2 changes: 1 addition & 1 deletion test/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testFormatDateTimeFromTimestamp(): void
date_default_timezone_set('UTC');

$filter = new DateTimeFormatter();
$result = $filter->filter(1359739801);
$result = $filter->filter(1_359_739_801);
$this->assertSame('2013-02-01T17:30:01+0000', $result);
}

Expand Down
5 changes: 2 additions & 3 deletions test/File/RenameUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,14 @@ public function testStringConstructorWithPsrFile(): void
$originalStream->getMetadata('uri')->willReturn($this->sourceFile);

$originalFile = $this->prophesize(UploadedFileInterface::class);
$originalFile->getStream()->will(function ($args, $mock) use ($originalStream) {
$originalFile->getStream()->will(function ($args, $mock) use ($originalStream): object {
$mock->getStream()->willThrow(new RuntimeException('Cannot call getStream() more than once'));

return $originalStream->reveal();
});
$originalFile->getClientFilename()->willReturn($targetFile);
$originalFile
->moveTo($targetFile)
->will(function ($args) use ($sourceFile) {
->will(function ($args) use ($sourceFile): void {
$targetFile = array_shift($args);
copy($sourceFile, $targetFile);
})
Expand Down
8 changes: 2 additions & 6 deletions test/FilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function testFiltersAreExecutedAccordingToPriority(): void
public function testAllowsConnectingArbitraryCallbacks(): void
{
$chain = new FilterChain();
$chain->attach(function ($value) {
return strtolower($value);
});
$chain->attach(static fn($value) => strtolower($value));
$value = 'AbC';
$this->assertSame('abc', $chain->filter($value));
}
Expand Down Expand Up @@ -122,9 +120,7 @@ protected function getChainConfig()
['callback' => self::class . '::staticUcaseFilter'],
[
'priority' => 10000,
'callback' => function (string $value): string {
return trim($value);
},
'callback' => static fn(string $value): string => trim($value),
],
],
'filters' => [
Expand Down
16 changes: 4 additions & 12 deletions test/FilterPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public function testFactoryReturnsPluginManager(): void
public function testFactoryConfiguresPluginManagerUnderContainerInterop(): void
{
$container = $this->prophesize(ContainerInterface::class)->reveal();
$filter = function ($value) {
return $value;
};
$filter = static fn($value) => $value;

$factory = new FilterPluginManagerFactory();
$filters = $factory($container, FilterPluginManagerFactory::class, [
Expand All @@ -67,9 +65,7 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
$container = $this->prophesize(ServiceLocatorInterface::class);
$container->willImplement(ContainerInterface::class);

$filter = function ($value) {
return $value;
};
$filter = static fn($value) => $value;

$factory = new FilterPluginManagerFactory();
$factory->setCreationOptions([
Expand All @@ -91,9 +87,7 @@ public function testConfiguresFilterServicesWhenFound(): void
'test' => Boolean::class,
],
'factories' => [
'test-too' => function ($container) use ($filter) {
return $filter;
},
'test-too' => static fn($container) => $filter,
],
],
];
Expand Down Expand Up @@ -124,9 +118,7 @@ public function testDoesNotConfigureFilterServicesWhenServiceListenerPresent():
'test' => Boolean::class,
],
'factories' => [
'test-too' => function ($container) use ($filter) {
return $filter;
},
'test-too' => static fn($container) => $filter,
],
],
];
Expand Down
3 changes: 1 addition & 2 deletions test/FilterPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

class FilterPluginManagerTest extends TestCase
{
/** @var FilterPluginManager */
private $filters;
private FilterPluginManager $filters;

public function setUp(): void
{
Expand Down
7 changes: 3 additions & 4 deletions test/HtmlEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPUnit\Framework\TestCase;
use stdClass;

use function dirname;
use function file_get_contents;
use function phpversion;
use function restore_error_handler;
Expand Down Expand Up @@ -198,7 +197,7 @@ public function testCorrectsForEncodingMismatch(): void
$this->markTestIncomplete('Code to test is not compatible with PHP 5.4 ');
}

$string = file_get_contents(dirname(__FILE__) . '/_files/latin-1-text.txt');
$string = file_get_contents(__DIR__ . '/_files/latin-1-text.txt');

// restore_error_handler can emit an E_WARNING; let's ignore that, as
// we want to test the returned value
Expand All @@ -218,7 +217,7 @@ public function testStripsUnknownCharactersWhenEncodingMismatchDetected(): void
$this->markTestIncomplete('Code to test is not compatible with PHP 5.4 ');
}

$string = file_get_contents(dirname(__FILE__) . '/_files/latin-1-text.txt');
$string = file_get_contents(__DIR__ . '/_files/latin-1-text.txt');

// restore_error_handler can emit an E_WARNING; let's ignore that, as
// we want to test the returned value
Expand All @@ -238,7 +237,7 @@ public function testRaisesExceptionIfEncodingMismatchDetectedAndFinalStringIsEmp
$this->markTestIncomplete('Code to test is not compatible with PHP 5.4 ');
}

$string = file_get_contents(dirname(__FILE__) . '/_files/latin-1-dash-only.txt');
$string = file_get_contents(__DIR__ . '/_files/latin-1-dash-only.txt');

// restore_error_handler can emit an E_WARNING; let's ignore that, as
// we want to test the returned value
Expand Down
12 changes: 3 additions & 9 deletions test/StaticFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,10 @@ public function testStaticFactoryClassNotFound(): void
public function testUsesDifferentConfigurationOnEachRequest(): void
{
$first = StaticFilter::execute('foo', Callback::class, [
'callback' => function ($value) {
return 'FOO';
},
'callback' => static fn($value) => 'FOO',
]);
$second = StaticFilter::execute('foo', Callback::class, [
'callback' => function ($value) {
return 'BAR';
},
'callback' => static fn($value) => 'BAR',
]);
$this->assertNotSame($first, $second);
$this->assertSame('FOO', $first);
Expand All @@ -115,9 +111,7 @@ public function testUsesDifferentConfigurationOnEachRequest(): void
public function testThatCallablesRegisteredWithThePluginManagerCanBeExecuted(): void
{
$plugins = new FilterPluginManager(new ServiceManager());
$plugins->setService('doStuff', static function (string $value): string {
return strtoupper($value);
});
$plugins->setService('doStuff', static fn(string $value): string => strtoupper($value));

StaticFilter::setPluginManager($plugins);

Expand Down
2 changes: 1 addition & 1 deletion test/StringPrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function invalidPrefixesDataProvider()
'empty array' => [[]],
'resource' => [fopen('php://memory', 'rb+')],
'array with callable' => [
function () {
static function (): void {
},
],
'object' => [new stdClass()],
Expand Down
Loading

0 comments on commit 8d352d8

Please sign in to comment.