Skip to content

Commit

Permalink
Upgrade code to PHP 8.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
corpsee committed Feb 11, 2024
1 parent c72da77 commit 923769d
Show file tree
Hide file tree
Showing 31 changed files with 105 additions and 181 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ psalm: php-info install ## Run Psalm check
rector: php-info install ## Run Rector
$(PHP) vendor/bin/rector process --clear-cache --dry-run

.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm rector
rector-fix: php-info install ## Run Rector
$(PHP) vendor/bin/rector process --clear-cache

.PHONY: php-info list install install-force update test test-coverage mutation-test code-style-fix psalm rector rector-fix
.DEFAULT_GOAL := list
2 changes: 1 addition & 1 deletion src/CodeQuality/Pahout.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function execute(): bool

$success = true;

list($files, $hints) = $this->processReport($this->commandExecutor->getLastCommandOutput());
[$files, $hints] = $this->processReport($this->commandExecutor->getLastCommandOutput());

if (0 < \count($hints)) {
if (-1 !== $this->allowedWarnings && \count($hints) > $this->allowedWarnings) {
Expand Down
2 changes: 1 addition & 1 deletion src/CodeQuality/Phan.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function processReport(string $jsonString): int
self::getName(),
$data['check_name']."\n\n".$data['description'],
$this->severity((int)$data['severity']),
isset($data['location']['path']) ? $data['location']['path'] : '??',
$data['location']['path'] ?? '??',
isset($data['location']['lines']['begin']) ? (int)$data['location']['lines']['begin'] : 0,
isset($data['location']['lines']['end']) ? (int)$data['location']['lines']['end'] : 0
);
Expand Down
2 changes: 1 addition & 1 deletion src/CodeQuality/Phlint.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function getPluginDefaultBinaryNames(): array

private function processReport(string $output): array
{
$data = \explode(\chr(226), \preg_replace('#\\x1b[[][^A-Za-z\n]*[A-Za-z]#', '', \trim($output)));
$data = \explode(\chr(226), (string)\preg_replace('#\\x1b[[][^A-Za-z\n]*[A-Za-z]#', '', \trim($output)));
\array_pop($data);
\array_shift($data);

Expand Down
6 changes: 3 additions & 3 deletions src/CodeQuality/PhpCodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function getName(): string
*/
public function execute(): bool
{
list($ignores, $standard, $suffixes, $severity, $errorSeverity, $warningSeverity) = $this->getFlags();
[$ignores, $standard, $suffixes, $severity, $errorSeverity, $warningSeverity] = $this->getFlags();

$executable = $this->commandExecutor->findBinary($this->binaryNames, $this->binaryPath);

Expand All @@ -77,7 +77,7 @@ public function execute(): bool
);

$output = $this->commandExecutor->getLastCommandOutput();
list($errors, $warnings) = $this->processReport($output);
[$errors, $warnings] = $this->processReport($output);

$this->commandExecutor->enableCommandOutput();

Expand Down Expand Up @@ -212,7 +212,7 @@ private function processReport(string $output): array
$warnings = $data['totals']['warnings'];

foreach ($data['files'] as $fileName => $file) {
$fileName = \str_replace($this->build->getBuildPath(), '', $fileName);
$fileName = \str_replace($this->build->getBuildPath(), '', (string)$fileName);

foreach ($file['messages'] as $message) {
$this->buildErrorWriter->write(
Expand Down
2 changes: 1 addition & 1 deletion src/CodeQuality/PhpCpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function execute(): bool
$this->directory
)
);
if (false !== \strpos($lastLine, '--names-exclude')) {
if (\str_contains($lastLine, '--names-exclude')) {
$ignoresString = $ignoreForNewVersion;
}

Expand Down
21 changes: 6 additions & 15 deletions src/CodeQuality/PhpCsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function processReport(string $output): int
$warnings = 0;
foreach ($data['files'] as $item) {
$filename = $item['name'];
$appliedFixers = isset($item['appliedFixers']) ? $item['appliedFixers'] : [];
$appliedFixers = $item['appliedFixers'] ?? [];

$parser = new Parser();
$parsed = $parser->parse($item['diff']);
Expand All @@ -201,20 +201,11 @@ private function processReport(string $output): int
}
$chunkDiff = [];
foreach ($chunk->getLines() as $line) {
switch ($line->getType()) {
case Line::ADDED:
$symbol = '+';

break;
case Line::REMOVED:
$symbol = '-';

break;
default:
$symbol = ' ';

break;
}
$symbol = match ($line->getType()) {
Line::ADDED => '+',
Line::REMOVED => '-',
default => ' ',
};
$chunkDiff[] = $symbol . $line->getContent();
if ($foundChanges) {
continue;
Expand Down
4 changes: 1 addition & 3 deletions src/CodeQuality/PhpLoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function execute(): bool
{
$ignoreString = '';
if ($this->ignores) {
$map = function (string $ignore): string {
return \sprintf(' --exclude="%s"', $ignore);
};
$map = fn(string $ignore): string => \sprintf(' --exclude="%s"', $ignore);

$ignoreString = \array_map($map, $this->ignores);
$ignoreString = \implode('', $ignoreString);
Expand Down
2 changes: 1 addition & 1 deletion src/CodeQuality/PhpMessDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function getPluginDefaultBinaryNames(): array
private function processRules(): void
{
foreach ($this->rules as &$rule) {
if (false !== \strpos($rule, '/')) {
if (\str_contains((string) $rule, '/')) {
$rule = $this->build->getBuildPath() . $rule;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/CodeQuality/PhpStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function execute(): bool
$this->commandExecutor->enableCommandOutput();

$success = true;
list($total_errors, $files) = $this->processReport($this->commandExecutor->getLastCommandOutput());
[$total_errors, $files] = $this->processReport($this->commandExecutor->getLastCommandOutput());

if (0 < $total_errors) {
if (-1 !== $this->allowedErrors && $total_errors > $this->allowedErrors) {
Expand All @@ -65,18 +65,18 @@ public function execute(): bool

foreach ($files as $file => $payload) {
if (0 < $payload['errors']) {
$file = \str_replace($this->build->getBuildPath(), '', $file);
$file = \str_replace($this->build->getBuildPath(), '', (string) $file);
$len = \strlen($file);
$out = '';
$filename = (false !== \strpos($file, ' (')) ? \strstr($file, ' (', true) : $file;
$filename = (\str_contains($file, ' (')) ? \strstr($file, ' (', true) : $file;

foreach ($payload['messages'] as $message) {
if (\strlen($message['message']) > $len) {
$len = \strlen($message['message']);
if (\strlen((string)$message['message']) > $len) {
$len = \strlen((string)$message['message']);
}
$out .= \vsprintf(' %d%s %s' . PHP_EOL, [
$message['line'],
\str_repeat(' ', 6 - \strlen($message['line'])),
\str_repeat(' ', 6 - \strlen((string)$message['line'])),
$message['message'],
]);

Expand Down
2 changes: 1 addition & 1 deletion src/CodeQuality/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function execute(): bool
$this->commandExecutor->enableCommandOutput();

$success = true;
list($errors, $infos) = $this->processReport($this->commandExecutor->getLastCommandOutput());
[$errors, $infos] = $this->processReport($this->commandExecutor->getLastCommandOutput());

if (0 < \count($errors)) {
if (-1 !== $this->allowedErrors && \count($errors) > $this->allowedErrors) {
Expand Down
6 changes: 3 additions & 3 deletions src/CodeQuality/TechnicalDebt.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function getErrorList(): int
$ignoreAbsolute = $this->build->getBuildPath() . $ignore;

if ('/' === $ignoreAbsolute[0]) {
if (0 === \strpos($filePath, $ignoreAbsolute)) {
if (\str_starts_with($filePath, $ignoreAbsolute)) {
$ignored = true;

break;
Expand Down Expand Up @@ -192,8 +192,8 @@ private function returnResult(): string
$string = '';
$fileNumber = 0;
foreach ($this->errorPerFile as $oneLine) {
$fileNumber += \strlen($oneLine);
$string .= \str_pad($oneLine, 60, ' ', STR_PAD_RIGHT);
$fileNumber += \strlen((string)$oneLine);
$string .= \str_pad((string)$oneLine, 60, ' ', STR_PAD_RIGHT);
$string .= \str_pad((string)$fileNumber, 4, ' ', STR_PAD_LEFT);
$string .= "/" . $this->numberOfAnalysedFile . " (" . \floor($fileNumber * 100 / $this->numberOfAnalysedFile) . " %)\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/CopyBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function execute(): bool
$cmd,
$buildPath,
$this->directory,
\rtrim($buildPath, '/'),
\rtrim((string)$buildPath, '/'),
$this->directory
);

Expand Down
22 changes: 7 additions & 15 deletions src/Common/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,13 @@ protected function initPluginSettings(): void
*/
private function runAction(string $action, array $options = []): bool
{
switch ($action) {
case 'merge':
return $this->runMergeAction($options);

case 'tag':
return $this->runTagAction($options);

case 'pull':
return $this->runPullAction($options);

case 'push':
return $this->runPushAction($options);
}

return false;
return match ($action) {
'merge' => $this->runMergeAction($options),
'tag' => $this->runTagAction($options),
'pull' => $this->runPullAction($options),
'push' => $this->runPushAction($options),
default => false,
};
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Deploy/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ private function getMageLog(): array
throw new Exception('Log dir read fail');
}

$list = \array_filter($list, function ($name) {
return \preg_match('/^log-\d+-\d+\.log$/', $name);
});
$list = \array_filter($list, fn($name) => \preg_match('/^log-\d+-\d+\.log$/', (string)$name));

if (empty($list)) {
throw new Exception('Log dir filter fail');
Expand Down
4 changes: 1 addition & 3 deletions src/Deploy/Mage3.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ private function getMageLog(): array
throw new Exception('Log dir read fail');
}

$list = \array_filter($list, function ($name) {
return \preg_match('/^\d+_\d+\.log$/', $name);
});
$list = \array_filter($list, fn($name) => \preg_match('/^\d+_\d+\.log$/', (string)$name));

if (empty($list)) {
throw new Exception('Log dir filter fail');
Expand Down
29 changes: 11 additions & 18 deletions src/Notification/BitbucketNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,11 @@ private function updateBuild(): void
$this->build->getCommitId()
);

switch ($this->build->getStatus()) {
case BuildInterface::STATUS_SUCCESS:
$state = 'SUCCESSFUL';

break;
case BuildInterface::STATUS_FAILED:
$state = 'FAILED';

break;
default:
$state = 'INPROGRESS';
}
$state = match ($this->build->getStatus()) {
BuildInterface::STATUS_SUCCESS => 'SUCCESSFUL',
BuildInterface::STATUS_FAILED => 'FAILED',
default => 'INPROGRESS',
};

$this->buildStatusRequest($endpoint, 'post', [
'state' => $state,
Expand Down Expand Up @@ -286,8 +279,8 @@ private function prepareResult(string $targetBranch): array
foreach ($plugins as $plugin) {
$result[] = new PluginResult(
$plugin,
isset($targetBranchBuildStats[$plugin]) ? $targetBranchBuildStats[$plugin] : 0,
isset($currentBranchBuildStats[$plugin]) ? $currentBranchBuildStats[$plugin] : 0
$targetBranchBuildStats[$plugin] ?? 0,
$currentBranchBuildStats[$plugin] ?? 0
);
}

Expand Down Expand Up @@ -321,18 +314,18 @@ private function getPhpUnitCoverage(string $targetBranch): PluginResult

$targetBranchCoverage = [];
if ($latestTargetBuildId && $targetMetaData) {
$targetBranchCoverage = \json_decode($targetMetaData->getValue(), true);
$targetBranchCoverage = \json_decode((string)$targetMetaData->getValue(), true);
}

$currentBranchCoverage = [];
if (!\is_null($currentMetaData)) {
$currentBranchCoverage = \json_decode($currentMetaData->getValue(), true);
$currentBranchCoverage = \json_decode((string)$currentMetaData->getValue(), true);
}

return new PluginResult(
PhpUnit::getName() . '-' . BuildMetaInterface::KEY_COVERAGE,
isset($targetBranchCoverage['lines']) ? $targetBranchCoverage['lines'] : 0,
isset($currentBranchCoverage['lines']) ? $currentBranchCoverage['lines'] : 0
$targetBranchCoverage['lines'] ?? 0,
$currentBranchCoverage['lines'] ?? 0
);
}

Expand Down
21 changes: 7 additions & 14 deletions src/Notification/BitbucketNotify/PluginResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@
* @author Dmitry Khomutov <[email protected]>
* @author Eugen Ganshorn <[email protected]>
*/
class PluginResult
class PluginResult implements \Stringable
{
public const DEFAULT_PLUGIN_OUTPUT_FORMAT = "%s | %d\t=> %d\t%s";

protected string $plugin;
protected string $outputFormat = self::DEFAULT_PLUGIN_OUTPUT_FORMAT;

protected int $left;

protected int $right;

protected string $outputFormat;

public function __construct(string $plugin, int $left, int $right)
{
$this->plugin = $plugin;
$this->left = $left;
$this->right = $right;
$this->outputFormat = self::DEFAULT_PLUGIN_OUTPUT_FORMAT;
public function __construct(
protected string $plugin,
protected int $left,
protected int $right
) {
}

public function getPlugin(): string
Expand Down
6 changes: 3 additions & 3 deletions src/Notification/EmailNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function execute(): bool

try {
$view = $this->getMailTemplate();
} catch (Exception $e) {
} catch (Exception) {
$this->buildLogger->logWarning(
\sprintf('Unknown mail template "%s", falling back to default.', (string)$this->options->get('template'))
\sprintf('Unknown mail template "%s", falling back to default.', $this->options->get('template'))
);
$view = $this->getDefaultMailTemplate();
}
Expand Down Expand Up @@ -201,7 +201,7 @@ private function getEmailAddresses(): array

$this->buildLogger->logDebug(\sprintf(
"Default mailTo option: '%s'",
$this->defaultMailtoAddress ? $this->defaultMailtoAddress : 'false'
$this->defaultMailtoAddress ?: 'false'
));

if (empty($addresses) && $this->defaultMailtoAddress) {
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/TelegramNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private function buildMessage(): string
$this->buildMsg = '';
$buildIcon = $this->build->isSuccessful() ? '' : '';
$buildLog = $this->build->getLog();
$buildLog = \str_replace(['[0;32m', '[0;31m', '[0m', '/[0m'], '', $buildLog);
$buildLog = \str_replace(['[0;32m', '[0;31m', '[0m', '/[0m'], '', (string)$buildLog);
$buildMessages = \explode('RUNNING PLUGIN: ', $buildLog);

foreach ($buildMessages as $bm) {
Expand Down
Loading

0 comments on commit 923769d

Please sign in to comment.