Skip to content

Commit

Permalink
:octocat: PHPCS: add Slevomat standard
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Jul 24, 2024
1 parent 927d5d8 commit 3754685
Show file tree
Hide file tree
Showing 46 changed files with 342 additions and 351 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"phan/phan": "^5.4",
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.9"
"slevomat/coding-standard": "^8.15",
"squizlabs/php_codesniffer": "^3.10"
},
"suggest": {
"chillerlan/php-httpinterface": "^6.0 - an alternative PSR-18 HTTP Client"
Expand All @@ -71,6 +72,9 @@
"config": {
"lock": false,
"sort-packages": true,
"platform-check": true
"platform-check": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
20 changes: 12 additions & 8 deletions examples/OAuthExampleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@
use Psr\Log\LoggerInterface;

/**
*
* An extended provider factory to simplify using the examples
*/
class OAuthExampleProviderFactory{

public const STORAGE_MEMORY = 0b001;
public const STORAGE_SESSION = 0b010;
public const STORAGE_FILE = 0b100;

protected DotEnv $dotEnv;
protected LoggerInterface $logger;
protected OAuthProviderFactory $factory;
protected string $cfgDir;
protected DotEnv $dotEnv;
protected LoggerInterface $logger;
protected OAuthOptions|SettingsContainerInterface $options;
protected OAuthStorageInterface $fileStorage;
protected OAuthStorageInterface $fileStorage;

public function __construct(
protected OAuthProviderFactory $factory,
protected string $cfgDir,
string $envFile,
string $logLevel,
OAuthProviderFactory $factory,
string $cfgDir,
string $envFile,
string $logLevel,
){
ini_set('date.timezone', 'UTC');

$this->factory = $factory;
$this->cfgDir = $cfgDir;
$this->dotEnv = (new DotEnv($this->cfgDir, $envFile, false))->load();
$this->logger = $this->initLogger($logLevel);
$this->fileStorage = $this->initFileStorage();
Expand Down
2 changes: 1 addition & 1 deletion examples/Providers/GitHub/gist-spotify-top-tracks.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
($t->artists[0]->name ?? ''),
($t->artists[0]->external_urls->spotify ?? ''),
($t->name ?? ''),
($t->external_urls->spotify ?? '')
($t->external_urls->spotify ?? ''),
);
}

Expand Down
18 changes: 16 additions & 2 deletions examples/Providers/LastFM/topalbum-patchwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,20 @@
}

$img = array_shift($res);
imagecopyresampled($patchwork, $img, ($x * $imageSize), ($y * $imageSize), 0, 0, $imageSize, $imageSize, imagesx($img), imagesy($img));

imagecopyresampled(
$patchwork,
$img,
($x * $imageSize),
($y * $imageSize),
0,
0,
$imageSize,
$imageSize,
imagesx($img),
imagesy($img),
);

imagedestroy($img);
}
}
Expand Down Expand Up @@ -146,7 +159,8 @@ function getImage(string $url, string $urlcache):string{
return $urlcache.$path;
}

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

echo json_encode($response, JSON_PRETTY_PRINT);
Expand Down
16 changes: 15 additions & 1 deletion examples/Providers/Spotify/MixesDBTrackSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
use chillerlan\HTTP\Utils\MessageUtil;

/**
* Track search in a JSON file scraped from MixesDB (RIP)
*
* @see https://gist.github.com/codemasher/8986089773f036a2d4c0f34eab439459
*/
class MixesDBTrackSearch extends SpotifyClient{

/**
* search tracks on spotify from the given mixesdb track lists
*
* @param string[] $find
*/
public function getTracks(
string $clubnightsJSON,
Expand Down Expand Up @@ -73,7 +77,15 @@ public function getTracks(
foreach($data->tracks->items as $i => $item){
$setTracks[$item->id] = $item->id;

$this->logger->info(sprintf('found: [%s][%s] %s - %s', ++$i, $item->id, implode(', ', array_column($item->artists, 'name')), $item->name));
$this->logger->info(
sprintf(
'found: [%s][%s] %s - %s',
++$i,
$item->id,
implode(', ', array_column($item->artists, 'name')),
$item->name,
),
);
}

}
Expand All @@ -97,6 +109,8 @@ public function getTracks(

/**
* check a string for the occurence of any in the given array of needles
*
* @param string[] $needles
*/
protected function setContains(string $haystack, array $needles):bool{

Expand Down
12 changes: 8 additions & 4 deletions examples/Providers/Spotify/SpotifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use chillerlan\OAuth\Providers\Spotify;

/**
*
* Extended functionality for the Spotify client
*/
class SpotifyClient extends Spotify{

Expand All @@ -22,7 +22,9 @@ class SpotifyClient extends Spotify{
protected object $me;
protected string $id;
protected string $market;
/** @var array<string, object> */
protected array $artists = [];
/** @var array<string, object> */
protected array $albums = [];

protected function construct():void{
Expand All @@ -39,7 +41,7 @@ protected function saveToFile(array $vars, string $dir):void{
foreach($vars as $var){
file_put_contents(
sprintf('%s/%s.json', rtrim($dir, '\\/'), $var),
json_encode($this->{$var}, (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))
json_encode($this->{$var}, (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)),
);
}

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

/**
* add the tracks to the given playlist
*
* @param string[] $trackIDs
*/
public function addTracks(string $playlistID, array $trackIDs):static{

$uris = array_chunk(
array_map(fn(string $t):string => 'spotify:track:'.$t , array_values($trackIDs)), // why not just ids???
100 // API max = 100 track URIs
100, // API max = 100 track URIs
);

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

usleep(self::sleepTimer);

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

$this->logger->info(sprintf('added tracks %s/%s [%s]', ++$i, count($uris), $json->snapshot_id));
Expand Down
5 changes: 3 additions & 2 deletions examples/Providers/Spotify/SpotifyNewReleases.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use chillerlan\HTTP\Utils\MessageUtil;

/**
*
* New releases crawler
*/
class SpotifyNewReleases extends SpotifyClient{

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

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

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

$this->addTracks($playlistID, $newtracks);
Expand Down
4 changes: 2 additions & 2 deletions examples/Providers/Spotify/playlist-diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function diff(string $playlistID1, string $playlistID2):array{

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

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

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

$this->addTracks($playlistID, array_keys($merged));
Expand Down
108 changes: 108 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<file>src</file>
<file>tests</file>

<config name="installed_paths" value="../../slevomat/coding-standard"/>

<arg value="ps"/>
<arg name="parallel" value="8"/>
<!--<arg name="cache" value=".build/phpcs.cache"/>-->
Expand All @@ -20,6 +22,112 @@
<type>error</type>
</rule>

<!--
Slevomat https://github.com/slevomat/coding-standard
-->

<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"/>
<rule ref="SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed"/>

<rule ref="SlevomatCodingStandard.Attributes.DisallowAttributesJoining"/>
<rule ref="SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine"/>
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>

<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
<rule ref="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion"/>
<rule ref="SlevomatCodingStandard.Classes.ForbiddenPublicProperty"/>
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>

<rule ref="SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration"/>
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
<rule ref="SlevomatCodingStandard.Commenting.UselessFunctionDocComment"/>
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment"/>

<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithoutParentheses"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
<rule ref="SlevomatCodingStandard.ControlStructures.UselessTernaryOperator"/>

<rule ref="SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch"/>

<rule ref="SlevomatCodingStandard.Functions.DisallowEmptyFunction"/>
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
<rule ref="SlevomatCodingStandard.Functions.StrictCall"/>

<rule ref="SlevomatCodingStandard.Namespaces.RequireOneNamespaceInFile"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>

<rule ref="SlevomatCodingStandard.Numbers.DisallowNumericLiteralSeparator"/>

<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators"/>
<rule ref="SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator"/>

<rule ref="SlevomatCodingStandard.PHP.OptimizedFunctionsWithoutUnpacking"/>
<rule ref="SlevomatCodingStandard.PHP.ShortList"/>
<rule ref="SlevomatCodingStandard.PHP.TypeCast"/>
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>

<rule ref="SlevomatCodingStandard.Strings.DisallowVariableParsing"/>

<!--<rule ref="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>-->
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>

<!--<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>-->

<rule ref="SlevomatCodingStandard.Variables.DisallowVariableVariable"/>
<rule ref="SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable"/>
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>

<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="linesCountBeforeDeclare" value="0"/>
<property name="linesCountAfterDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">
<properties>
<property name="minLineLength" value="131"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable">
<exclude-pattern>examples</exclude-pattern>
<exclude-pattern>src/Storage/SessionStorage.php</exclude-pattern>
<exclude-pattern>tests/Storage/SessionStorageTest.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat">
<properties>
<property name="withSpaces" value="no"/>
<property name="shortNullable" value="no"/>
<property name="nullPosition" value="last"/>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Variables.UnusedVariable">
<properties>
<property name="ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach" value="true"/>
</properties>
<exclude-pattern>examples</exclude-pattern>
</rule>


<!--
PHPCS built-in https://tentyp.dev/library/php/phpcs/
-->
Expand Down
4 changes: 4 additions & 0 deletions src/Core/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ final class AccessToken extends SettingsContainerAbstract{

/**
* (magic) The scopes that are attached to this token
*
* @var string[]
*/
protected array $scopes = [];

/**
* (magic) Additional token parameters supplied by the provider
*
* @var array<string, mixed>
*/
protected array $extraParams = [];

Expand Down
6 changes: 4 additions & 2 deletions src/Core/AuthenticatedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ final class AuthenticatedUser extends SettingsContainerAbstract{

/**
* (magic) The full user endpoint response
*
* @var array<string, mixed>
*/
protected array $data = [];

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

/** @codeCoverageIgnore */
public function __set(string $property, $value):void{
public function __set(string $property, mixed $value):void{
// noop
}

/** @codeCoverageIgnore */
public function fromIterable(iterable $properties):static{
public function fromIterable(iterable $properties):static{ // phpcs:ignore
// noop
return $this;
}
Expand Down
Loading

0 comments on commit 3754685

Please sign in to comment.