Skip to content

Commit 33eabba

Browse files
authored
Merge pull request #1 from williameggers/drop-php56-php7-support
Drop support for PHP 5.6 and PHP 7
2 parents bae5a04 + 3a7765c commit 33eabba

12 files changed

+251
-117
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor/
22
.php_cs.cache
33
composer.lock
4+
.devcontainer
5+
.php-cs-fixer.cache
6+
.phpunit.result.cache

.php-cs-fixer.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()->in(__DIR__);
4+
5+
return (new PhpCsFixer\Config())
6+
->setRules([
7+
'@PSR12' => true,
8+
'array_syntax' => [
9+
'syntax' => 'short',
10+
],
11+
'binary_operator_spaces' => [
12+
'operators' => [
13+
'=>' => 'align',
14+
'=' => 'align',
15+
],
16+
],
17+
'concat_space' => [
18+
'spacing' => 'one',
19+
],
20+
'declare_strict_types' => true,
21+
'type_declaration_spaces' => true,
22+
'single_line_comment_style' => [
23+
'comment_types' => ['hash'],
24+
],
25+
'lowercase_cast' => true,
26+
'class_attributes_separation' => [
27+
'elements' => ['method' => 'one', 'property' => 'one', 'const' => 'one'],
28+
],
29+
'native_function_casing' => true,
30+
'new_with_parentheses' => true,
31+
'no_alias_functions' => true,
32+
'no_blank_lines_after_class_opening' => true,
33+
'no_blank_lines_after_phpdoc' => true,
34+
'no_empty_comment' => true,
35+
'no_empty_phpdoc' => true,
36+
'no_empty_statement' => true,
37+
'no_extra_blank_lines' => true,
38+
'no_leading_import_slash' => true,
39+
'no_leading_namespace_whitespace' => true,
40+
'no_multiline_whitespace_around_double_arrow' => true,
41+
'multiline_whitespace_before_semicolons' => false,
42+
'no_short_bool_cast' => true,
43+
'no_singleline_whitespace_before_semicolons' => true,
44+
'no_spaces_around_offset' => true,
45+
'no_trailing_comma_in_singleline' => true,
46+
'no_unreachable_default_argument_value' => true,
47+
'no_unused_imports' => true,
48+
'no_useless_else' => true,
49+
'no_useless_return' => true,
50+
'no_whitespace_before_comma_in_array' => true,
51+
'object_operator_without_whitespace' => true,
52+
'ordered_imports' => true,
53+
'phpdoc_align' => true,
54+
'phpdoc_indent' => true,
55+
'general_phpdoc_tag_rename' => true,
56+
'phpdoc_inline_tag_normalizer' => true,
57+
'phpdoc_tag_type' => true,
58+
'phpdoc_order' => true,
59+
'phpdoc_scalar' => true,
60+
'phpdoc_separation' => true,
61+
'phpdoc_single_line_var_spacing' => true,
62+
'phpdoc_summary' => true,
63+
'phpdoc_to_comment' => true,
64+
'phpdoc_trim' => true,
65+
'phpdoc_types' => true,
66+
'self_accessor' => true,
67+
'short_scalar_cast' => true,
68+
'single_quote' => true,
69+
'space_after_semicolon' => true,
70+
'standardize_not_equals' => true,
71+
'trailing_comma_in_multiline' => [
72+
'elements' => ['arrays'],
73+
],
74+
'trim_array_spaces' => true,
75+
'unary_operator_spaces' => true,
76+
'whitespace_after_comma_in_array' => true,
77+
])
78+
->setRiskyAllowed(true)
79+
->setUsingCache(true)
80+
->setFinder($finder);

.php_cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
6-
- hhvm
4+
- 8.1
5+
- 8.2
6+
- 8.3
7+
- 8.4
78

89
cache:
910
directories:
@@ -20,6 +21,4 @@ script:
2021
- composer test
2122

2223
matrix:
23-
fast_finish: true
24-
allow_failures:
25-
- php: hhvm
24+
fast_finish: true

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [2.0.0] - 2025-08-13
6+
### Changed
7+
* Minimum supported PHP version is now `8.1`.
8+
59
## [1.0.0] - 2016-09-09
610
### Changed
711
* Moved `TomPHP\ExceptionConstructorTools\ExceptionConstructorTools` to

composer.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
}
1515
],
1616
"require": {
17-
"php": "^5.6|^7.0"
17+
"php": ">=8.1"
1818
},
1919
"require-dev": {
20-
"friendsofphp/php-cs-fixer": "^1.10",
21-
"phpunit/phpunit": "^5.5",
22-
"squizlabs/php_codesniffer": "*"
20+
"friendsofphp/php-cs-fixer": "^3.85",
21+
"phpstan/phpstan": "^2.1",
22+
"phpunit/phpunit": "^9.6 || ^7.5 || ^11.5",
23+
"rector/rector": "^2.1",
24+
"squizlabs/php_codesniffer": "^3.9"
2325
},
2426
"autoload": {
2527
"psr-4": {
@@ -44,8 +46,18 @@
4446
"phpcs --standard=psr2 src tests",
4547
"php-cs-fixer fix --dry-run --verbose"
4648
],
49+
"analyse": [
50+
"phpstan analyse --memory-limit=1G"
51+
],
52+
"rector": [
53+
"rector process src tests --ansi"
54+
],
55+
"rector:dry": [
56+
"rector process src tests --dry-run --ansi"
57+
],
4758
"test": [
4859
"@cs:check",
60+
"@analyse",
4961
"phpunit --colors=always"
5062
]
5163
}

phpstan.neon.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: max
3+
4+
paths:
5+
- src/
6+
- tests/
7+
8+
treatPhpDocTypesAsCertain: false

rector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\LevelSetList;
7+
use Rector\Set\ValueObject\SetList;
8+
9+
return RectorConfig::configure()
10+
->withPaths([
11+
__DIR__ . '/src',
12+
__DIR__ . '/tests',
13+
])
14+
// Ensure Rector knows we are targeting PHP 8.1+ syntax and features
15+
->withPhpSets(php81: true)
16+
->withPreparedSets(
17+
typeDeclarations: true, // Add param/return/var types where possible
18+
earlyReturn: true, // Replace nested ifs with early returns
19+
strictBooleans: true, // Use strict bool checks
20+
phpunitCodeQuality: true, // Modernise PHPUnit usage
21+
deadCode: true // Remove unused code, vars, imports, etc.
22+
)
23+
->withSets([
24+
LevelSetList::UP_TO_PHP_81, // Enforces all rules up to PHP 8.1
25+
SetList::CODE_QUALITY,
26+
SetList::CODING_STYLE,
27+
SetList::DEAD_CODE,
28+
SetList::NAMING, // Enforce clear & consistent naming
29+
SetList::TYPE_DECLARATION, // Add missing type declarations aggressively
30+
SetList::PRIVATIZATION, // Make props/methods private if possible
31+
SetList::STRICT_BOOLEANS, // Strict boolean expressions
32+
])->withPHPStanConfigs([
33+
__DIR__ . '/phpstan.neon.dist',
34+
]);

src/ExceptionConstructorTools.php

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TomPHP;
46

57
/**
@@ -11,69 +13,64 @@ trait ExceptionConstructorTools
1113
/**
1214
* Create an instance of the exception with a formatted message.
1315
*
14-
* @param string $message The exception message in sprintf format.
15-
* @param array $params The sprintf parameters for the message.
16-
* @param int $code Numeric exception code.
17-
* @param \Exception $previous The previous exception.
18-
*
19-
* @return static
16+
* @param string $message The exception message in sprintf format.
17+
* @param array<bool|float|int|string|null> $params The sprintf parameters for the message.
18+
* @param int $code Numeric exception code.
19+
* @param \Exception $exception The previous exception.
2020
*/
2121
protected static function create(
22-
$message,
22+
string $message,
2323
array $params = [],
24-
$code = 0,
25-
\Exception $previous = null
26-
) {
27-
return new static(sprintf($message, ...$params), $code, $previous);
24+
int $code = 0,
25+
?\Exception $exception = null
26+
): static {
27+
$class = static::class;
28+
$reflectionClass = new \ReflectionClass($class);
29+
return $reflectionClass->newInstance(sprintf($message, ...$params), $code, $exception);
2830
}
2931

3032
/**
3133
* Returns a string representation of the type of a variable.
32-
*
33-
* @param mixed $variable
34-
*
35-
* @return string
3634
*/
37-
protected static function typeToString($variable)
35+
protected static function typeToString(mixed $variable): string
3836
{
3937
return is_object($variable)
40-
? get_class($variable)
38+
? $variable::class
4139
: '[' . gettype($variable) . ']';
4240
}
4341

4442
/**
4543
* Returns a string representation of the value.
46-
*
47-
* @param mixed $value
48-
*
49-
* @return string
5044
*/
51-
protected static function valueToString($value)
45+
protected static function valueToString(mixed $value): string
5246
{
53-
switch (gettype($value)) {
54-
case 'string':
55-
return '"' . addslashes($value) . '"';
56-
case 'boolean':
57-
return $value ? 'true' : 'false';
58-
default:
59-
return $value;
47+
if (!is_string($value)
48+
&& !is_numeric($value)
49+
&& !is_bool($value)
50+
&& !$value instanceof \Stringable
51+
) {
52+
throw new \InvalidArgumentException("Value isn't stringable");
6053
}
54+
55+
return match (gettype($value)) {
56+
'string' => '"' . addslashes($value) . '"',
57+
'boolean' => $value ? 'true' : 'false',
58+
default => (string) $value,
59+
};
6160
}
6261

6362
/**
6463
* Returns the list as a formatted string.
6564
*
66-
* @param string[] $list
67-
*
68-
* @return string
65+
* @param array<mixed> $list
6966
*/
70-
protected static function listToString(array $list)
67+
protected static function listToString(array $list): string
7168
{
72-
if (empty($list)) {
69+
if ($list === []) {
7370
return '[]';
7471
}
7572

76-
$list = array_map(['static', 'valueToString'], $list);
73+
$list = array_map(fn ($item): string => static::valueToString($item), $list);
7774

7875
return '[' . implode(', ', $list) . ']';
7976
}

tests/support/ExampleException.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace tests\support;
46

57
use TomPHP\ExceptionConstructorTools;
@@ -8,32 +10,42 @@ class ExampleException extends \RuntimeException
810
{
911
use ExceptionConstructorTools;
1012

11-
public static function fromFormatString($format, $param)
13+
/**
14+
* @param array<bool|float|int|string|null>|bool|float|int|string|null $param
15+
*/
16+
public static function fromFormatString(string $format, array|bool|float|int|string|null $param): static
1217
{
13-
return self::create($format, [$param]);
18+
if (!is_array($param)) {
19+
$param = [$param];
20+
}
21+
22+
return self::create($format, $param);
1423
}
1524

16-
public static function fromCode($code)
25+
public static function fromCode(int $code): static
1726
{
1827
return self::create('', [], $code);
1928
}
2029

21-
public static function fromPreviousException($previous)
30+
public static function fromPreviousException(?\Exception $exception): static
2231
{
23-
return self::create('', [], 0, $previous);
32+
return self::create('', [], 0, $exception);
2433
}
2534

26-
public static function withTypeInMessage($param)
35+
public static function withTypeInMessage(mixed $param): static
2736
{
2837
return self::create(self::typeToString($param));
2938
}
3039

31-
public static function withValueInMessage($value)
40+
public static function withValueInMessage(mixed $value): static
3241
{
3342
return self::create(self::valueToString($value));
3443
}
3544

36-
public static function withListInMessage($param)
45+
/**
46+
* @param array<mixed> $param
47+
*/
48+
public static function withListInMessage(array $param): static
3749
{
3850
return self::create(self::listToString($param));
3951
}

0 commit comments

Comments
 (0)