Skip to content

Commit ea613f2

Browse files
committed
Adjust code style settings and fix issues
1 parent 328e1a9 commit ea613f2

12 files changed

+49
-39
lines changed

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@
3838
"phpstan/phpstan": "^1.9",
3939
"phpstan/phpstan-phpunit": "^1.3",
4040
"phpunit/phpunit": "^9.5.27",
41+
"slevomat/coding-standard": "^8.15",
4142
"squizlabs/php_codesniffer": "^3.7.1"
4243
},
4344
"config": {
44-
"sort-packages": true
45+
"sort-packages": true,
46+
"allow-plugins": {
47+
"dealerdirect/phpcodesniffer-composer-installer": true
48+
}
4549
},
4650
"extra": {
4751
"branch-alias": {

phpcs.xml.dist

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ruleset name="structured-fields">
3-
<description>Default PHP CodeSniffer configuration for gapple/structured-fields.</description>
3+
<description>Default PHP CodeSniffer configuration for gapple/structured-fields.</description>
44

5-
<file>src</file>
6-
<file>tests</file>
5+
<file>src</file>
6+
<file>tests</file>
77

8-
<rule ref="PSR12"/>
8+
<rule ref="PSR12"/>
9+
10+
<rule ref="Generic.PHP.DeprecatedFunctions"/>
11+
<rule ref="Generic.PHP.RequireStrictTypes">
12+
<exclude-pattern>tests/*\.php$</exclude-pattern>
13+
</rule>
14+
<rule ref="Generic.VersionControl.GitMergeConflict"/>
15+
<rule ref="Squiz.NamingConventions.ValidVariableName">
16+
<exclude-pattern>tests/*\.php$</exclude-pattern>
17+
</rule>
18+
<rule ref="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore">
19+
<severity>0</severity>
20+
</rule>
21+
<rule ref="SlevomatCodingStandard.Arrays.ArrayAccess"/>
22+
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
23+
<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
24+
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
25+
<rule ref="SlevomatCodingStandard.Functions.UselessParameterDefaultValue"/>
26+
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
27+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
28+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
929
</ruleset>

phpmd.ruleset.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
http://pmd.sf.net/ruleset_xml_schema.xsd">
99
<description>PHPMD Ruleset</description>
1010

11-
<rule ref="rulesets/codesize.xml"/>
11+
<rule ref="rulesets/codesize.xml">
12+
<exclude name="CyclomaticComplexity" />
13+
<exclude name="ExcessiveClassComplexity" />
14+
</rule>
1215
<rule ref="rulesets/unusedcode.xml"/>
1316
</ruleset>

src/InnerList.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static function validateItemType(mixed $value): void
5757
throw new \InvalidArgumentException('InnerList objects cannot be nested');
5858
}
5959
} elseif (is_array($value)) {
60-
if (count($value) != 2) {
60+
if (count($value) !== 2) {
6161
throw new \InvalidArgumentException();
6262
}
6363
} else {

src/OuterList.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static function validateItemType(mixed $value): void
6262
);
6363
}
6464
} elseif (is_array($value)) {
65-
if (count($value) != 2) {
65+
if (count($value) !== 2) {
6666
throw new \InvalidArgumentException();
6767
}
6868
} else {

src/Parser.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace gapple\StructuredFields;
66

7-
/**
8-
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
9-
*/
107
class Parser
118
{
129
public static function parseDictionary(string $string): Dictionary
@@ -290,7 +287,6 @@ private static function parseString(ParsingInput $input): string
290287

291288
/**
292289
* @phpstan-impure
293-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
294290
*/
295291
private static function parseDisplayString(ParsingInput $string): DisplayString
296292
{
@@ -301,7 +297,7 @@ private static function parseDisplayString(ParsingInput $string): DisplayString
301297
throw new ParseException('Invalid start of display string at position ' . $startPosition);
302298
}
303299

304-
$encoded_string = '';
300+
$encodedString = '';
305301
while (!$string->empty()) {
306302
$char = $string->consumeChar();
307303

@@ -311,22 +307,22 @@ private static function parseDisplayString(ParsingInput $string): DisplayString
311307
);
312308
} elseif ($char === '%') {
313309
try {
314-
$encoded_string .= '%' . $string->consumeRegex('/^[0-9a-f]{2}/');
310+
$encodedString .= '%' . $string->consumeRegex('/^[0-9a-f]{2}/');
315311
} catch (\RuntimeException) {
316312
throw new ParseException(
317313
'Invalid hex values in display string at position ' . ($string->position() - 1)
318314
);
319315
}
320316
} elseif ($char === '"') {
321-
$display_string = new DisplayString(rawurldecode($encoded_string));
317+
$displayString = new DisplayString(rawurldecode($encodedString));
322318
// An invalid UTF-8 subject will cause the preg_* function to match nothing.
323319
// @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
324-
if (!preg_match('/^\X*$/u', (string) $display_string)) {
320+
if (!preg_match('/^\X*$/u', (string) $displayString)) {
325321
throw new ParseException('Invalid byte sequence in display string at position ' . $startPosition);
326322
}
327-
return $display_string;
323+
return $displayString;
328324
} else {
329-
$encoded_string .= $char;
325+
$encodedString .= $char;
330326
}
331327
}
332328

src/Serializer.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace gapple\StructuredFields;
66

7-
/**
8-
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
9-
*/
107
class Serializer
118
{
129
/**
@@ -59,7 +56,7 @@ public static function serializeList(iterable $value): string
5956
if ($item instanceof TupleInterface) {
6057
$itemValue = $item->getValue();
6158
$itemParameters = $item->getParameters();
62-
} elseif (is_array($item) && count($item) == 2) {
59+
} elseif (is_array($item) && count($item) === 2) {
6360
$itemValue = $item[0];
6461
$itemParameters = $item[1];
6562
} else {
@@ -152,9 +149,6 @@ private static function serializeInnerList(array $value, ?object $parameters = n
152149
return $returnValue;
153150
}
154151

155-
/**
156-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
157-
*/
158152
private static function serializeBareItem(mixed $value): string
159153
{
160154
if (is_int($value)) {
@@ -220,12 +214,12 @@ private static function serializeString(string $value): string
220214

221215
private static function serializeDisplayString(DisplayString $value): string
222216
{
223-
$encode_pattern = '/[%"\x00-\x1F\x7F-\xFF]/';
224-
$encode_callback = function ($matches) {
217+
$encodePattern = '/[%"\x00-\x1F\x7F-\xFF]/';
218+
$encodeCallback = function ($matches) {
225219
return strtolower(rawurlencode($matches[0]));
226220
};
227221

228-
return '%"' . preg_replace_callback($encode_pattern, $encode_callback, (string) $value) . '"';
222+
return '%"' . preg_replace_callback($encodePattern, $encodeCallback, (string) $value) . '"';
229223
}
230224

231225
private static function serializeToken(Token $value): string

tests/Httpwg/HttpwgTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ abstract class HttpwgTest extends RulesetTest
1212
*/
1313
protected $ruleset;
1414

15-
/**
16-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
17-
*/
1815
protected function rulesetDataProvider(): array
1916
{
2017
$path = __DIR__ . '/../../vendor/httpwg/structured-field-tests/' . $this->ruleset . '.json';

tests/InnerListTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testArrayAccess(): void
2121
$list = new InnerList(
2222
[
2323
['Test Value One', (object) []],
24-
['Test Value Two', (object) []]
24+
['Test Value Two', (object) []],
2525
],
2626
(object) ['paramKey' => 'param value']
2727
);

tests/ParseListTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function multipleStringProvider(): array
2424
new Item(1),
2525
new Item(42, Parameters::fromArray(['towel' => true, 'panic' => false])),
2626
new Item('two'),
27-
])
27+
]),
2828
];
2929

3030
$dataset[] = [

tests/Rule.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
namespace gapple\Tests\StructuredFields;
44

5-
use gapple\StructuredFields\Dictionary;
6-
use gapple\StructuredFields\Item;
7-
use gapple\StructuredFields\OuterList;
8-
95
class Rule
106
{
117
/**
@@ -27,7 +23,7 @@ class Rule
2723
public $raw;
2824

2925
/**
30-
* @var null|Item|Dictionary|OuterList
26+
* @var null|\gapple\StructuredFields\Item|\gapple\StructuredFields\Dictionary|\gapple\StructuredFields\OuterList
3127
* @readonly
3228
*/
3329
public $expected;

tests/SerializeListTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testNestedInnerListTuple(): void
110110
],
111111
new \stdClass(), // Inner Inner List Parameters
112112
],
113-
new Item(42)
113+
new Item(42),
114114
],
115115
new \stdClass(), // Outer Inner Parameters
116116
],

0 commit comments

Comments
 (0)