Skip to content

Commit

Permalink
[TASK] Apply PHP 8.2 adjustments
Browse files Browse the repository at this point in the history
Kudos to rector/rector
  • Loading branch information
a-r-m-i-n committed Dec 28, 2023
1 parent 73f31f4 commit ec01550
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 97 deletions.
6 changes: 3 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class Application extends SingleCommandApplication
{
private Scanner $scanner;
private readonly Scanner $scanner;

private string $version;

Expand Down Expand Up @@ -69,7 +69,7 @@ public function __construct(string $name = 'ec', Scanner $scanner = null)
->addOption('uncovered', 'u', InputOption::VALUE_NONE, 'When set, all files which are not covered by .editorconfig get listed')
->addOption('no-progress', '', InputOption::VALUE_NONE, 'When set, no progress indicator is displayed')
->addOption('no-error-on-exit', '', InputOption::VALUE_NONE, 'When set, the CLI tool will always return code 0, also when issues have been found')
->setCode([$this, 'executing'])
->setCode($this->executing(...))
;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ protected function executing(Input $input, Output $output): int
/** @var string|null $gitOnlyCommand */
$gitOnlyCommand = $input->getOption('git-only-cmd');

$finder = $finder ?? FinderUtility::createByFinderOptions($finderOptions, $gitOnlyEnabled ? $gitOnlyCommand : null);
$finder ??= FinderUtility::createByFinderOptions($finderOptions, $gitOnlyEnabled ? $gitOnlyCommand : null);

// Check amount of files to scan and ask for confirmation
if ($finderConfigPath) {
Expand Down
4 changes: 1 addition & 3 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public static function compile(): void
$phar->startBuffering();

// Sort files by real path
$finderSort = function (\SplFileInfo $a, \SplFileInfo $b) {
return strcmp(str_replace('\\', '/', $a->getRealPath()), str_replace('\\', '/', $b->getRealPath()));
};
$finderSort = fn (\SplFileInfo $a, \SplFileInfo $b) => strcmp(str_replace('\\', '/', $a->getRealPath()), str_replace('\\', '/', $b->getRealPath()));

$finder = new Finder();
$finder->files()
Expand Down
2 changes: 1 addition & 1 deletion src/EditorConfig/Rules/File/CharsetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CharsetRule extends Rule
{
private string $expectedEncoding;
private readonly string $expectedEncoding;

public function __construct(string $filePath, string $fileContent, string $expectedEncoding)
{
Expand Down
4 changes: 2 additions & 2 deletions src/EditorConfig/Rules/File/EndOfLineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class EndOfLineRule extends Rule
{
private string $endOfLine;
private string $expectedEndOfLine;
private readonly string $endOfLine;
private readonly string $expectedEndOfLine;

public function __construct(string $filePath, string $fileContent, string $endOfLine)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EditorConfig/Rules/File/InsertFinalNewLineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class InsertFinalNewLineRule extends Rule
{
private string $newLineFormat;
private readonly string $newLineFormat;

public function __construct(string $filePath, string $fileContent, string $newLineFormat = null)
{
Expand Down
10 changes: 5 additions & 5 deletions src/EditorConfig/Rules/File/TrimTrailingWhitespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

class TrimTrailingWhitespaceRule extends Rule
{
private bool $insertFinalNewLine;

public function __construct(string $filePath, string $fileContent, bool $insertFinalNewLine)
{
$this->insertFinalNewLine = $insertFinalNewLine;
public function __construct(
string $filePath,
string $fileContent,
private readonly bool $insertFinalNewLine
) {
parent::__construct($filePath, $fileContent);
}

Expand Down
28 changes: 10 additions & 18 deletions src/EditorConfig/Rules/FileResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@

namespace Armin\EditorconfigCli\EditorConfig\Rules;

class FileResult
class FileResult implements \Stringable
{
private string $filePath;

/**
* @var array|Rule[]
*/
private array $rules;

private bool $isBinary;

/**
* @var array|UnfixableException[]
*/
private array $unfixableExceptions = [];

public function __construct(string $filePath, array $rules, bool $isBinary = false)
{
$this->rules = $rules;
$this->filePath = $filePath;
$this->isBinary = $isBinary;
/**
* @param Rule[] $rules
*/
public function __construct(
private readonly string $filePath,
private readonly array $rules,
private readonly bool $isBinary = false
) {
foreach ($this->rules as $rule) {
if ($rule->getFilePath() !== $this->filePath) {
throw new \InvalidArgumentException(sprintf('Given rules in FileResult must all be related to the same file! Rule expects file "%s" but file "%s" is given.', $this->filePath, $rule->getFilePath()));
Expand Down Expand Up @@ -69,9 +63,7 @@ public function getErrors(): array
array_push($errors, ...$rule->getErrors());
}

uasort($errors, function (RuleError $a, RuleError $b): int {
return $a->getLine() > $b->getLine() ? 1 : -1;
});
uasort($errors, fn (RuleError $a, RuleError $b): int => $a->getLine() > $b->getLine() ? 1 : -1);

return $errors;
}
Expand Down
19 changes: 10 additions & 9 deletions src/EditorConfig/Rules/Line/IndentionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@

class IndentionRule extends Rule
{
private string $style;
private ?int $size;
private bool $strict;

public function __construct(string $filePath, string $fileContent, string $style, ?int $size, bool $strict = false)
{
private readonly string $style;

public function __construct(
string $filePath,
string $fileContent,
string $style,
private readonly ?int $size,
private readonly bool $strict = false
) {
$this->style = strtolower($style);
if (!in_array($this->style, ['space', 'tab'], true)) {
throw new \InvalidArgumentException(sprintf('Unknown indention style value "%s" given in .editorconfig', $style), 1621325864);
}
$this->size = $size;
$this->strict = $strict;
parent::__construct($filePath, $fileContent);
}

Expand Down Expand Up @@ -52,7 +53,7 @@ protected function validate(string $content): void
$this->addError($lineCount, 'Expected indention style "tab" but found "spaces"');
$lineValid = false;
}
if ('space' === $this->style && false !== strpos($whitespaces, "\t")) {
if ('space' === $this->style && str_contains($whitespaces, "\t")) {
$this->addError($lineCount, 'Expected indention style "space" but found "tabs"');
$lineValid = false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/EditorConfig/Rules/Line/MaxLineLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

class MaxLineLengthRule extends Rule
{
private ?int $maxLineLength;

public function __construct(string $filePath, string $fileContent = null, int $maxLineLength = null)
{
$this->maxLineLength = $maxLineLength;
public function __construct(
string $filePath,
string $fileContent = null,
private readonly ?int $maxLineLength = null
) {
parent::__construct($filePath, $fileContent);
}

Expand Down
9 changes: 4 additions & 5 deletions src/EditorConfig/Rules/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ abstract class Rule implements RuleInterface
public const MAX_LINE_LENGTH = 'max_line_length';
public const TRIM_TRAILING_WHITESPACE = 'trim_trailing_whitespace';

protected string $filePath;

/**
* @var array|RuleError[]
*/
Expand All @@ -36,9 +34,10 @@ public static function getDefinitions(): array
];
}

public function __construct(string $filePath, string $fileContent = null)
{
$this->filePath = $filePath;
public function __construct(
protected string $filePath,
string $fileContent = null
) {
$this->validate($fileContent ?? (string)file_get_contents($this->filePath));
}

Expand Down
14 changes: 5 additions & 9 deletions src/EditorConfig/Rules/RuleError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

namespace Armin\EditorconfigCli\EditorConfig\Rules;

class RuleError
readonly class RuleError implements \Stringable
{
private string $message;

private ?int $line;

public function __construct(string $message, int $line = null)
{
$this->message = $message;
$this->line = $line;
public function __construct(
private string $message,
private ?int $line = null
) {
}

public function getLine(): int
Expand Down
2 changes: 1 addition & 1 deletion src/EditorConfig/Rules/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function createValidatedFileResult(SplFileInfo $file, array $editorConfig
if ($this->hasRuleSet(Rule::TAB_WIDTH)) {
$width = $editorConfig[Rule::TAB_WIDTH]->getValue();
}
$size = $size ?? $width;
$size ??= $width;
if (!$size) {
$size = 4;
}
Expand Down
25 changes: 11 additions & 14 deletions src/EditorConfig/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@

class Scanner
{
private ?string $rootPath;

/**
* @var array|string[]
*/
private array $skippingRules;

private EditorConfig $editorConfig;

private Validator $validator;
private readonly EditorConfig $editorConfig;
private readonly Validator $validator;

/**
* @var array|string[]
Expand All @@ -36,12 +28,17 @@ class Scanner
*/
private array $unavailableFiles = [];

public function __construct(EditorConfig $editorConfig = null, Validator $validator = null, string $rootPath = null, array $skippingRules = [])
{
/**
* @param string[] $skippingRules
*/
public function __construct(
EditorConfig $editorConfig = null,
Validator $validator = null,
private ?string $rootPath = null,
private array $skippingRules = []
) {
$this->editorConfig = $editorConfig ?? new EditorConfig();
$this->validator = $validator ?? new Validator();
$this->rootPath = $rootPath;
$this->skippingRules = $skippingRules;
}

public function setRootPath(?string $rootPath): void
Expand Down
8 changes: 3 additions & 5 deletions src/EditorConfig/Utility/FinderUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ protected static function buildGitOnlyFinder(string $workingDir, string $gitOnly
$iterator = [];
foreach ($result as $item) {
// Check for quotepath, containing octal values for special chars
if ('"' === substr($item, 0, 1) && '"' === substr($item, -1)) {
if (str_starts_with($item, '"') && str_ends_with($item, '"')) {
$item = substr($item, 1, -1);
// Convert octal values to special chars
$item = preg_replace_callback('/\\\\(\d{3})/', static function ($matches) {
return chr((int)octdec($matches[1]));
}, $item);
$item = preg_replace_callback('/\\\\(\d{3})/', static fn ($matches) => chr((int)octdec((string)$matches[1])), $item);
}

$iterator[] = new SplFileInfo($workingDir . '/' . $item, $item ?? '', $item ?? '');
Expand Down Expand Up @@ -93,7 +91,7 @@ public static function loadCustomFinderInstance(
$finder = require $finderConfigPath;
if (!is_object($finder) || !$finder instanceof Finder) {
if (is_object($finder)) {
$returnType = 'instance of ' . get_class($finder);
$returnType = 'instance of ' . $finder::class;
} else {
$returnType = gettype($finder);
}
Expand Down
32 changes: 16 additions & 16 deletions src/EditorConfig/Utility/MimeTypeUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public static function guessMimeType(string $filePath): string
public static function isCommonTextType(string $filePath): bool
{
$mimeType = self::guessMimeType($filePath);
if (0 === strpos($mimeType, 'text/')) {
if (str_starts_with($mimeType, 'text/')) {
return true;
}

if (0 === strpos($mimeType, 'application/')) {
if ('script' === substr($mimeType, -strlen('script'))) {
if (str_starts_with($mimeType, 'application/')) {
if (str_ends_with($mimeType, 'script')) {
return true;
}
if ('json' === substr($mimeType, -strlen('json'))) {
if (str_ends_with($mimeType, 'json')) {
return true;
}
if ('yaml' === substr($mimeType, -strlen('yaml'))) {
if (str_ends_with($mimeType, 'yaml')) {
return true;
}
if ('xml' === substr($mimeType, -strlen('xml'))) {
if (str_ends_with($mimeType, 'xml')) {
return true;
}
if ('sql' === substr($mimeType, -strlen('sql'))) {
if (str_ends_with($mimeType, 'sql')) {
return true;
}
}
Expand All @@ -44,19 +44,19 @@ public static function isCommonTextType(string $filePath): bool
public static function isCommonBinaryType(string $filePath): bool
{
$mimeType = self::guessMimeType($filePath);
if (0 === strpos($mimeType, 'font/')) {
if (str_starts_with($mimeType, 'font/')) {
return true;
}
if ('image/svg' !== $mimeType && 0 === strpos($mimeType, 'image/')) {
if ('image/svg' !== $mimeType && str_starts_with($mimeType, 'image/')) {
return true;
}
if (0 === strpos($mimeType, 'audio/')) {
if (str_starts_with($mimeType, 'audio/')) {
return true;
}
if (0 === strpos($mimeType, 'video/')) {
if (str_starts_with($mimeType, 'video/')) {
return true;
}
if (0 === strpos($mimeType, 'application/vnd.')) {
if (str_starts_with($mimeType, 'application/vnd.')) {
return true;
}
if ('application/pdf' === $mimeType) {
Expand All @@ -83,14 +83,14 @@ public static function isCommonBinaryType(string $filePath): bool
if ('application/wasm' === $mimeType) {
return true;
}
if (0 === strpos($mimeType, 'application/')) {
if ('-compressed' === substr($mimeType, -strlen('-compressed'))) {
if (str_starts_with($mimeType, 'application/')) {
if (str_ends_with($mimeType, '-compressed')) {
return true;
}
if ('-ttf' === substr($mimeType, -strlen('-ttf'))) {
if (str_ends_with($mimeType, '-ttf')) {
return true;
}
if ('-archive' === substr($mimeType, -strlen('-archive'))) {
if (str_ends_with($mimeType, '-archive')) {
return true;
}
}
Expand Down

0 comments on commit ec01550

Please sign in to comment.