Compare commits

...

2 Commits

Author SHA1 Message Date
Timothy Warren
8bcc4c6807 Start experimenting with PHP7.4 transpiling via rector
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good
2021-04-23 14:38:42 -04:00
Timothy Warren
a5223adfa5 Refactor matches to be transpilable 2021-04-23 14:34:08 -04:00
8 changed files with 3689 additions and 69 deletions

View File

@ -17,6 +17,7 @@
"ext-json": "*",
"phpunit/phpunit": "^9.5.0",
"phpstan/phpstan": "^0.12.19",
"rector/rector": "^0.10.9",
"spatie/phpunit-snapshot-assertions": "^4.2.0"
},
"scripts": {
@ -27,6 +28,6 @@
},
"require": {
"ext-ffi": "*",
"php": ">= 8.0.0"
"php": ">= 7.4.0"
}
}

3669
composer.lock generated

File diff suppressed because it is too large Load Diff

54
rector.php Normal file
View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
use Rector\DowngradePhp80\Rector\ClassConstFetch\DowngradeClassOnObjectToGetClassRector;
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStaticTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector;
use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Rector\Generics\Rector\Class_\GenericsPHPStormMethodAnnotationRector;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
// $parameters = $containerConfigurator->parameters();
//
// // Define what rule sets will be applied
// $parameters->set(Option::SETS, [
// SetList::DEAD_CODE,
// SetList::PHP_80,
// ]);
// get services (needed for register a single rule)
$services = $containerConfigurator->services();
$rules = [
// PHP8 downgrade
DowngradeClassOnObjectToGetClassRector::class,
DowngradeMatchToSwitchRector::class,
DowngradeMixedTypeDeclarationRector::class,
DowngradeNonCapturingCatchesRector::class,
DowngradeNullsafeToTernaryOperatorRector::class,
DowngradePropertyPromotionRector::class,
DowngradeStaticTypeDeclarationRector::class,
DowngradeTrailingCommasInParamUseRector::class,
DowngradeUnionTypeDeclarationRector::class,
DowngradeUnionTypeTypedPropertyRector::class,
GenericsPHPStormMethodAnnotationRector::class,
];
foreach ($rules as $rule)
{
$services->set($rule);
}
};

View File

@ -33,7 +33,7 @@ class Highlight {
*/
public static function fromPHPToken(int $token): int
{
return match($token) {
$token = match($token) {
// Delimiters
T_ARRAY,
T_CURLY_OPEN,
@ -186,6 +186,8 @@ class Highlight {
default => Highlight::NORMAL,
};
return $token;
}
/**
@ -197,7 +199,7 @@ class Highlight {
*/
public static function fromPHPChar(string $char): int
{
return match ($char) {
$hl = match ($char) {
// Delimiter characters
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
@ -207,5 +209,7 @@ class Highlight {
default => Highlight::NORMAL,
};
return $hl;
}
}

View File

@ -26,7 +26,7 @@ class FileType {
{
$ext = strstr(basename($filename), '.');
$ext = ($ext !== FALSE) ? $ext : '';
return match ($ext) {
$syntax = match ($ext) {
'.sh', '.bash' => Syntax::new(
'Shell',
[
@ -127,6 +127,8 @@ class FileType {
),
default => Syntax::default(),
};
return $syntax;
}
private function __construct(public string $name, public Syntax $syntax) {}

View File

@ -76,13 +76,15 @@ class Row {
public function __get(string $name): mixed
{
return match ($name)
$prop = match ($name)
{
'size' => strlen($this->chars),
'rsize' => strlen($this->render),
'chars' => $this->chars,
default => NULL,
};
return $prop;
}
/**

View File

@ -87,7 +87,7 @@ class Terminal {
{
$c = Terminal::read();
return match($c)
$key = match($c)
{
// Unambiguous mappings
RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
@ -113,6 +113,8 @@ class Terminal {
default => $c,
};
return $key;
}
/**

View File

@ -76,7 +76,7 @@ function is_digit(string $char): bool
*/
function is_space(string $char): bool
{
return match($char) {
$isSpace = match($char) {
RawKeyCode::CARRIAGE_RETURN,
RawKeyCode::FORM_FEED,
RawKeyCode::NEWLINE,
@ -86,6 +86,8 @@ function is_space(string $char): bool
default => false,
};
return $isSpace;
}
// ----------------------------------------------------------------------------
@ -158,7 +160,7 @@ function str_has(string $haystack, string $str, ?int $offset = NULL): bool
*/
function syntax_to_color(int $hl): int
{
return match ($hl)
$color = match ($hl)
{
Highlight::COMMENT => Color::FG_CYAN,
Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK,
@ -175,6 +177,8 @@ function syntax_to_color(int $hl): int
Highlight::IDENTIFIER => Color::FG_BRIGHT_WHITE,
default => Color::FG_WHITE,
};
return $color;
}
/**
@ -191,7 +195,7 @@ function tabs_to_spaces(string $str, int $number = KILO_TAB_STOP): string
function error_code_name(int $code): string
{
return match ($code) {
$errorName = match ($code) {
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
@ -208,6 +212,8 @@ function error_code_name(int $code): string
E_USER_DEPRECATED => 'User Deprecated',
default => 'Unknown',
};
return $errorName;
}
function saturating_add(int $a, int $b, int $max): int