Skip to content

Commit

Permalink
Revert PHP 7.2 support and require at least 8.1 (#14)
Browse files Browse the repository at this point in the history
* Revert "Widen PHP support (#10)"

This reverts commit 7532a89.

* Lower PHP requirement to 8.1

PHPStan 2.0 is in the works, and it will only support 8.1 and higher.

* Run PHPStan on 8.1 (lowest)

* Move readonly to class properties
  • Loading branch information
ruudk authored Oct 2, 2024
1 parent 10f85b3 commit 879be48
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 71 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ concurrency:

jobs:
phpstan:
strategy:
fail-fast: false
matrix:
php:
- '7.2'
- '8.3'
- '8.4'

runs-on: ubuntu-latest

steps:
Expand All @@ -27,7 +19,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: '8.1'

- name: Install Composer packages
uses: ramsey/composer-install@v3
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["phpstan", "dev"],
"type": "phpstan-extension",
"require": {
"php": "^7.2|^8.0",
"php": "^8.1",
"phpstan/phpstan": "^1.12.4"
},
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

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

2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ parameters:
level: 8
paths:
- src
phpVersion: 70200
phpVersion: 80100
errorFormat: ticketswap
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
90 changes: 32 additions & 58 deletions src/TicketSwapErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,24 @@ final class TicketSwapErrorFormatter implements ErrorFormatter
private const LINK_FORMAT_PHPSTORM = "↳ file://{absolutePath}:{line}\n";
private const LINK_FORMAT_WITHOUT_EDITOR = "↳ {relativePath}:{line}\n";

/**
* @var string
*/
private $linkFormat;

/**
* @var RelativePathHelper
*/
private $relativePathHelper;

/**
* @var CiDetectedErrorFormatter
*/
private $ciDetectedErrorFormatter;

/**
* @var string|null
*/
private $editorUrl;
private string $linkFormat;

public function __construct(
RelativePathHelper $relativePathHelper,
CiDetectedErrorFormatter $ciDetectedErrorFormatter,
?string $editorUrl = null
private readonly RelativePathHelper $relativePathHelper,
private readonly CiDetectedErrorFormatter $ciDetectedErrorFormatter,
private readonly ?string $editorUrl,
) {
$this->editorUrl = $editorUrl;
$this->ciDetectedErrorFormatter = $ciDetectedErrorFormatter;
$this->relativePathHelper = $relativePathHelper;
$this->linkFormat = self::getLinkFormatFromEnv();
}

public static function getLinkFormatFromEnv() : string
{
if (getenv('GITHUB_ACTIONS') !== false) {
return self::LINK_FORMAT_GITHUB_ACTIONS;
}
if (getenv('TERMINAL_EMULATOR') !== 'JetBrains-JediTerm') {
return self::LINK_FORMAT_PHPSTORM;
}
if (getenv('TERM_PROGRAM') !== 'WarpTerminal') {
return self::LINK_FORMAT_WARP;
}

return self::LINK_FORMAT_DEFAULT;
return match (true) {
getenv('GITHUB_ACTIONS') !== false => self::LINK_FORMAT_GITHUB_ACTIONS,
getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm' => self::LINK_FORMAT_PHPSTORM,
getenv('TERM_PROGRAM') === 'WarpTerminal' => self::LINK_FORMAT_WARP,
default => self::LINK_FORMAT_DEFAULT,
};
}

public function formatErrors(AnalysisResult $analysisResult, Output $output) : int
Expand All @@ -78,7 +52,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
$output->writeLineFormatted(
sprintf(
'<unknown location> %s',
$notFileSpecificError
$notFileSpecificError,
)
);
}
Expand All @@ -101,7 +75,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
$error->getTip()
) : null,
$error->getIdentifier(),
$output->isDecorated()
$output->isDecorated(),
),
'{identifier}' => $error->getIdentifier(),
'{links}' => implode([
Expand All @@ -111,19 +85,19 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
$error->getFilePath(),
$this->relativePathHelper->getRelativePath($error->getFilePath()),
$this->editorUrl,
$output->isDecorated()
$output->isDecorated(),
),
$error->getTraitFilePath() !== null ? $this::link(
$this->linkFormat,
(int) $error->getLine(),
$error->getTraitFilePath(),
$this->relativePathHelper->getRelativePath($error->getTraitFilePath()),
$this->editorUrl,
$output->isDecorated()
$output->isDecorated(),
) : '',
]),
]
)
],
),
);
}

Expand All @@ -136,7 +110,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
sprintf(
'<bg=red;options=bold>Found %d error%s</>',
$analysisResult->getTotalErrorsCount(),
$analysisResult->getTotalErrorsCount() === 1 ? '' : 's'
$analysisResult->getTotalErrorsCount() === 1 ? '' : 's',
)
);
$output->writeLineFormatted('');
Expand All @@ -152,7 +126,7 @@ public static function link(
string $absolutePath,
string $relativePath,
?string $editorUrl,
bool $isDecorated
bool $isDecorated,
) : string {
if (!$isDecorated || $editorUrl === null) {
$format = self::LINK_FORMAT_WITHOUT_EDITOR;
Expand All @@ -165,12 +139,12 @@ public static function link(
'{editorUrl}' => $editorUrl === null ? '' : str_replace(
['%relFile%', '%file%', '%line%'],
[$relativePath, $absolutePath, $line],
$editorUrl
$editorUrl,
),
'{relativePath}' => $relativePath,
'{shortPath}' => self::trimPath($relativePath),
'{line}' => $line,
]
],
);
}

Expand All @@ -183,11 +157,11 @@ private static function trimPath(string $path) : string

return implode(
DIRECTORY_SEPARATOR,
array_merge(
array_slice($parts, 0, 3),
['...'],
array_slice($parts, -2)
)
[
...array_slice($parts, 0, 3),
'...',
...array_slice($parts, -2),
],
);
}

Expand All @@ -197,7 +171,7 @@ public static function highlight(string $message, ?string $tip, ?string $identif
return $message;
}

if (strpos($message, 'Ignored error pattern') === 0) {
if (str_starts_with($message, 'Ignored error pattern')) {
return $message;
}

Expand All @@ -208,42 +182,42 @@ public static function highlight(string $message, ?string $tip, ?string $identif
$message = (string) preg_replace(
"/([A-Z0-9]{1}[A-Za-z0-9_\-]+[\\\]+[A-Z0-9]{1}[A-Za-z0-9_\-\\\]+)/",
'<fg=yellow>$1</>',
$message
$message,
);

// Quoted strings
$message = (string) preg_replace(
"/(?<=[\"'])([A-Za-z0-9_\-\\\]+)(?=[\"'])/",
'<fg=yellow>$1</>',
$message
$message,
);

// Variable
$message = (string) preg_replace(
"/(?<=[:]{2}|[\s\"\(])([.]{3})?(\\$[A-Za-z0-9_\\-]+)(?=[\s|\"|\)]|$)/",
'<fg=green>$1$2</>',
$message
$message,
);

// Method
$message = (string) preg_replace(
'/(?<=[:]{2}|[\s])(\w+\(\))/',
'<fg=blue>$1</>',
$message
$message,
);

// Function
$message = (string) preg_replace(
'/(?<=function\s)(\w+)(?=\s)/',
'<fg=blue>$1</>',
$message
$message,
);

// Types
$message = (string) preg_replace(
'/(?<=[\s\|\(><])(null|true|false|int|float|bool|([-\w]+-)?string|array|object|mixed|resource|iterable|void|callable)(?=[:]{2}|[\.\s\|><,\(\)\{\}]+)/',
'<fg=magenta>$1</>',
$message
$message,
);

if ($tip !== null) {
Expand Down

0 comments on commit 879be48

Please sign in to comment.