Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rector] Add Rector to CI for PHP upgrades code to PHP 8 #3876

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,37 @@ jobs:
- name: Static analysis with PHPStan
run: ./vendor/bin/phpstan analyse

rector:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
coverage: none
tools: cs2pr

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Static analysis with Rector
run: ./vendor/bin/rector process --dry-run

coverage:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@
"mitoteam/jpgraph": "^10.3",
"mpdf/mpdf": "^8.1.1",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan": "^1.10.56",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.6",
"rector/rector": "^0.19.2",
"squizlabs/php_codesniffer": "^3.7",
"tecnickcom/tcpdf": "^6.5"
},
Expand Down
68 changes: 62 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 5 additions & 19 deletions infra/LocaleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@ class LocaleGenerator
private const FUNCTION_NAME_LIST_FIRST_ROW = 4;
private const ENGLISH_FUNCTION_CATEGORIES_COLUMN = 'A';
private const ENGLISH_REFERENCE_COLUMN = 'B';
private const EOL = "\n"; // not PHP_EOL

protected string $translationSpreadsheetName;

protected string $translationBaseFolder;

protected array $phpSpreadsheetFunctions;
// not PHP_EOL
private const EOL = "\n";

protected Spreadsheet $translationSpreadsheet;

protected bool $verbose;

protected Worksheet $localeTranslations;

protected array $localeLanguageMap = [];
Expand All @@ -50,16 +44,8 @@ class LocaleGenerator

protected array $functionNameMap = [];

public function __construct(
string $translationBaseFolder,
string $translationSpreadsheetName,
array $phpSpreadsheetFunctions,
bool $verbose = false
) {
$this->translationBaseFolder = $translationBaseFolder;
$this->translationSpreadsheetName = $translationSpreadsheetName;
$this->phpSpreadsheetFunctions = $phpSpreadsheetFunctions;
$this->verbose = $verbose;
public function __construct(protected string $translationBaseFolder, protected string $translationSpreadsheetName, protected array $phpSpreadsheetFunctions, protected bool $verbose = false)
{
}

public function generateLocales(): void
Expand Down Expand Up @@ -174,7 +160,7 @@ protected function buildFunctionsFileForLocale(string $column, string $locale):
}
if ($this->isFunctionCategoryEntry($translationCell)) {
$this->writeFileSectionHeader($functionFile, "{$translationValue} ({$functionName})");
} elseif (!array_key_exists($functionName, $this->phpSpreadsheetFunctions) && substr($functionName, 0, 1) !== '*') {
} elseif (!array_key_exists($functionName, $this->phpSpreadsheetFunctions) && !str_starts_with($functionName, '*')) {
$this->log("Function {$functionName} is not defined in PhpSpreadsheet");
} elseif (!empty($translationValue)) {
$functionTranslation = "{$functionName} = {$translationValue}" . self::EOL;
Expand Down
24 changes: 24 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/infra',
__DIR__ . '/samples',
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
AddLiteralSeparatorToNumberRector::class,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
/** Define a Read Filter class implementing IReadFilter */
class ChunkReadFilter implements IReadFilter
{
private int $startRow;

private int $endRow;

/**
* We expect a list of the rows that we want to read to be passed into the constructor.
*/
public function __construct(int $startRow, int $chunkSize)
public function __construct(private int $startRow, int $chunkSize)
{
$this->startRow = $startRow;
$this->endRow = $startRow + $chunkSize;
$this->endRow = $this->startRow + $chunkSize;
}

public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
Expand Down
67 changes: 32 additions & 35 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ class Calculation
/** Constants */
/** Regular Expressions */
// Numeric operand
const CALCULATION_REGEXP_NUMBER = '[-+]?\d*\.?\d+(e[-+]?\d+)?';
public const CALCULATION_REGEXP_NUMBER = '[-+]?\d*\.?\d+(e[-+]?\d+)?';
// String operand
const CALCULATION_REGEXP_STRING = '"(?:[^"]|"")*"';
public const CALCULATION_REGEXP_STRING = '"(?:[^"]|"")*"';
// Opening bracket
const CALCULATION_REGEXP_OPENBRACE = '\(';
public const CALCULATION_REGEXP_OPENBRACE = '\(';
// Function (allow for the old @ symbol that could be used to prefix a function, but we'll ignore it)
const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?(?:_xlws\.)?([\p{L}][\p{L}\p{N}\.]*)[\s]*\(';
public const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?(?:_xlws\.)?([\p{L}][\p{L}\p{N}\.]*)[\s]*\(';
// Strip xlfn and xlws prefixes from function name
const CALCULATION_REGEXP_STRIP_XLFN_XLWS = '/(_xlfn[.])?(_xlws[.])?(?=[\p{L}][\p{L}\p{N}\.]*[\s]*[(])/';
public const CALCULATION_REGEXP_STRIP_XLFN_XLWS = '/(_xlfn[.])?(_xlws[.])?(?=[\p{L}][\p{L}\p{N}\.]*[\s]*[(])/';
// Cell reference (cell or range of cells, with or without a sheet reference)
const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])';
public const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])';
// Cell reference (with or without a sheet reference) ensuring absolute/relative
const CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?(\$?\b[a-z]{1,3})(\$?\d{1,7})(?![\w.])';
const CALCULATION_REGEXP_COLUMN_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\".(?:[^\"]|\"[^!])?\"))!)?(\$?[a-z]{1,3})):(?![.*])';
const CALCULATION_REGEXP_ROW_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?(\$?[1-9][0-9]{0,6})):(?![.*])';
public const CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?(\$?\b[a-z]{1,3})(\$?\d{1,7})(?![\w.])';
public const CALCULATION_REGEXP_COLUMN_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\".(?:[^\"]|\"[^!])?\"))!)?(\$?[a-z]{1,3})):(?![.*])';
public const CALCULATION_REGEXP_ROW_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?(\$?[1-9][0-9]{0,6})):(?![.*])';
// Cell reference (with or without a sheet reference) ensuring absolute/relative
// Cell ranges ensuring absolute/relative
const CALCULATION_REGEXP_COLUMNRANGE_RELATIVE = '(\$?[a-z]{1,3}):(\$?[a-z]{1,3})';
const CALCULATION_REGEXP_ROWRANGE_RELATIVE = '(\$?\d{1,7}):(\$?\d{1,7})';
public const CALCULATION_REGEXP_COLUMNRANGE_RELATIVE = '(\$?[a-z]{1,3}):(\$?[a-z]{1,3})';
public const CALCULATION_REGEXP_ROWRANGE_RELATIVE = '(\$?\d{1,7}):(\$?\d{1,7})';
// Defined Names: Named Range of cells, or Named Formulae
const CALCULATION_REGEXP_DEFINEDNAME = '((([^\s,!&%^\/\*\+<>=-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)';
public const CALCULATION_REGEXP_DEFINEDNAME = '((([^\s,!&%^\/\*\+<>=-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)';
// Structured Reference (Fully Qualified and Unqualified)
const CALCULATION_REGEXP_STRUCTURED_REFERENCE = '([\p{L}_\\\\][\p{L}\p{N}\._]+)?(\[(?:[^\d\]+-])?)';
public const CALCULATION_REGEXP_STRUCTURED_REFERENCE = '([\p{L}_\\\\][\p{L}\p{N}\._]+)?(\[(?:[^\d\]+-])?)';
// Error
const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?';
public const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?';

/** constants */
const RETURN_ARRAY_AS_ERROR = 'error';
const RETURN_ARRAY_AS_VALUE = 'value';
const RETURN_ARRAY_AS_ARRAY = 'array';
public const RETURN_ARRAY_AS_ERROR = 'error';
public const RETURN_ARRAY_AS_VALUE = 'value';
public const RETURN_ARRAY_AS_ARRAY = 'array';

const FORMULA_OPEN_FUNCTION_BRACE = '(';
const FORMULA_CLOSE_FUNCTION_BRACE = ')';
const FORMULA_OPEN_MATRIX_BRACE = '{';
const FORMULA_CLOSE_MATRIX_BRACE = '}';
const FORMULA_STRING_QUOTE = '"';
public const FORMULA_OPEN_FUNCTION_BRACE = '(';
public const FORMULA_CLOSE_FUNCTION_BRACE = ')';
public const FORMULA_OPEN_MATRIX_BRACE = '{';
public const FORMULA_CLOSE_MATRIX_BRACE = '}';
public const FORMULA_STRING_QUOTE = '"';

private static string $returnArrayAsType = self::RETURN_ARRAY_AS_VALUE;

Expand All @@ -74,11 +74,6 @@ class Calculation
*/
private static ?Calculation $instance = null;

/**
* Instance of the spreadsheet this Calculation Engine is using.
*/
private ?Spreadsheet $spreadsheet;

/**
* Calculation cache.
*/
Expand Down Expand Up @@ -2874,9 +2869,11 @@ public static function getExcelConstants(string $key): bool|null
],
];

public function __construct(?Spreadsheet $spreadsheet = null)
{
$this->spreadsheet = $spreadsheet;
public function __construct(/**
* Instance of the spreadsheet this Calculation Engine is using.
*/
private ?Spreadsheet $spreadsheet = null
) {
Comment on lines +2872 to +2876
Copy link
Member

@PowerKiKi PowerKiKi Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't great. It looks ugly, even more so with multiple parameters, and it is not supported by any IDE AFAIK. Instead it should be a proper @param annotation IMHO.

EDIT: I'd rather ignore the entire rector if it cannot produce adequate result, which is exactly what I did in the past, or fix it manually.

$this->cyclicReferenceStack = new CyclicReferenceStack();
$this->debugLog = new Logger($this->cyclicReferenceStack);
$this->branchPruner = new BranchPruner($this->branchPruningEnabled);
Expand Down Expand Up @@ -3129,7 +3126,7 @@ public function setLocale(string $locale): bool
// Search for a file with a list of function names for locale
try {
$functionNamesFile = $this->getLocaleFile($localeDir, $locale, $language, 'functions');
} catch (Exception $e) {
} catch (Exception) {
return false;
}

Expand Down Expand Up @@ -3273,10 +3270,10 @@ private static function translateFormula(array $from, array $to, string $formula
}

/** @var ?array */
private static ?array $functionReplaceFromExcel;
private static ?array $functionReplaceFromExcel = null;

/** @var ?array */
private static ?array $functionReplaceToLocale;
private static ?array $functionReplaceToLocale = null;

/**
* @deprecated 1.30.0 use translateFormulaToLocale() instead
Expand Down Expand Up @@ -3322,10 +3319,10 @@ public function translateFormulaToLocale(string $formula): string
}

/** @var ?array */
private static ?array $functionReplaceFromLocale;
private static ?array $functionReplaceFromLocale = null;

/** @var ?array */
private static ?array $functionReplaceToExcel;
private static ?array $functionReplaceToExcel = null;

/**
* @deprecated 1.30.0 use translateFormulaToEnglish() instead
Expand Down
Loading
Loading