Skip to content

Commit 9f6e216

Browse files
committed
Configure PHP CS Fixer and clean up code
1 parent 7021d54 commit 9f6e216

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+472
-417
lines changed

.php-cs-fixer.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require __DIR__ . '/tools/php-cs-fixer/ClarifyingParenthesesAroundComparisonsFixer.php';
4+
require __DIR__ . '/tools/php-cs-fixer/MultilineWhitespaceBeforeDoubleColonFixer.php';
45

56
$finder = PhpCsFixer\Finder
67
::create()
@@ -10,50 +11,50 @@
1011
$config = new PhpCsFixer\Config();
1112
$config->registerCustomFixers([
1213
new Hereldar\Tools\PhpCsFixer\ClarifyingParenthesesAroundComparisonsFixer(),
14+
new Hereldar\Tools\PhpCsFixer\MultilineWhitespaceBeforeDoubleColonFixer(),
1315
]);
1416
$config->setFinder($finder);
1517
$config->setRules([
1618
'@PHP81Migration' => true,
17-
'@PSR12' => true,
19+
'@PER-CS' => true,
1820
'align_multiline_comment' => true,
1921
'array_indentation' => true,
2022
'backtick_to_shell_exec' => true,
2123
'binary_operator_spaces' => true,
22-
'blank_line_before_statement' => ['statements' => ['case', 'declare', 'default', 'phpdoc', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield', 'yield_from']],
23-
'braces' => ['allow_single_line_anonymous_class_with_empty_body' => true, 'allow_single_line_closure' => true],
24+
'braces_position' => ['allow_single_line_anonymous_functions' => true, 'allow_single_line_empty_anonymous_classes' => true],
2425
'cast_spaces' => true,
2526
'class_attributes_separation' => ['elements' => ['method' => 'one', /*'case' => 'one'*/]],
2627
'class_reference_name_casing' => true,
2728
'combine_consecutive_issets' => true,
2829
'combine_consecutive_unsets' => true,
29-
'concat_space' => ['spacing' => 'one'],
30+
'concat_space' => ['spacing' => 'none'],
31+
'declare_parentheses' => true,
3032
'echo_tag_syntax' => true,
3133
'empty_loop_body' => ['style' => 'braces'],
3234
'empty_loop_condition' => true,
3335
'escape_implicit_backslashes' => true,
3436
'explicit_string_variable' => true,
3537
'fully_qualified_strict_types' => true,
36-
'function_typehint_space' => true,
3738
'general_phpdoc_tag_rename' => ['replacements' => ['inheritDocs' => 'inheritDoc']],
3839
'include' => true,
3940
'increment_style' => true,
4041
'integer_literal_case' => true,
4142
'lambda_not_used_import' => true,
4243
'linebreak_after_opening_tag' => true,
4344
'magic_constant_casing' => true,
45+
'method_argument_space' => ['on_multiline' => 'ignore'],
4446
'method_chaining_indentation' => true,
4547
'multiline_comment_opening_closing' => true,
46-
'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'],
4748
'native_function_casing' => true,
48-
'native_function_type_declaration_casing' => true,
49+
'native_type_declaration_casing' => true,
4950
'no_alias_language_construct_call' => true,
5051
'no_alternative_syntax' => true,
5152
'no_binary_string' => true,
5253
'no_blank_lines_after_phpdoc' => true,
5354
'no_empty_comment' => true,
5455
'no_empty_phpdoc' => true,
5556
'no_empty_statement' => true,
56-
'no_extra_blank_lines' => ['tokens' => ['case', 'continue', 'default', 'extra', 'parenthesis_brace_block', 'square_brace_block', 'switch', 'throw', 'use']],
57+
'no_extra_blank_lines' => ['tokens' => ['attribute', 'case', 'continue', 'curly_brace_block', 'default', 'extra', 'parenthesis_brace_block', 'square_brace_block', 'switch', 'throw', 'use']],
5758
'no_leading_namespace_whitespace' => true,
5859
'no_mixed_echo_print' => true,
5960
'no_multiline_whitespace_around_double_arrow' => true,
@@ -62,35 +63,41 @@
6263
'no_spaces_around_offset' => true,
6364
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => true],
6465
'no_trailing_comma_in_singleline' => true,
65-
//'no_trailing_comma_in_singleline_function_call' => true,
66+
'no_unneeded_braces' => ['namespaces' => true],
6667
'no_unneeded_control_parentheses' => ['statements' => ['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield', 'yield_from']],
67-
'no_unneeded_curly_braces' => true,
6868
'no_unneeded_import_alias' => true,
6969
'no_unused_imports' => true,
7070
'object_operator_without_whitespace' => true,
7171
'operator_linebreak' => ['position' => 'beginning'],
72-
'ordered_class_elements' => true,
72+
'ordered_class_elements' => ['order' => ['use_trait']],
7373
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
7474
'php_unit_fqcn_annotation' => true,
7575
'php_unit_method_casing' => true,
76-
'php_unit_test_class_requires_covers' => true,
7776
'phpdoc_align' => ['align' => 'left'],
7877
'phpdoc_annotation_without_dot' => true,
7978
'phpdoc_indent' => true,
8079
'phpdoc_inline_tag_normalizer' => true,
8180
'phpdoc_no_access' => true,
8281
'phpdoc_no_alias_tag' => ['replacements' => ['type' => 'var', 'link' => 'see']],
83-
'phpdoc_no_empty_return' => true,
8482
'phpdoc_no_package' => true,
8583
'phpdoc_order' => true,
8684
'phpdoc_order_by_value' => true,
8785
'phpdoc_return_self_reference' => true,
8886
'phpdoc_scalar' => true,
89-
'phpdoc_separation' => true,
87+
'phpdoc_separation' => ['groups' => [
88+
['deprecated', 'link', 'see', 'since'],
89+
['author', 'copyright', 'license'],
90+
['category', 'package', 'subpackage'],
91+
['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write', 'psalm-property', 'psalm-property-read', 'psalm-property-write'],
92+
['pure', 'phpstan-pure', 'psalm-pure'],
93+
['param', 'phpstan-param', 'psalm-param'],
94+
['throws', 'phpstan-throws', 'psalm-throws'],
95+
['return', 'phpstan-return', 'psalm-return'],
96+
]],
9097
'phpdoc_single_line_var_spacing' => true,
9198
'phpdoc_summary' => true,
9299
'phpdoc_tag_type' => true,
93-
'phpdoc_to_comment' => true,
100+
'phpdoc_to_comment' => ['ignored_tags' => ['var', 'phpstan-var', 'psalm-var', 'phpstan-ignore-next-line', 'psalm-suppress']],
94101
'phpdoc_trim' => true,
95102
'phpdoc_trim_consecutive_blank_line_separation' => true,
96103
'phpdoc_types' => true,
@@ -104,17 +111,19 @@
104111
//'single_line_comment_spacing' => true,
105112
'single_line_comment_style' => true,
106113
'single_quote' => true,
107-
'single_space_after_construct' => true,
114+
'single_space_around_construct' => true,
108115
'space_after_semicolon' => true,
109116
'standardize_increment' => true,
110117
'standardize_not_equals' => true,
111118
'switch_continue_to_break' => true,
112119
'trailing_comma_in_multiline' => true,
113120
'trim_array_spaces' => true,
121+
'type_declaration_spaces' => true,
114122
'types_spaces' => true,
115123
'unary_operator_spaces' => true,
116124
'whitespace_after_comma_in_array' => true,
117125
'Hereldar/clarifying_parentheses_around_comparisons' => true,
126+
'Hereldar/multiline_whitespace_before_double_colon' => true,
118127
]);
119128

120129
return $config;

src/DateTime.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use ArithmeticError;
88
use DateTime as MutableNativeDateTime;
99
use DateTimeImmutable as NativeDateTime;
10-
use DateTimeZone as NativeTimeZone;
1110
use DateTimeInterface as NativeDateTimeInterface;
11+
use DateTimeZone as NativeTimeZone;
1212
use Hereldar\DateTimes\Exceptions\FormatException;
1313
use Hereldar\DateTimes\Exceptions\ParseException;
1414
use Hereldar\DateTimes\Exceptions\TimeZoneException;
@@ -76,8 +76,7 @@ class DateTime implements Datelike, Timelike, Formattable, Stringable, Copyable,
7676
*/
7777
private function __construct(
7878
private readonly NativeDateTime $value
79-
) {
80-
}
79+
) {}
8180

8281
/**
8382
* Outputs this date-time as a `string`, using the default format

src/LocalDate.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class LocalDate implements Datelike, Formattable, Stringable, Copyable, Summable
5252
*/
5353
private function __construct(
5454
private readonly NativeDateTime $value,
55-
) {
56-
}
55+
) {}
5756

5857
/**
5958
* Outputs this date as a `string`, using the default format of

src/LocalDateTime.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ class LocalDateTime implements Datelike, Timelike, Formattable, Stringable, Copy
6363
*/
6464
private function __construct(
6565
private readonly NativeDateTime $value,
66-
) {
67-
}
66+
) {}
6867

6968
/**
7069
* Outputs this date-time as a `string`, using the default format

src/LocalTime.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ class LocalTime implements Timelike, Formattable, Stringable, Copyable, Summable
6666
*/
6767
private function __construct(
6868
private readonly NativeDateTime $value,
69-
) {
70-
}
69+
) {}
7170

7271
/**
7372
* Outputs this time as a `string`, using the default format of
@@ -727,7 +726,7 @@ public function plus(
727726

728727
/**
729728
* Returns a copy of this time with the specified amount of hours,
730-
* minutes, seconds and microseconds subtracted. It accepts a
729+
* minutes, seconds and microseconds subtracted. It accepts a
731730
* `Period` or individual time units.
732731
*
733732
* If a `Period` is passed as the first argument, no individual

src/Offset.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Offset implements Formattable, Stringable, Copyable, Summable
6969
(?P<seconds>[0-9]{2})
7070
)?
7171
/xS
72-
REGEX;
72+
REGEX;
7373

7474
private const RFC2822_PATTERN = <<<'REGEX'
7575
/
@@ -80,7 +80,7 @@ class Offset implements Formattable, Stringable, Copyable, Summable
8080
(?P<seconds>[0-9]{2})
8181
)?
8282
/xS
83-
REGEX;
83+
REGEX;
8484

8585
private const FORMAT_PATTERN = '/%([%a-zA-Z])/';
8686

@@ -89,8 +89,7 @@ class Offset implements Formattable, Stringable, Copyable, Summable
8989
*/
9090
private function __construct(
9191
private readonly int $value,
92-
) {
93-
}
92+
) {}
9493

9594
/**
9695
* Outputs this offset as a `string`, using the ISO 8601 format.
@@ -244,7 +243,7 @@ private static function parseSimple(
244243
): Ok|Error {
245244
$pattern = preg_replace_callback(
246245
pattern: self::FORMAT_PATTERN,
247-
callback: static fn (array $matches) => match ($matches[1]) {
246+
callback: static fn(array $matches) => match ($matches[1]) {
248247
'%' => '%',
249248
'R' => '(?P<sign>[+-])',
250249
'r' => '(?P<sign>\-?)',
@@ -389,7 +388,7 @@ public function format(string $format = Offset::ISO8601): Ok|Error
389388
{
390389
$string = preg_replace_callback(
391390
pattern: self::FORMAT_PATTERN,
392-
callback: fn (array $matches) => match ($matches[1]) {
391+
callback: fn(array $matches) => match ($matches[1]) {
393392
'%' => '%',
394393
'R' => ($this->isNegative()) ? '-' : '+',
395394
'r' => ($this->isNegative()) ? '-' : '',
@@ -577,7 +576,7 @@ public function totalSeconds(): int
577576
*/
578577
public function compareTo(Offset $that): int
579578
{
580-
return $this->value <=> $that->totalSeconds();
579+
return ($this->value <=> $that->totalSeconds());
581580
}
582581

583582
/**

src/Period.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Period implements Formattable, Stringable, Copyable, Summable, Multipliabl
7676
)?
7777
)?
7878
/xS
79-
REGEX;
79+
REGEX;
8080

8181
private const FORMAT_PATTERN = '/%([%a-zA-Z])/';
8282

@@ -91,8 +91,7 @@ private function __construct(
9191
private readonly int $minutes = 0,
9292
private readonly int $seconds = 0,
9393
private readonly int $microseconds = 0,
94-
) {
95-
}
94+
) {}
9695

9796
/**
9897
* Outputs this period as a `string`, using the default format of
@@ -300,7 +299,7 @@ private static function parseSimple(
300299
): Ok|Error {
301300
$pattern = preg_replace_callback(
302301
pattern: self::FORMAT_PATTERN,
303-
callback: static fn (array $matches) => match ($matches[1]) {
302+
callback: static fn(array $matches) => match ($matches[1]) {
304303
'%' => '%',
305304
'R' => '(?P<sign>[+-])',
306305
'r' => '(?P<sign>\-?)',
@@ -442,7 +441,7 @@ public function format(string $format = Period::ISO8601): Ok|Error
442441

443442
$string = preg_replace_callback(
444443
pattern: self::FORMAT_PATTERN,
445-
callback: fn (array $matches) => match ($matches[1]) {
444+
callback: fn(array $matches) => match ($matches[1]) {
446445
'%' => '%',
447446
'Y' => sprintf('%04d', $this->years),
448447
'y' => (string) $this->years,
@@ -552,7 +551,7 @@ public function toIso8601(): string
552551
}
553552

554553
if ($f) {
555-
$microseconds = rtrim(sprintf("%06d", $f), '0');
554+
$microseconds = rtrim(sprintf('%06d', $f), '0');
556555
$string .= "{$s}.{$microseconds}S";
557556
} elseif ($s) {
558557
$string .= "{$s}S";
@@ -753,7 +752,7 @@ public function isNotEqual(Period $that): bool
753752
*/
754753
public function isSimilar(Period $that): bool
755754
{
756-
return 0 === $this->compareTo($that);
755+
return (0 === $this->compareTo($that));
757756
}
758757

759758
/**
@@ -766,7 +765,7 @@ public function isSimilar(Period $that): bool
766765
*/
767766
public function isNotSimilar(Period $that): bool
768767
{
769-
return 0 !== $this->compareTo($that);
768+
return (0 !== $this->compareTo($that));
770769
}
771770

772771
/**
@@ -1143,7 +1142,6 @@ public function normalized(): static
11431142
/**
11441143
* @param int[] $values
11451144
* @param int[] $factors
1146-
* @param bool $changed
11471145
*
11481146
* @return int[]
11491147
*/
@@ -1172,7 +1170,6 @@ private function normalizeOverflowedValues(array $values, array $factors, bool &
11721170
/**
11731171
* @param int[] $values
11741172
* @param int[] $factors
1175-
* @param bool $changed
11761173
*
11771174
* @return int[]
11781175
*/

src/Services/Validator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public static function day(int $day, int $month, int $year): void
5050

5151
public static function dayOfYear(int $day, int $year): void
5252
{
53-
5453
if (self::yearIsLeap($year)) {
5554
$max = 366;
5655
} else {

src/TimeZone.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Hereldar\DateTimes\Enums\TimeZoneType;
1010
use Hereldar\DateTimes\Exceptions\CountryException;
1111
use Hereldar\DateTimes\Exceptions\TimeZoneException;
12-
use InvalidArgumentException;
1312
use Stringable;
1413
use Throwable;
1514
use ValueError;
@@ -39,8 +38,7 @@ class TimeZone implements Stringable
3938
*/
4039
private function __construct(
4140
private readonly NativeTimeZone $value,
42-
) {
43-
}
41+
) {}
4442

4543
/**
4644
* Outputs this time-zone as a `string`, using its name.
@@ -225,7 +223,7 @@ public function compareTo(TimeZone $that): int
225223
return $result;
226224
}
227225

228-
return $a->getName() <=> $b->getName();
226+
return ($a->getName() <=> $b->getName());
229227
}
230228

231229
/**

tests/DateTime/AdditionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function testAddMicrosecondsPositive(): void
173173
$dateTime = DateTime::of(1986, 9, 21, 0, 0, 0);
174174
self::assertSame(1, $dateTime->plus(microseconds: 1)->microsecond());
175175
self::assertSame(1, $dateTime->plus(Period::of(microseconds: 1))->microsecond());
176-
$dateTime = DateTime::of(1986, 9, 21, 0, 0, 0, 100_000);
176+
$dateTime = DateTime::of(1986, 9, 21, 0, 0, 0, 100_000);
177177
self::assertSame(100001, $dateTime->plus(microseconds: 1)->microsecond());
178178
self::assertSame(100001, $dateTime->plus(Period::of(microseconds: 1))->microsecond());
179179
}
@@ -343,11 +343,11 @@ public function testArithmeticError(): void
343343
{
344344
self::assertException(
345345
ArithmeticError::class,
346-
fn () => DateTime::epoch()->plus(weeks: PHP_INT_MAX)
346+
fn() => DateTime::epoch()->plus(weeks: PHP_INT_MAX)
347347
);
348348
self::assertException(
349349
ArithmeticError::class,
350-
fn () => DateTime::epoch()->plus(microseconds: PHP_INT_MAX, milliseconds: 1)
350+
fn() => DateTime::epoch()->plus(microseconds: PHP_INT_MAX, milliseconds: 1)
351351
);
352352
}
353353
}

0 commit comments

Comments
 (0)