Skip to content

Commit

Permalink
cs fixes, new Utils methods, some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsizemore committed Apr 11, 2024
1 parent 8a3a90c commit ff3ac28
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 139 deletions.
30 changes: 19 additions & 11 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
->setRules([
'@PER-CS' => true,
'@PSR12' => true,
'@PHP82Migration' => true,
'@PHP81Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'php_unit_internal_class' => ['types' => ['normal', 'final']],
'php_unit_namespaced' => true,
'php_unit_expectation' => true,
'php_unit_set_up_tear_down_visibility' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag_normalizer' => true,
Expand All @@ -46,7 +44,6 @@
'phpdoc_var_without_name' => true,
'phpdoc_no_useless_inheritdoc' => true,
'align_multiline_comment' => true,
'phpdoc_add_missing_param_annotation' => ['only_untyped' => true],
'binary_operator_spaces' => [
'operators' => [
'*=' => 'align_single_space_minimal',
Expand All @@ -57,13 +54,18 @@
'=>' => 'align_single_space_minimal',
],
],
'heredoc_to_nowdoc' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const',]],
'no_unused_imports' => true,
'single_import_per_statement' => true,
'declare_equal_normalize' => ['space' => 'none'],
'declare_parentheses' => true,
'declare_strict_types' => true,
'heredoc_to_nowdoc' => true,
'fully_qualified_strict_types' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const',]],
'no_leading_import_slash' => true,
'no_unneeded_import_alias' => true,
'no_unused_imports' => true,
'single_import_per_statement' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true],
'native_constant_invocation' => ['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_INT_SIZE', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced', 'strict' => true],
'declare_equal_normalize' => ['space' => 'none'],
'declare_parentheses' => true,
'declare_strict_types' => true,
//'global_namespace_import' => ['import_classes' => true, 'import_constants' => true, 'import_functions' => true],
'header_comment' => ['comment_type' => 'PHPDoc', 'header' => $header, 'separate' => 'top'],
'ordered_class_elements' => [
Expand All @@ -74,15 +76,21 @@
'constant_protected',
'constant_private',
'property_public',
'property_public_static',
'property_protected',
'property_protected_static',
'property_private',
'property_private_static',
'construct',
'destruct',
'magic',
'phpunit',
'method_public',
'method_public_static',
'method_protected',
'method_protected_static',
'method_private',
'method_private_static',
],
'sort_algorithm' => 'alpha',
],
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ However, if you make use of the `raw`, `toArray`, or `toObject` functions, pleas
* Exception\InvalidEndpointException
* Exception\InvalidEndpointOptionsException
* `vimeo/psalm` as a dev dependency
* New `Utils` functions:
* `validatePagination()` - to not only make sure the `page` and `per_page` are integers, but also locks to a min/max range.
* `validateCachePath()` - simplifies the cachePath check in `AbstractClient`.

### Changed

Expand All @@ -33,6 +36,7 @@ However, if you make use of the `raw`, `toArray`, or `toObject` functions, pleas
* The `LibrariesIO` class now only defines functions to access the API endpoints and leaves the rest of the work up to `AbstractClient` and `Utils`.
* `Exception\RateLimitExceededException` now takes `GuzzleHttp\Exception\ClientException` as a parameter.
* Fixes to both code and docblocks/etc. throughout per Psalm.
* Updated PHP-CS-Fixer configuration and applied fixes per those rules throughout.

### Removed

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"require-dev": {
"esi/phpunit-coverage-check": "^1.0",
"friendsofphp/php-cs-fixer": "dev-master",
"friendsofphp/php-cs-fixer": "^3.53",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.11 <2.0",
"phpstan/phpstan-phpunit": "^1.4",
Expand Down
36 changes: 15 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

use function array_filter;
use function array_merge;
use function is_array;
use function is_dir;
use function is_writable;
use function preg_match;

use const ARRAY_FILTER_USE_BOTH;
Expand Down Expand Up @@ -74,14 +71,15 @@ protected function __construct(#[SensitiveParameter] string $apiKey, ?string $ca
// To ease unit testing and handle cache...
/** @var array{_mockHandler?: MockHandler} $clientOptions */
$handlerStack = HandlerStack::create($clientOptions['_mockHandler'] ?? null);
unset($clientOptions['_mockHandler']);

if (is_dir((string) $cachePath) && is_writable((string) $cachePath)) {
$handlerStack->push(middleware: new CacheMiddleware(cacheStrategy: new PrivateCacheStrategy(
cache: new Psr6CacheStorage(cachePool: new FilesystemAdapter(namespace: 'libIo', defaultLifetime: 300, directory: $cachePath))
)), name: 'cache');
}
$cachePath = Utils::validateCachePath($cachePath);

unset($clientOptions['_mockHandler']);
if ($cachePath !== null) {
$handlerStack->push(new CacheMiddleware(new PrivateCacheStrategy(
new Psr6CacheStorage(new FilesystemAdapter('libIo', 300, $cachePath))
)), 'cache');
}

$this->client = new Client(array_merge([
'base_uri' => self::API_URL,
Expand Down Expand Up @@ -113,7 +111,7 @@ protected function request(string $method, string $endpoint, ?array $options = n
],
];

if (isset($options['query']) && is_array($options['query'])) {
if (isset($options['query']) && \is_array($options['query'])) {
$requestOptions['query'] += $options['query'];

unset($options['query']);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/RateLimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(private readonly ClientException $clientException)
]
);

parent::__construct($message, 0, $clientException);
parent::__construct($message, $clientException->getCode(), $clientException);
}

public function getResponse(): ResponseInterface
Expand Down
27 changes: 14 additions & 13 deletions src/LibrariesIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use SensitiveParameter;

/**
* @see \Esi\LibrariesIO\Tests\LibrariesIOTest
* @see Tests\LibrariesIOTest
*
* @psalm-api
*/
Expand Down Expand Up @@ -64,9 +64,11 @@ public function project(string $endpoint, array $options): ResponseInterface
{
[$endpointFormat, $requestMethod] = Utils::endpointParameters('project', $endpoint, $options);

$options = Utils::validatePagination($options);

$query = [
'page' => $options['page'] ?? 1,
'per_page' => $options['per_page'] ?? 30,
'page' => $options['page'],
'per_page' => $options['per_page'],
];

// If on the 'search' endpoint, we have to provide the query and sort parameters.
Expand All @@ -75,12 +77,7 @@ public function project(string $endpoint, array $options): ResponseInterface
'q' => $options['query'],
'sort' => Utils::searchVerifySortOption((string) $options['sort']),
];

$additionalParams = Utils::searchAdditionalParams($options);

if ($additionalParams !== []) {
$query += $additionalParams;
}
$query += Utils::searchAdditionalParams($options);
}

$query = ['query' => $query];
Expand All @@ -103,10 +100,12 @@ public function repository(string $endpoint, array $options): ResponseInterface
{
[$endpointFormat, $requestMethod] = Utils::endpointParameters('repository', $endpoint, $options);

$options = Utils::validatePagination($options);

$query = [
'query' => [
'page' => $options['page'] ?? 1,
'per_page' => $options['per_page'] ?? 30,
'page' => $options['page'],
'per_page' => $options['per_page'],
],
];

Expand Down Expand Up @@ -152,10 +151,12 @@ public function user(string $endpoint, array $options): ResponseInterface
{
[$endpointFormat, $requestMethod] = Utils::endpointParameters('user', $endpoint, $options);

$options = Utils::validatePagination($options);

$query = [
'query' => [
'page' => $options['page'] ?? 1,
'per_page' => $options['per_page'] ?? 30,
'page' => $options['page'],
'per_page' => $options['per_page'],
],
];

Expand Down

0 comments on commit ff3ac28

Please sign in to comment.