Skip to content

Commit 3754685

Browse files
committed
:octocat: PHPCS: add Slevomat standard
1 parent 927d5d8 commit 3754685

Some content is hidden

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

46 files changed

+342
-351
lines changed

composer.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"phan/phan": "^5.4",
4949
"phpmd/phpmd": "^2.15",
5050
"phpunit/phpunit": "^10.5",
51-
"squizlabs/php_codesniffer": "^3.9"
51+
"slevomat/coding-standard": "^8.15",
52+
"squizlabs/php_codesniffer": "^3.10"
5253
},
5354
"suggest": {
5455
"chillerlan/php-httpinterface": "^6.0 - an alternative PSR-18 HTTP Client"
@@ -71,6 +72,9 @@
7172
"config": {
7273
"lock": false,
7374
"sort-packages": true,
74-
"platform-check": true
75+
"platform-check": true,
76+
"allow-plugins": {
77+
"dealerdirect/phpcodesniffer-composer-installer": true
78+
}
7579
}
7680
}

examples/OAuthExampleProviderFactory.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,31 @@
3030
use Psr\Log\LoggerInterface;
3131

3232
/**
33-
*
33+
* An extended provider factory to simplify using the examples
3434
*/
3535
class OAuthExampleProviderFactory{
3636

3737
public const STORAGE_MEMORY = 0b001;
3838
public const STORAGE_SESSION = 0b010;
3939
public const STORAGE_FILE = 0b100;
4040

41-
protected DotEnv $dotEnv;
42-
protected LoggerInterface $logger;
41+
protected OAuthProviderFactory $factory;
42+
protected string $cfgDir;
43+
protected DotEnv $dotEnv;
44+
protected LoggerInterface $logger;
4345
protected OAuthOptions|SettingsContainerInterface $options;
44-
protected OAuthStorageInterface $fileStorage;
46+
protected OAuthStorageInterface $fileStorage;
4547

4648
public function __construct(
47-
protected OAuthProviderFactory $factory,
48-
protected string $cfgDir,
49-
string $envFile,
50-
string $logLevel,
49+
OAuthProviderFactory $factory,
50+
string $cfgDir,
51+
string $envFile,
52+
string $logLevel,
5153
){
5254
ini_set('date.timezone', 'UTC');
5355

56+
$this->factory = $factory;
57+
$this->cfgDir = $cfgDir;
5458
$this->dotEnv = (new DotEnv($this->cfgDir, $envFile, false))->load();
5559
$this->logger = $this->initLogger($logLevel);
5660
$this->fileStorage = $this->initFileStorage();

examples/Providers/GitHub/gist-spotify-top-tracks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
($t->artists[0]->name ?? ''),
6969
($t->artists[0]->external_urls->spotify ?? ''),
7070
($t->name ?? ''),
71-
($t->external_urls->spotify ?? '')
71+
($t->external_urls->spotify ?? ''),
7272
);
7373
}
7474

examples/Providers/LastFM/topalbum-patchwork.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,20 @@
104104
}
105105

106106
$img = array_shift($res);
107-
imagecopyresampled($patchwork, $img, ($x * $imageSize), ($y * $imageSize), 0, 0, $imageSize, $imageSize, imagesx($img), imagesy($img));
107+
108+
imagecopyresampled(
109+
$patchwork,
110+
$img,
111+
($x * $imageSize),
112+
($y * $imageSize),
113+
0,
114+
0,
115+
$imageSize,
116+
$imageSize,
117+
imagesx($img),
118+
imagesy($img),
119+
);
120+
108121
imagedestroy($img);
109122
}
110123
}
@@ -146,7 +159,8 @@ function getImage(string $url, string $urlcache):string{
146159
return $urlcache.$path;
147160
}
148161

149-
function sendResponse(array $response):void{
162+
/** @param array<string, mixed> $response */
163+
function sendResponse(array $response):never{
150164
header('Content-type: application/json;charset=utf-8;');
151165

152166
echo json_encode($response, JSON_PRETTY_PRINT);

examples/Providers/Spotify/MixesDBTrackSearch.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
use chillerlan\HTTP\Utils\MessageUtil;
1313

1414
/**
15+
* Track search in a JSON file scraped from MixesDB (RIP)
1516
*
17+
* @see https://gist.github.com/codemasher/8986089773f036a2d4c0f34eab439459
1618
*/
1719
class MixesDBTrackSearch extends SpotifyClient{
1820

1921
/**
2022
* search tracks on spotify from the given mixesdb track lists
23+
*
24+
* @param string[] $find
2125
*/
2226
public function getTracks(
2327
string $clubnightsJSON,
@@ -73,7 +77,15 @@ public function getTracks(
7377
foreach($data->tracks->items as $i => $item){
7478
$setTracks[$item->id] = $item->id;
7579

76-
$this->logger->info(sprintf('found: [%s][%s] %s - %s', ++$i, $item->id, implode(', ', array_column($item->artists, 'name')), $item->name));
80+
$this->logger->info(
81+
sprintf(
82+
'found: [%s][%s] %s - %s',
83+
++$i,
84+
$item->id,
85+
implode(', ', array_column($item->artists, 'name')),
86+
$item->name,
87+
),
88+
);
7789
}
7890

7991
}
@@ -97,6 +109,8 @@ public function getTracks(
97109

98110
/**
99111
* check a string for the occurence of any in the given array of needles
112+
*
113+
* @param string[] $needles
100114
*/
101115
protected function setContains(string $haystack, array $needles):bool{
102116

examples/Providers/Spotify/SpotifyClient.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use chillerlan\OAuth\Providers\Spotify;
1414

1515
/**
16-
*
16+
* Extended functionality for the Spotify client
1717
*/
1818
class SpotifyClient extends Spotify{
1919

@@ -22,7 +22,9 @@ class SpotifyClient extends Spotify{
2222
protected object $me;
2323
protected string $id;
2424
protected string $market;
25+
/** @var array<string, object> */
2526
protected array $artists = [];
27+
/** @var array<string, object> */
2628
protected array $albums = [];
2729

2830
protected function construct():void{
@@ -39,7 +41,7 @@ protected function saveToFile(array $vars, string $dir):void{
3941
foreach($vars as $var){
4042
file_put_contents(
4143
sprintf('%s/%s.json', rtrim($dir, '\\/'), $var),
42-
json_encode($this->{$var}, (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))
44+
json_encode($this->{$var}, (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)),
4345
);
4446
}
4547

@@ -264,12 +266,14 @@ public function createPlaylist(string $name, string $description):string{
264266

265267
/**
266268
* add the tracks to the given playlist
269+
*
270+
* @param string[] $trackIDs
267271
*/
268272
public function addTracks(string $playlistID, array $trackIDs):static{
269273

270274
$uris = array_chunk(
271275
array_map(fn(string $t):string => 'spotify:track:'.$t , array_values($trackIDs)), // why not just ids???
272-
100 // API max = 100 track URIs
276+
100, // API max = 100 track URIs
273277
);
274278

275279
foreach($uris as $i => $chunk){
@@ -283,7 +287,7 @@ public function addTracks(string $playlistID, array $trackIDs):static{
283287

284288
usleep(self::sleepTimer);
285289

286-
if(in_array($playlistAddTracks->getStatusCode(), [200, 201, 204])){
290+
if(in_array($playlistAddTracks->getStatusCode(), [200, 201, 204], true)){
287291
$json = MessageUtil::decodeJSON($playlistAddTracks);
288292

289293
$this->logger->info(sprintf('added tracks %s/%s [%s]', ++$i, count($uris), $json->snapshot_id));

examples/Providers/Spotify/SpotifyNewReleases.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
use chillerlan\HTTP\Utils\MessageUtil;
1313

1414
/**
15-
*
15+
* New releases crawler
1616
*/
1717
class SpotifyNewReleases extends SpotifyClient{
1818

19+
/** @var array<string, object> */
1920
protected array $newAlbums = [];
2021

2122
/**
@@ -140,7 +141,7 @@ protected function getNewAlbumTracks(int $since, int $until):void{
140141

141142
$playlistID = $this->createPlaylist(
142143
sprintf('new releases %s - %s', date('d.m.Y', $since), date('d.m.Y', $until)),
143-
sprintf('new releases by the artists i\'m following, %s - %s', date('d.m.Y', $since), date('d.m.Y', $until))
144+
sprintf('new releases by the artists i\'m following, %s - %s', date('d.m.Y', $since), date('d.m.Y', $until)),
144145
);
145146

146147
$this->addTracks($playlistID, $newtracks);

examples/Providers/Spotify/playlist-diff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function diff(string $playlistID1, string $playlistID2):array{
2020

2121
$playlistID = $this->createPlaylist(
2222
'playlist diff',
23-
sprintf('diff between playlists "spotify:playlist:%s" and "spotify:playlist:%s"', $playlistID1, $playlistID2)
23+
sprintf('diff between playlists "spotify:playlist:%s" and "spotify:playlist:%s"', $playlistID1, $playlistID2),
2424
);
2525

2626
$this->addTracks($playlistID, $diff);
@@ -37,7 +37,7 @@ public function merge(string $targetID, string ...$playlistIDs):array{
3737

3838
$playlistID = $this->createPlaylist(
3939
'playlist merge',
40-
sprintf('merged playlists "%s" into "spotify:playlist:%s"', implode('", "', $playlistIDs), $targetID)
40+
sprintf('merged playlists "%s" into "spotify:playlist:%s"', implode('", "', $playlistIDs), $targetID),
4141
);
4242

4343
$this->addTracks($playlistID, array_keys($merged));

phpcs.xml.dist

+108
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<file>src</file>
99
<file>tests</file>
1010

11+
<config name="installed_paths" value="../../slevomat/coding-standard"/>
12+
1113
<arg value="ps"/>
1214
<arg name="parallel" value="8"/>
1315
<!--<arg name="cache" value=".build/phpcs.cache"/>-->
@@ -20,6 +22,112 @@
2022
<type>error</type>
2123
</rule>
2224

25+
<!--
26+
Slevomat https://github.com/slevomat/coding-standard
27+
-->
28+
29+
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
30+
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"/>
31+
<rule ref="SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed"/>
32+
33+
<rule ref="SlevomatCodingStandard.Attributes.DisallowAttributesJoining"/>
34+
<rule ref="SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine"/>
35+
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>
36+
37+
<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
38+
<rule ref="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion"/>
39+
<rule ref="SlevomatCodingStandard.Classes.ForbiddenPublicProperty"/>
40+
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
41+
42+
<rule ref="SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration"/>
43+
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
44+
<rule ref="SlevomatCodingStandard.Commenting.UselessFunctionDocComment"/>
45+
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>
46+
47+
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
48+
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch"/>
49+
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator"/>
50+
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperator"/>
51+
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithoutParentheses"/>
52+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
53+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
54+
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator"/>
55+
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
56+
<rule ref="SlevomatCodingStandard.ControlStructures.UselessTernaryOperator"/>
57+
58+
<rule ref="SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch"/>
59+
60+
<rule ref="SlevomatCodingStandard.Functions.DisallowEmptyFunction"/>
61+
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>
62+
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
63+
<rule ref="SlevomatCodingStandard.Functions.StrictCall"/>
64+
65+
<rule ref="SlevomatCodingStandard.Namespaces.RequireOneNamespaceInFile"/>
66+
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
67+
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
68+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
69+
70+
<rule ref="SlevomatCodingStandard.Numbers.DisallowNumericLiteralSeparator"/>
71+
72+
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators"/>
73+
<rule ref="SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator"/>
74+
75+
<rule ref="SlevomatCodingStandard.PHP.OptimizedFunctionsWithoutUnpacking"/>
76+
<rule ref="SlevomatCodingStandard.PHP.ShortList"/>
77+
<rule ref="SlevomatCodingStandard.PHP.TypeCast"/>
78+
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
79+
80+
<rule ref="SlevomatCodingStandard.Strings.DisallowVariableParsing"/>
81+
82+
<!--<rule ref="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>-->
83+
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
84+
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition"/>
85+
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
86+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
87+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
88+
89+
<!--<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>-->
90+
91+
<rule ref="SlevomatCodingStandard.Variables.DisallowVariableVariable"/>
92+
<rule ref="SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable"/>
93+
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>
94+
95+
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
96+
<properties>
97+
<property name="linesCountBeforeDeclare" value="0"/>
98+
<property name="linesCountAfterDeclare" value="1"/>
99+
<property name="spacesCountAroundEqualsSign" value="0"/>
100+
</properties>
101+
</rule>
102+
103+
<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">
104+
<properties>
105+
<property name="minLineLength" value="131"/>
106+
</properties>
107+
</rule>
108+
109+
<rule ref="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable">
110+
<exclude-pattern>examples</exclude-pattern>
111+
<exclude-pattern>src/Storage/SessionStorage.php</exclude-pattern>
112+
<exclude-pattern>tests/Storage/SessionStorageTest.php</exclude-pattern>
113+
</rule>
114+
115+
<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat">
116+
<properties>
117+
<property name="withSpaces" value="no"/>
118+
<property name="shortNullable" value="no"/>
119+
<property name="nullPosition" value="last"/>
120+
</properties>
121+
</rule>
122+
123+
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable">
124+
<properties>
125+
<property name="ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach" value="true"/>
126+
</properties>
127+
<exclude-pattern>examples</exclude-pattern>
128+
</rule>
129+
130+
23131
<!--
24132
PHPCS built-in https://tentyp.dev/library/php/phpcs/
25133
-->

src/Core/AccessToken.php

+4
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ final class AccessToken extends SettingsContainerAbstract{
7070

7171
/**
7272
* (magic) The scopes that are attached to this token
73+
*
74+
* @var string[]
7375
*/
7476
protected array $scopes = [];
7577

7678
/**
7779
* (magic) Additional token parameters supplied by the provider
80+
*
81+
* @var array<string, mixed>
7882
*/
7983
protected array $extraParams = [];
8084

src/Core/AuthenticatedUser.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ final class AuthenticatedUser extends SettingsContainerAbstract{
5555

5656
/**
5757
* (magic) The full user endpoint response
58+
*
59+
* @var array<string, mixed>
5860
*/
5961
protected array $data = [];
6062

@@ -78,12 +80,12 @@ public function __construct(iterable|null $properties = null){
7880
*/
7981

8082
/** @codeCoverageIgnore */
81-
public function __set(string $property, $value):void{
83+
public function __set(string $property, mixed $value):void{
8284
// noop
8385
}
8486

8587
/** @codeCoverageIgnore */
86-
public function fromIterable(iterable $properties):static{
88+
public function fromIterable(iterable $properties):static{ // phpcs:ignore
8789
// noop
8890
return $this;
8991
}

0 commit comments

Comments
 (0)