From c507737e4097a8b49fb6612cdcdde8156de0a28e Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 28 Jun 2020 12:42:30 +0100 Subject: [PATCH 1/3] Switch to PSR18 client impl --- .styleci.yml | 10 +- .travis.yml | 16 ++- README.md | 39 +++++--- composer.json | 97 ++++++++++--------- lib/Gitlab/Api/AbstractApi.php | 2 +- lib/Gitlab/Api/Groups.php | 6 +- lib/Gitlab/Api/Issues.php | 10 +- lib/Gitlab/Api/MergeRequests.php | 2 +- lib/Gitlab/Api/ProjectNamespaces.php | 2 +- lib/Gitlab/Api/Projects.php | 30 +++--- lib/Gitlab/Api/Repositories.php | 4 +- lib/Gitlab/Api/Users.php | 2 +- lib/Gitlab/Client.php | 12 +-- lib/Gitlab/HttpClient/Builder.php | 19 ++-- lib/Gitlab/HttpClient/Plugin/ApiVersion.php | 8 +- .../HttpClient/Plugin/Authentication.php | 5 +- .../Plugin/GitlabExceptionThrower.php | 5 +- lib/Gitlab/HttpClient/Plugin/History.php | 9 +- lib/Gitlab/HttpClient/Plugin/HistoryTrait.php | 24 ----- lib/Gitlab/Model/AbstractModel.php | 3 +- lib/Gitlab/Model/Badge.php | 2 +- lib/Gitlab/Model/Branch.php | 2 +- lib/Gitlab/Model/Commit.php | 2 +- lib/Gitlab/Model/CommitNote.php | 2 +- lib/Gitlab/Model/Comparison.php | 2 +- lib/Gitlab/Model/Contributor.php | 2 +- lib/Gitlab/Model/Diff.php | 2 +- lib/Gitlab/Model/Event.php | 2 +- lib/Gitlab/Model/File.php | 2 +- lib/Gitlab/Model/Group.php | 4 +- lib/Gitlab/Model/GroupMilestone.php | 4 +- lib/Gitlab/Model/Hook.php | 2 +- lib/Gitlab/Model/Issue.php | 2 +- lib/Gitlab/Model/IssueLink.php | 2 +- lib/Gitlab/Model/Job.php | 2 +- lib/Gitlab/Model/Key.php | 2 +- lib/Gitlab/Model/Label.php | 2 +- lib/Gitlab/Model/MergeRequest.php | 2 +- lib/Gitlab/Model/Milestone.php | 2 +- lib/Gitlab/Model/Node.php | 2 +- lib/Gitlab/Model/Note.php | 4 +- lib/Gitlab/Model/Pipeline.php | 2 +- lib/Gitlab/Model/Project.php | 32 +++--- lib/Gitlab/Model/ProjectHook.php | 2 +- lib/Gitlab/Model/ProjectNamespace.php | 2 +- lib/Gitlab/Model/Release.php | 10 +- lib/Gitlab/Model/Schedule.php | 2 +- lib/Gitlab/Model/Session.php | 2 +- lib/Gitlab/Model/Snippet.php | 2 +- lib/Gitlab/Model/Tag.php | 2 +- lib/Gitlab/Model/User.php | 2 +- lib/Gitlab/Model/Wiki.php | 2 +- phpstan.neon.dist | 4 + test/Gitlab/Tests/Api/AbstractApiTest.php | 7 +- test/Gitlab/Tests/Api/GroupBoardsTest.php | 18 ++-- test/Gitlab/Tests/Api/IssueBoardsTest.php | 18 ++-- test/Gitlab/Tests/Api/JobsTest.php | 2 +- test/Gitlab/Tests/Api/MergeRequestsTest.php | 4 +- test/Gitlab/Tests/Api/ProjectsTest.php | 80 ++++++++++----- test/Gitlab/Tests/Api/ScheduleTest.php | 3 +- test/Gitlab/Tests/Api/TestCase.php | 4 +- test/Gitlab/Tests/Api/UsersTest.php | 17 ++-- test/Gitlab/Tests/Api/WikiTest.php | 3 +- test/Gitlab/Tests/HttpClient/BuilderTest.php | 4 +- test/Gitlab/Tests/Model/ProjectTest.php | 12 ++- test/Gitlab/Tests/ResultPagerTest.php | 6 +- 66 files changed, 318 insertions(+), 277 deletions(-) delete mode 100644 lib/Gitlab/HttpClient/Plugin/HistoryTrait.php create mode 100644 phpstan.neon.dist diff --git a/.styleci.yml b/.styleci.yml index 247a09c5d..9f3f02fcc 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1 +1,9 @@ -preset: psr2 +preset: psr12 + +enabled: + - phpdoc_indent + - phpdoc_scalar + +disabled: + - blank_line_after_opening_tag + - concat_with_spaces diff --git a/.travis.yml b/.travis.yml index 325df3dc6..c63d39cf5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,24 +1,22 @@ language: php +dist: bionic + php: - - 5.6 - - 7.0 - 7.1 - 7.2 - 7.3 - 7.4 matrix: - # test latest PHP stable version with lowest dependencies and phpstan static analysis include: - - php: 7.3 - env: STATIC_ANALYSIS=yes + - php: 7.4 + name: phpstan + script: + - vendor/bin/phpstan analyse --no-progress -before_script: - - travis_retry composer self-update +install: - travis_retry composer update --no-interaction ${COMPOSER_FLAGS} - - if [ "$STATIC_ANALYSIS" != "" ]; then curl -L https://github.com/phpstan/phpstan/releases/download/0.8/phpstan.phar -o phpstan.phar; fi; script: - vendor/bin/phpunit --verbose --coverage-text - - if [ "$STATIC_ANALYSIS" != "" ]; then php phpstan.phar analyse --level=4 lib; fi; diff --git a/README.md b/README.md index 103e7d971..67d4f2e88 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,33 @@ Based on [php-github-api](https://github.com/m4tthumphrey/php-github-api) and co Installation ------------ -Via [composer](https://getcomposer.org) +Via [Composer](https://getcomposer.org). + +### PHP 7.2+: + +```bash +composer require m4tthumphrey/php-gitlab-api guzzlehttp/guzzle:^7.0.1 +``` + +### PHP 7.1+: + +```bash +composer require m4tthumphrey/php-gitlab-api php-http/guzzle6-adapter:^2.0.1 +``` + +### Laravel 5.5+: ```bash -composer require m4tthumphrey/php-gitlab-api php-http/guzzle6-adapter:^1.0 +composer require graham-campbell/gitlab guzzlehttp/guzzle:^7.0.1 ``` -Why `php-http/guzzle6-adapter`? We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). +### Symfony 3.4+: -You can visit [HTTPlug for library users](http://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages. +```bash +composer require zeichen32/gitlabapibundle guzzlehttp/guzzle:^7.0.1 +``` + +We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). You can visit [HTTPlug for library users](https://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages. [graham-campbell/gitlab](https://github.com/GrahamCampbell/Laravel-GitLab) is by [Graham Campbell](https://github.com/GrahamCampbell) and [zeichen32/gitlabapibundle](https://github.com/Zeichen32/GitLabApiBundle) is by [Jens Averkamp](https://github.com/Zeichen32). Versioning ---------- @@ -98,18 +116,9 @@ $issue = $project->createIssue('This does not work.', array( $issue->close(); ``` -You get the idea! Take a look around ([API methods](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Api), -[models](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Model)) and please feel free to report any bugs. - -Framework Integrations ----------------------- -- **Symfony** - https://github.com/Zeichen32/GitLabApiBundle -- **Laravel** - https://github.com/GrahamCampbell/Laravel-GitLab - -If you have integrated GitLab into a popular PHP framework, let us know! +You get the idea! Take a look around ([API methods](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Api), [models](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Model)) and please feel free to report any bugs. If you have integrated GitLab into a popular PHP framework, let us know! Contributing ------------ -There are many parts of Gitlab that I have not added to this as it was originally created for personal use, hence the -lack of tests. Feel free to fork and add new functionality and tests, I'll gladly accept decent pull requests. +There are many parts of Gitlab that I have not added to this as it was originally created for personal use, hence the lack of tests. Feel free to fork and add new functionality and tests, I'll gladly accept decent pull requests. diff --git a/composer.json b/composer.json index fba3182ac..282acb869 100644 --- a/composer.json +++ b/composer.json @@ -1,50 +1,51 @@ { - "name": "m4tthumphrey/php-gitlab-api", - "type": "library", - "description": "GitLab API client", - "homepage": "https://github.com/m4tthumphrey/php-gitlab-api", - "keywords": ["gitlab", "api"], - "license": "MIT", - "authors": [ - { - "name": "Matt Humphrey", - "homepage": "http://m4tt.io" - }, - { - "name": "KnpLabs Team", - "homepage": "http://knplabs.com" - }, - { - "name": "Thibault Duplessis", - "email": "thibault.duplessis@gmail.com", - "homepage": "http://ornicar.github.com" - } - ], - "require": { - "php": "^5.6 || ^7.0", - "ext-xml": "*", - "php-http/client-common": "^1.6 || ^2.0", - "php-http/client-implementation": "^1.0", - "php-http/discovery": "^1.2", - "php-http/httplug": "^1.1 || ^2.0", - "php-http/multipart-stream-builder": "^1.0", - "symfony/options-resolver": "^2.6 || ^3.0 || ^4.0 || ^5.0" - }, - "require-dev": { - "guzzlehttp/psr7": "^1.2", - "php-http/guzzle6-adapter": "^1.0 || ^2.0", - "php-http/mock-client": "^1.2", - "phpunit/phpunit": "^5.7.27 || ^7.5.15" - }, - "autoload": { - "psr-4": { "Gitlab\\": "lib/Gitlab/" } - }, - "autoload-dev": { - "psr-4": { "Gitlab\\Tests\\": "test/Gitlab/Tests/" } - }, - "extra": { - "branch-alias": { - "dev-master": "9.17-dev" - } - } + "name": "m4tthumphrey/php-gitlab-api", + "type": "library", + "description": "GitLab API client", + "homepage": "https://github.com/m4tthumphrey/php-gitlab-api", + "keywords": ["gitlab", "api"], + "license": "MIT", + "authors": [ + { + "name": "Matt Humphrey", + "homepage": "http://m4tt.io" + }, + { + "name": "KnpLabs Team", + "homepage": "http://knplabs.com" + }, + { + "name": "Thibault Duplessis", + "email": "thibault.duplessis@gmail.com", + "homepage": "http://ornicar.github.com" + } + ], + "require": { + "php": "^7.1", + "ext-xml": "*", + "php-http/client-common": "^2.1", + "php-http/cache-plugin": "^1.7", + "php-http/discovery": "^1.7", + "php-http/httplug": "^2.1", + "php-http/multipart-stream-builder": "^1.1", + "symfony/options-resolver": "^3.4 || ^4.0 || ^5.0" + }, + "require-dev": { + "guzzlehttp/psr7": "^1.2", + "php-http/guzzle6-adapter": "^2.0", + "php-http/mock-client": "^1.2", + "phpunit/phpunit": "^7.5.15", + "phpstan/phpstan": "^0.12.23" + }, + "autoload": { + "psr-4": { "Gitlab\\": "lib/Gitlab/" } + }, + "autoload-dev": { + "psr-4": { "Gitlab\\Tests\\": "test/Gitlab/Tests/" } + }, + "extra": { + "branch-alias": { + "dev-master": "9.17-dev" + } + } } diff --git a/lib/Gitlab/Api/AbstractApi.php b/lib/Gitlab/Api/AbstractApi.php index f63cfb4ab..faae682d8 100644 --- a/lib/Gitlab/Api/AbstractApi.php +++ b/lib/Gitlab/Api/AbstractApi.php @@ -248,7 +248,7 @@ private function preparePath($path, array $parameters = []) } /** - * @param $file + * @param string $file * * @return string */ diff --git a/lib/Gitlab/Api/Groups.php b/lib/Gitlab/Api/Groups.php index b771dc579..cca20c9e2 100644 --- a/lib/Gitlab/Api/Groups.php +++ b/lib/Gitlab/Api/Groups.php @@ -90,8 +90,8 @@ public function transfer($group_id, $project_id) } /** - * @param integer $id - * @param integer|null $user_id + * @param int $id + * @param int|null $user_id * @param array $parameters * @return mixed */ @@ -158,7 +158,7 @@ public function removeMember($group_id, $user_id) } /** - * @param $id + * @param int $id * @param array $parameters ( * * @var bool $archived Limit by archived status. diff --git a/lib/Gitlab/Api/Issues.php b/lib/Gitlab/Api/Issues.php index 17ef2bf2c..88c5c1e9b 100644 --- a/lib/Gitlab/Api/Issues.php +++ b/lib/Gitlab/Api/Issues.php @@ -301,10 +301,10 @@ public function awardEmoji($project_id, $issue_iid) } /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ + * @param int $project_id + * @param int $issue_iid + * @return mixed + */ public function closedByMergeRequests($project_id, $issue_iid) { return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/closed_by'); @@ -319,7 +319,7 @@ public function showParticipants($project_id, $issue_iid) { return $this->get($this->getProjectPath($project_id, 'issues/' .$this->encodePath($issue_iid)).'/participants'); } - + /** * {@inheritDoc} */ diff --git a/lib/Gitlab/Api/MergeRequests.php b/lib/Gitlab/Api/MergeRequests.php index 780ddd6b2..3da019d19 100644 --- a/lib/Gitlab/Api/MergeRequests.php +++ b/lib/Gitlab/Api/MergeRequests.php @@ -158,7 +158,7 @@ public function update($project_id, $mr_id, array $params) /** * @param int $project_id * @param int $mr_id - * @param string $message + * @param string|array $message * @return mixed */ public function merge($project_id, $mr_id, $message = null) diff --git a/lib/Gitlab/Api/ProjectNamespaces.php b/lib/Gitlab/Api/ProjectNamespaces.php index a9643db6f..9aaaa2143 100644 --- a/lib/Gitlab/Api/ProjectNamespaces.php +++ b/lib/Gitlab/Api/ProjectNamespaces.php @@ -19,7 +19,7 @@ public function all(array $parameters = []) } /** - * @param integer|string $namespace_id + * @param int|string $namespace_id * @return mixed */ public function show($namespace_id) diff --git a/lib/Gitlab/Api/Projects.php b/lib/Gitlab/Api/Projects.php index cb6841ff8..294528d76 100644 --- a/lib/Gitlab/Api/Projects.php +++ b/lib/Gitlab/Api/Projects.php @@ -287,10 +287,10 @@ public function cancelPipeline($project_id, $pipeline_id) { return $this->post($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id)).'/cancel'); } - + /** - * @param $project_id - * @param $pipeline_id + * @param int $project_id + * @param int $pipeline_id * @return mixed */ public function deletePipeline($project_id, $pipeline_id) @@ -299,8 +299,8 @@ public function deletePipeline($project_id, $pipeline_id) } /** - * @param integer $project_id - * @param integer|null $user_id + * @param int $project_id + * @param int|null $user_id * @param array $parameters * @return mixed */ @@ -315,11 +315,10 @@ public function allMembers($project_id, $user_id = null, $parameters = []) /** * @param int $project_id * @param array $parameters ( - * * @var string $query The query you want to search members for. * ) * - * @throws MissingOptionsException If a required option is not provided + * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException If a required option is not provided * * @return mixed */ @@ -664,32 +663,31 @@ public function languages($project_id) /** * @param int $project_id - * @param array $parameters + * @param array $params * @return mixed */ - public function forks($project_id, array $parameters = []) + public function forks($project_id, array $params = []) { $resolver = $this->createOptionsResolver(); - return $this->get($this->getProjectPath($project_id, 'forks'), $resolver->resolve($parameters)); + return $this->get($this->getProjectPath($project_id, 'forks'), $resolver->resolve($params)); } /** * @param int $project_id * @param array $params ( - * * @var string $namespace The ID or path of the namespace that the project will be forked to * @var string $path The path of the forked project (optional) * @var string $name The name of the forked project (optional) * ) * @return mixed */ - public function fork($project_id, array $parameters = []) + public function fork($project_id, array $params = []) { $resolver = new OptionsResolver(); $resolver->setDefined(['namespace', 'path', 'name']); - $resolved = $resolver->resolve($parameters); + $resolved = $resolver->resolve($params); return $this->post($this->getProjectPath($project_id, 'fork'), $resolved); } @@ -848,7 +846,7 @@ public function deployment($project_id, $deployment_id) { return $this->get($this->getProjectPath($project_id, 'deployments/'.$this->encodePath($deployment_id))); } - + /** * @param mixed $project_id * @param array $parameters @@ -876,7 +874,7 @@ public function addShare($project_id, array $parameters = []) return $this->post($this->getProjectPath($project_id, 'share'), $resolver->resolve($parameters)); } - + /** * @param mixed $project_id * @param int $group_id @@ -931,7 +929,7 @@ public function removeBadge($project_id, $badge_id) * @param string $badge_id * @param array $params * @return mixed - */ + */ public function updateBadge($project_id, $badge_id, array $params = array()) { return $this->put($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id)), $params); diff --git a/lib/Gitlab/Api/Repositories.php b/lib/Gitlab/Api/Repositories.php index a52369c42..c43f9f331 100644 --- a/lib/Gitlab/Api/Repositories.php +++ b/lib/Gitlab/Api/Repositories.php @@ -188,7 +188,7 @@ public function commits($project_id, array $parameters = []) /** * @param int $project_id - * @param $sha + * @param string $sha * @return mixed */ public function commit($project_id, $sha) @@ -198,7 +198,7 @@ public function commit($project_id, $sha) /** * @param int $project_id - * @param $sha + * @param string $sha * @param array $parameters * @return mixed */ diff --git a/lib/Gitlab/Api/Users.php b/lib/Gitlab/Api/Users.php index 7c786a5d8..e416fd109 100644 --- a/lib/Gitlab/Api/Users.php +++ b/lib/Gitlab/Api/Users.php @@ -323,7 +323,7 @@ public function emails() } /** - * @param $id + * @param int $id * @return mixed */ public function email($id) diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php index a981ec156..7f19a0ba6 100644 --- a/lib/Gitlab/Client.php +++ b/lib/Gitlab/Client.php @@ -12,8 +12,8 @@ use Http\Client\Common\Plugin\HeaderDefaultsPlugin; use Http\Client\Common\Plugin\HistoryPlugin; use Http\Client\Common\Plugin\RedirectPlugin; -use Http\Client\HttpClient; use Http\Discovery\UriFactoryDiscovery; +use Psr\Http\Client\ClientInterface; /** * Simple API wrapper for Gitlab @@ -107,13 +107,13 @@ public static function create($url) } /** - * Create a Gitlab\Client using an HttpClient. + * Create a Gitlab\Client using an Http client. * - * @param HttpClient $httpClient + * @param ClientInterface $httpClient * * @return Client */ - public static function createWithHttpClient(HttpClient $httpClient) + public static function createWithHttpClient(ClientInterface $httpClient) { $builder = new Builder($httpClient); @@ -336,7 +336,7 @@ public function api($name) case 'groups': return $this->groups(); - + case 'groupsMilestones': return $this->groupsMilestones(); @@ -377,7 +377,7 @@ public function api($name) case 'repositoryFiles': return $this->repositoryFiles(); - + case 'snippets': return $this->snippets(); diff --git a/lib/Gitlab/HttpClient/Builder.php b/lib/Gitlab/HttpClient/Builder.php index 8c6f586eb..6803ed691 100644 --- a/lib/Gitlab/HttpClient/Builder.php +++ b/lib/Gitlab/HttpClient/Builder.php @@ -6,13 +6,12 @@ use Http\Client\Common\Plugin; use Http\Client\Common\PluginClient; use Http\Client\Common\PluginClientFactory; -use Http\Client\HttpClient; -use Http\Discovery\HttpClientDiscovery; use Http\Discovery\MessageFactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; use Http\Discovery\StreamFactoryDiscovery; -use Http\Message\MessageFactory; use Http\Message\RequestFactory; use Http\Message\StreamFactory; +use Psr\Http\Client\ClientInterface; /** * A builder that builds the API client. @@ -25,7 +24,7 @@ class Builder /** * The object that sends HTTP messages. * - * @var HttpClient + * @var ClientInterface */ private $httpClient; @@ -37,7 +36,7 @@ class Builder private $pluginClient; /** - * @var MessageFactory + * @var RequestFactory */ private $requestFactory; @@ -59,16 +58,16 @@ class Builder private $plugins = []; /** - * @param HttpClient $httpClient - * @param RequestFactory $requestFactory - * @param StreamFactory $streamFactory + * @param ClientInterface $httpClient + * @param RequestFactory $requestFactory + * @param StreamFactory $streamFactory */ public function __construct( - HttpClient $httpClient = null, + ClientInterface $httpClient = null, RequestFactory $requestFactory = null, StreamFactory $streamFactory = null ) { - $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); + $this->httpClient = $httpClient ?: Psr18ClientDiscovery::find(); $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); } diff --git a/lib/Gitlab/HttpClient/Plugin/ApiVersion.php b/lib/Gitlab/HttpClient/Plugin/ApiVersion.php index 4dacbe9d6..f06fa42ef 100644 --- a/lib/Gitlab/HttpClient/Plugin/ApiVersion.php +++ b/lib/Gitlab/HttpClient/Plugin/ApiVersion.php @@ -3,6 +3,7 @@ namespace Gitlab\HttpClient\Plugin; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -13,14 +14,15 @@ */ class ApiVersion implements Plugin { - use Plugin\VersionBridgePlugin; - + /** + * @var bool + */ private $redirected = false; /** * {@inheritdoc} */ - public function doHandleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $uri = $request->getUri(); diff --git a/lib/Gitlab/HttpClient/Plugin/Authentication.php b/lib/Gitlab/HttpClient/Plugin/Authentication.php index e2f30ec15..c39234b32 100644 --- a/lib/Gitlab/HttpClient/Plugin/Authentication.php +++ b/lib/Gitlab/HttpClient/Plugin/Authentication.php @@ -4,6 +4,7 @@ use Gitlab\Client; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -14,8 +15,6 @@ */ class Authentication implements Plugin { - use Plugin\VersionBridgePlugin; - /** * @var string */ @@ -46,7 +45,7 @@ public function __construct($method, $token, $sudo = null) /** * {@inheritdoc} */ - public function doHandleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { switch ($this->method) { case Client::AUTH_HTTP_TOKEN: diff --git a/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php b/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php index 6df03c8c5..00c708a45 100644 --- a/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php +++ b/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php @@ -6,6 +6,7 @@ use Gitlab\Exception\RuntimeException; use Gitlab\HttpClient\Message\ResponseMediator; use Http\Client\Common\Plugin; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -17,12 +18,10 @@ */ class GitlabExceptionThrower implements Plugin { - use Plugin\VersionBridgePlugin; - /** * {@inheritdoc} */ - public function doHandleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { return $next($request)->then(function (ResponseInterface $response) { if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { diff --git a/lib/Gitlab/HttpClient/Plugin/History.php b/lib/Gitlab/HttpClient/Plugin/History.php index dc21e2421..a0b46299a 100644 --- a/lib/Gitlab/HttpClient/Plugin/History.php +++ b/lib/Gitlab/HttpClient/Plugin/History.php @@ -3,6 +3,7 @@ namespace Gitlab\HttpClient\Plugin; use Http\Client\Common\Plugin\Journal; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -13,10 +14,8 @@ */ class History implements Journal { - use HistoryTrait; - /** - * @var ResponseInterface + * @var ResponseInterface|null */ private $lastResponse; @@ -35,4 +34,8 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons { $this->lastResponse = $response; } + + public function addFailure(RequestInterface $request, ClientExceptionInterface $exception) + { + } } diff --git a/lib/Gitlab/HttpClient/Plugin/HistoryTrait.php b/lib/Gitlab/HttpClient/Plugin/HistoryTrait.php deleted file mode 100644 index 07e9c55b2..000000000 --- a/lib/Gitlab/HttpClient/Plugin/HistoryTrait.php +++ /dev/null @@ -1,24 +0,0 @@ -hydrate($data); } diff --git a/lib/Gitlab/Model/Branch.php b/lib/Gitlab/Model/Branch.php index 8c7ef36b4..8a741bf12 100644 --- a/lib/Gitlab/Model/Branch.php +++ b/lib/Gitlab/Model/Branch.php @@ -31,7 +31,7 @@ class Branch extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $branch = new static($project, $data['name'], $client); + $branch = new self($project, $data['name'], $client); if (isset($data['commit'])) { $data['commit'] = Commit::fromArray($client, $project, $data['commit']); diff --git a/lib/Gitlab/Model/Commit.php b/lib/Gitlab/Model/Commit.php index 9b0073255..8ac2b1ce0 100644 --- a/lib/Gitlab/Model/Commit.php +++ b/lib/Gitlab/Model/Commit.php @@ -50,7 +50,7 @@ class Commit extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $commit = new static($project, $data['id'], $client); + $commit = new self($project, $data['id'], $client); if (isset($data['parents'])) { $parents = array(); diff --git a/lib/Gitlab/Model/CommitNote.php b/lib/Gitlab/Model/CommitNote.php index 4b455c7f1..d068cd337 100644 --- a/lib/Gitlab/Model/CommitNote.php +++ b/lib/Gitlab/Model/CommitNote.php @@ -31,7 +31,7 @@ class CommitNote extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $comment = new static($client); + $comment = new self($client); if (isset($data['author'])) { $data['author'] = User::fromArray($client, $data['author']); diff --git a/lib/Gitlab/Model/Comparison.php b/lib/Gitlab/Model/Comparison.php index 27901829d..8e888a904 100644 --- a/lib/Gitlab/Model/Comparison.php +++ b/lib/Gitlab/Model/Comparison.php @@ -34,7 +34,7 @@ class Comparison extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $file = new static($project, $client); + $file = new self($project, $client); if (isset($data['commit'])) { $data['commit'] = Commit::fromArray($client, $project, $data['commit']); diff --git a/lib/Gitlab/Model/Contributor.php b/lib/Gitlab/Model/Contributor.php index 658da48bc..7b51de1b8 100644 --- a/lib/Gitlab/Model/Contributor.php +++ b/lib/Gitlab/Model/Contributor.php @@ -34,7 +34,7 @@ class Contributor extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $contributor = new static($project, $client); + $contributor = new self($project, $client); return $contributor->hydrate($data); } diff --git a/lib/Gitlab/Model/Diff.php b/lib/Gitlab/Model/Diff.php index 5fd95d5ac..719f6df3c 100644 --- a/lib/Gitlab/Model/Diff.php +++ b/lib/Gitlab/Model/Diff.php @@ -40,7 +40,7 @@ class Diff extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $diff = new static($project, $client); + $diff = new self($project, $client); return $diff->hydrate($data); } diff --git a/lib/Gitlab/Model/Event.php b/lib/Gitlab/Model/Event.php index 83565e174..3652dbed3 100644 --- a/lib/Gitlab/Model/Event.php +++ b/lib/Gitlab/Model/Event.php @@ -44,7 +44,7 @@ class Event extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $event = new static($project, $client); + $event = new self($project, $client); if (isset($data['author_id'])) { $data['author'] = new User($data['author_id'], $client); diff --git a/lib/Gitlab/Model/File.php b/lib/Gitlab/Model/File.php index b2edac127..cccdf1edb 100644 --- a/lib/Gitlab/Model/File.php +++ b/lib/Gitlab/Model/File.php @@ -28,7 +28,7 @@ class File extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $file = new static($project, $data['file_path'], $client); + $file = new self($project, $data['file_path'], $client); return $file->hydrate($data); } diff --git a/lib/Gitlab/Model/Group.php b/lib/Gitlab/Model/Group.php index 89f867ce4..303da8e93 100644 --- a/lib/Gitlab/Model/Group.php +++ b/lib/Gitlab/Model/Group.php @@ -51,7 +51,7 @@ class Group extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $group = new static($data['id'], $client); + $group = new self($data['id'], $client); if (isset($data['projects'])) { $projects = array(); @@ -117,7 +117,7 @@ public function transfer($project_id) } /** - * @param integer|null $user_id + * @param int|null $user_id * @param bool $all * @return array|User */ diff --git a/lib/Gitlab/Model/GroupMilestone.php b/lib/Gitlab/Model/GroupMilestone.php index 8814cc3de..52eb2882d 100644 --- a/lib/Gitlab/Model/GroupMilestone.php +++ b/lib/Gitlab/Model/GroupMilestone.php @@ -46,7 +46,7 @@ class GroupMilestone extends AbstractModel */ public static function fromArray(Client $client, Group $group, array $data) { - $milestone = new static($group, $data['id'], $client); + $milestone = new self($group, $data['id'], $client); return $milestone->hydrate($data); } @@ -54,7 +54,7 @@ public static function fromArray(Client $client, Group $group, array $data) /** * GroupMilestone constructor. * @param Group $group - * @param $id + * @param int $id * @param Client|null $client */ public function __construct(Group $group, $id, Client $client = null) diff --git a/lib/Gitlab/Model/Hook.php b/lib/Gitlab/Model/Hook.php index d276982e2..3d0b8b391 100644 --- a/lib/Gitlab/Model/Hook.php +++ b/lib/Gitlab/Model/Hook.php @@ -27,7 +27,7 @@ class Hook extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $hook = new static($data['id'], $client); + $hook = new self($data['id'], $client); return $hook->hydrate($data); } diff --git a/lib/Gitlab/Model/Issue.php b/lib/Gitlab/Model/Issue.php index 094a547cb..cc1b8008d 100644 --- a/lib/Gitlab/Model/Issue.php +++ b/lib/Gitlab/Model/Issue.php @@ -50,7 +50,7 @@ class Issue extends AbstractModel implements Noteable */ public static function fromArray(Client $client, Project $project, array $data) { - $issue = new static($project, $data['iid'], $client); + $issue = new self($project, $data['iid'], $client); if (isset($data['author'])) { $data['author'] = User::fromArray($client, $data['author']); diff --git a/lib/Gitlab/Model/IssueLink.php b/lib/Gitlab/Model/IssueLink.php index c72f83a7f..ae15fff7a 100644 --- a/lib/Gitlab/Model/IssueLink.php +++ b/lib/Gitlab/Model/IssueLink.php @@ -27,7 +27,7 @@ class IssueLink extends AbstractModel public static function fromArray(Client $client, Project $project, array $data) { $issue = Issue::fromArray($client, $project, $data); - $issueLink = new static($issue, $data['issue_link_id'], $client); + $issueLink = new self($issue, $data['issue_link_id'], $client); return $issueLink->hydrate($data); } diff --git a/lib/Gitlab/Model/Job.php b/lib/Gitlab/Model/Job.php index 586e3a311..384436e18 100644 --- a/lib/Gitlab/Model/Job.php +++ b/lib/Gitlab/Model/Job.php @@ -52,7 +52,7 @@ class Job extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $job = new static($project, $data['id'], $client); + $job = new self($project, $data['id'], $client); if (isset($data['user'])) { $data['user'] = User::fromArray($client, $data['user']); diff --git a/lib/Gitlab/Model/Key.php b/lib/Gitlab/Model/Key.php index 5e6aebfa5..58039f162 100644 --- a/lib/Gitlab/Model/Key.php +++ b/lib/Gitlab/Model/Key.php @@ -29,7 +29,7 @@ class Key extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $key = new static($client); + $key = new self($client); return $key->hydrate($data); } diff --git a/lib/Gitlab/Model/Label.php b/lib/Gitlab/Model/Label.php index 711f09d8a..da9030c9a 100644 --- a/lib/Gitlab/Model/Label.php +++ b/lib/Gitlab/Model/Label.php @@ -28,7 +28,7 @@ class Label extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $label = new static($project, $client); + $label = new self($project, $client); return $label->hydrate($data); } diff --git a/lib/Gitlab/Model/MergeRequest.php b/lib/Gitlab/Model/MergeRequest.php index 03251f3b0..a7ddcd038 100644 --- a/lib/Gitlab/Model/MergeRequest.php +++ b/lib/Gitlab/Model/MergeRequest.php @@ -62,7 +62,7 @@ class MergeRequest extends AbstractModel implements Noteable */ public static function fromArray(Client $client, Project $project, array $data) { - $mr = new static($project, $data['id'], $client); + $mr = new self($project, $data['id'], $client); if (isset($data['author'])) { $data['author'] = User::fromArray($client, $data['author']); diff --git a/lib/Gitlab/Model/Milestone.php b/lib/Gitlab/Model/Milestone.php index 8b07d38af..926e45ab5 100644 --- a/lib/Gitlab/Model/Milestone.php +++ b/lib/Gitlab/Model/Milestone.php @@ -46,7 +46,7 @@ class Milestone extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $milestone = new static($project, $data['id'], $client); + $milestone = new self($project, $data['id'], $client); return $milestone->hydrate($data); } diff --git a/lib/Gitlab/Model/Node.php b/lib/Gitlab/Model/Node.php index bb9451687..9a66dbade 100644 --- a/lib/Gitlab/Model/Node.php +++ b/lib/Gitlab/Model/Node.php @@ -34,7 +34,7 @@ class Node extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $node = new static($project, $data['id'], $client); + $node = new self($project, $data['id'], $client); return $node->hydrate($data); } diff --git a/lib/Gitlab/Model/Note.php b/lib/Gitlab/Model/Note.php index 3bfd1eec8..e15a04b98 100644 --- a/lib/Gitlab/Model/Note.php +++ b/lib/Gitlab/Model/Note.php @@ -5,7 +5,7 @@ /** * Class Note * - * @property-read integer $id + * @property-read int $id * @property-read User $author * @property-read string $body * @property-read string $created_at @@ -40,7 +40,7 @@ class Note extends AbstractModel */ public static function fromArray(Client $client, Noteable $type, array $data) { - $comment = new static($type, $client); + $comment = new self($type, $client); if (isset($data['author'])) { $data['author'] = User::fromArray($client, $data['author']); diff --git a/lib/Gitlab/Model/Pipeline.php b/lib/Gitlab/Model/Pipeline.php index efeccbc98..8ac650e4a 100644 --- a/lib/Gitlab/Model/Pipeline.php +++ b/lib/Gitlab/Model/Pipeline.php @@ -30,7 +30,7 @@ class Pipeline extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $pipeline = new static($project, $data['id'], $client); + $pipeline = new self($project, $data['id'], $client); return $pipeline->hydrate($data); } diff --git a/lib/Gitlab/Model/Project.php b/lib/Gitlab/Model/Project.php index 471bdfa15..13832d935 100644 --- a/lib/Gitlab/Model/Project.php +++ b/lib/Gitlab/Model/Project.php @@ -103,7 +103,7 @@ class Project extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $project = new static($data['id']); + $project = new self($data['id']); $project->setClient($client); if (isset($data['owner'])) { @@ -218,7 +218,7 @@ public function remove() } /** - * @param integer|null $user_id + * @param int|null $user_id * @param bool $all * @return array|User */ @@ -554,7 +554,7 @@ public function commit($sha) * @param string $ref * @param array $parameters * - * @return Commit[] + * @return CommitNote[] * @see Repositories::commitComments() for available parameters. * */ @@ -631,8 +631,8 @@ public function blob($sha, $filepath) } /** - * @param $sha - * @param $filepath + * @param string $sha + * @param string $filepath * * @return array */ @@ -646,8 +646,8 @@ public function getFile($sha, $filepath) * @param string $content * @param string $branch_name * @param string $commit_message - * @param string $author_email - * @param string $author_name + * @param string|null $author_email + * @param string|null $author_name * @return File */ public function createFile( @@ -803,8 +803,15 @@ public function mergeRequest($id) */ public function createMergeRequest($source, $target, $title, $assignee = null, $description = null) { - $data = $this->client->mergeRequests()->create($this->id, $source, $target, $title, $assignee, $this->id, - $description); + $data = $this->client->mergeRequests()->create( + $this->id, + $source, + $target, + $title, + $assignee, + $this->id, + $description + ); return MergeRequest::fromArray($this->getClient(), $this, $data); } @@ -1281,8 +1288,7 @@ public function badges() } /** - * @param string $link_url - * @param string $color + * @param array $params * @return Badge */ public function addBadge(array $params) @@ -1293,7 +1299,7 @@ public function addBadge(array $params) } /** - * @param string $name + * @param int $badge_id * @param array $params * @return Badge */ @@ -1307,7 +1313,7 @@ public function updateBadge($badge_id, array $params) } /** - * @param string $name + * @param int $badge_id * @return bool */ public function removeBadge($badge_id) diff --git a/lib/Gitlab/Model/ProjectHook.php b/lib/Gitlab/Model/ProjectHook.php index 1789ca68b..4857ca172 100644 --- a/lib/Gitlab/Model/ProjectHook.php +++ b/lib/Gitlab/Model/ProjectHook.php @@ -44,7 +44,7 @@ class ProjectHook extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $hook = new static($project, $data['id'], $client); + $hook = new self($project, $data['id'], $client); return $hook->hydrate($data); } diff --git a/lib/Gitlab/Model/ProjectNamespace.php b/lib/Gitlab/Model/ProjectNamespace.php index 9e3aee4ed..5e8e062fb 100644 --- a/lib/Gitlab/Model/ProjectNamespace.php +++ b/lib/Gitlab/Model/ProjectNamespace.php @@ -37,7 +37,7 @@ class ProjectNamespace extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $project = new static($data['id']); + $project = new self($data['id']); $project->setClient($client); return $project->hydrate($data); diff --git a/lib/Gitlab/Model/Release.php b/lib/Gitlab/Model/Release.php index 6044c871a..b22235ba7 100644 --- a/lib/Gitlab/Model/Release.php +++ b/lib/Gitlab/Model/Release.php @@ -20,21 +20,19 @@ class Release extends AbstractModel ); /** - * @param Client $client - * @param array $data + * @param Client $client + * @param array $data * @return Release */ public static function fromArray(Client $client, array $data) { - $release = new static($client); + $release = new self($client); return $release->hydrate($data); } /** - * @param Project $project - * @param int $id - * @param Client $client + * @param Client $client */ public function __construct(Client $client = null) { diff --git a/lib/Gitlab/Model/Schedule.php b/lib/Gitlab/Model/Schedule.php index ff7ba054a..8c6cc9b5f 100644 --- a/lib/Gitlab/Model/Schedule.php +++ b/lib/Gitlab/Model/Schedule.php @@ -46,7 +46,7 @@ class Schedule extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $schedule = new static($project, $data['id'], $client); + $schedule = new self($project, $data['id'], $client); return $schedule->hydrate($data); } diff --git a/lib/Gitlab/Model/Session.php b/lib/Gitlab/Model/Session.php index 7cf4cfe79..bca2c429c 100644 --- a/lib/Gitlab/Model/Session.php +++ b/lib/Gitlab/Model/Session.php @@ -33,7 +33,7 @@ class Session extends AbstractModel */ public static function fromArray(Client $client, array $data) { - $session = new static($client); + $session = new self($client); return $session->hydrate($data); } diff --git a/lib/Gitlab/Model/Snippet.php b/lib/Gitlab/Model/Snippet.php index 1f021bcb4..41bdadcb5 100644 --- a/lib/Gitlab/Model/Snippet.php +++ b/lib/Gitlab/Model/Snippet.php @@ -36,7 +36,7 @@ class Snippet extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $snippet = new static($project, $data['id'], $client); + $snippet = new self($project, $data['id'], $client); if (isset($data['author'])) { $data['author'] = User::fromArray($client, $data['author']); diff --git a/lib/Gitlab/Model/Tag.php b/lib/Gitlab/Model/Tag.php index 643edd0f2..90d7c2216 100644 --- a/lib/Gitlab/Model/Tag.php +++ b/lib/Gitlab/Model/Tag.php @@ -32,7 +32,7 @@ class Tag extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $branch = new static($project, $data['name'], $client); + $branch = new self($project, $data['name'], $client); if (isset($data['commit'])) { $data['commit'] = Commit::fromArray($client, $project, $data['commit']); diff --git a/lib/Gitlab/Model/User.php b/lib/Gitlab/Model/User.php index ef654756d..34ea01446 100644 --- a/lib/Gitlab/Model/User.php +++ b/lib/Gitlab/Model/User.php @@ -72,7 +72,7 @@ public static function fromArray(Client $client, array $data) { $id = isset($data['id']) ? $data['id'] : 0; - $user = new static($id, $client); + $user = new self($id, $client); return $user->hydrate($data); } diff --git a/lib/Gitlab/Model/Wiki.php b/lib/Gitlab/Model/Wiki.php index 2e6856272..99050de02 100644 --- a/lib/Gitlab/Model/Wiki.php +++ b/lib/Gitlab/Model/Wiki.php @@ -34,7 +34,7 @@ class Wiki extends AbstractModel */ public static function fromArray(Client $client, Project $project, array $data) { - $wiki = new static($project, $data['slug'], $client); + $wiki = new self($project, $data['slug'], $client); return $wiki->hydrate($data); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 000000000..1c57e1fef --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: 4 + paths: + - lib diff --git a/test/Gitlab/Tests/Api/AbstractApiTest.php b/test/Gitlab/Tests/Api/AbstractApiTest.php index eaae170c4..a08d813c1 100644 --- a/test/Gitlab/Tests/Api/AbstractApiTest.php +++ b/test/Gitlab/Tests/Api/AbstractApiTest.php @@ -1,9 +1,10 @@ getMockBuilder(HttpClient::class) + $httpClient = $this->getMockBuilder(ClientInterface::class) ->setMethods(array('sendRequest')) ->getMock() ; @@ -46,7 +47,7 @@ protected function getAbstractApiMock(array $methods = []) ; $client = Client::createWithHttpClient($httpClient); - $abstractApiMock = $this->getMockBuilder('Gitlab\Api\AbstractApi') + $abstractApiMock = $this->getMockBuilder(AbstractApi::class) ->setConstructorArgs([ $client, null diff --git a/test/Gitlab/Tests/Api/GroupBoardsTest.php b/test/Gitlab/Tests/Api/GroupBoardsTest.php index 8f9b9aa28..312d136a6 100644 --- a/test/Gitlab/Tests/Api/GroupBoardsTest.php +++ b/test/Gitlab/Tests/Api/GroupBoardsTest.php @@ -38,7 +38,7 @@ public function shouldShowIssueBoard() $this->assertEquals($expectedArray, $api->show(1, 2)); } - + /** * @test */ @@ -121,7 +121,7 @@ public function shouldGetAllLists() ->with('groups/1/boards/2/lists') ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->allLists(1, 2)); } @@ -141,14 +141,14 @@ public function shouldGetList() 'position' => 3 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') ->with('groups/1/boards/2/lists/3') ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->showList(1, 2, 3)); } @@ -168,7 +168,7 @@ public function shouldCreateList() 'position' => 3 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') @@ -195,14 +195,14 @@ public function shouldUpdateList() 'position' => 1 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') ->with('groups/5/boards/2/lists/3', array('position' => 1)) ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->updateList(5, 2, 3, 1)); } @@ -212,14 +212,14 @@ public function shouldUpdateList() public function shouldDeleteList() { $expectedBool = true; - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') ->with('groups/1/boards/2/lists/3') ->will($this->returnValue($expectedBool)) ; - + $this->assertEquals($expectedBool, $api->deleteList(1, 2, 3)); } diff --git a/test/Gitlab/Tests/Api/IssueBoardsTest.php b/test/Gitlab/Tests/Api/IssueBoardsTest.php index 2b90db426..7b49fa442 100644 --- a/test/Gitlab/Tests/Api/IssueBoardsTest.php +++ b/test/Gitlab/Tests/Api/IssueBoardsTest.php @@ -38,7 +38,7 @@ public function shouldShowIssueBoard() $this->assertEquals($expectedArray, $api->show(1, 2)); } - + /** * @test */ @@ -121,7 +121,7 @@ public function shouldGetAllLists() ->with('projects/1/boards/2/lists') ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->allLists(1, 2)); } @@ -141,14 +141,14 @@ public function shouldGetList() 'position' => 3 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') ->with('projects/1/boards/2/lists/3') ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->showList(1, 2, 3)); } @@ -168,7 +168,7 @@ public function shouldCreateList() 'position' => 3 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') @@ -195,14 +195,14 @@ public function shouldUpdateList() 'position' => 1 ) ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') ->with('projects/5/boards/2/lists/3', array('position' => 1)) ->will($this->returnValue($expectedArray)) ; - + $this->assertEquals($expectedArray, $api->updateList(5, 2, 3, 1)); } @@ -212,14 +212,14 @@ public function shouldUpdateList() public function shouldDeleteList() { $expectedBool = true; - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') ->with('projects/1/boards/2/lists/3') ->will($this->returnValue($expectedBool)) ; - + $this->assertEquals($expectedBool, $api->deleteList(1, 2, 3)); } diff --git a/test/Gitlab/Tests/Api/JobsTest.php b/test/Gitlab/Tests/Api/JobsTest.php index 63f9e2f9a..eb0958c82 100644 --- a/test/Gitlab/Tests/Api/JobsTest.php +++ b/test/Gitlab/Tests/Api/JobsTest.php @@ -101,7 +101,7 @@ public function shouldGetArtifactsByRefName() $this->assertEquals('foobar', $api->artifactsByRefName(1, 'master', 'job_name')->getContents()); } - + /** * @test */ diff --git a/test/Gitlab/Tests/Api/MergeRequestsTest.php b/test/Gitlab/Tests/Api/MergeRequestsTest.php index 4cfa56ce2..5acd9ce2a 100644 --- a/test/Gitlab/Tests/Api/MergeRequestsTest.php +++ b/test/Gitlab/Tests/Api/MergeRequestsTest.php @@ -108,7 +108,7 @@ public function shouldShowMergeRequest() $this->assertEquals($expectedArray, $api->show(1, 2)); } - + /** * @test */ @@ -120,7 +120,7 @@ public function shouldShowMergeRequestWithOptionalParameters() 'diverged_commits_count' => 0, 'rebase_in_progress' => false ); - + $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') diff --git a/test/Gitlab/Tests/Api/ProjectsTest.php b/test/Gitlab/Tests/Api/ProjectsTest.php index 753c3dd1c..fbe76988b 100644 --- a/test/Gitlab/Tests/Api/ProjectsTest.php +++ b/test/Gitlab/Tests/Api/ProjectsTest.php @@ -21,11 +21,16 @@ public function shouldGetAllProjectsSortedByName() { $expectedArray = $this->getMultipleProjectsData(); - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, - ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']); + $api = $this->getMultipleProjectsRequestMock( + 'projects', + $expectedArray, + ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc'] + ); - $this->assertEquals($expectedArray, - $api->all(['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc'])); + $this->assertEquals( + $expectedArray, + $api->all(['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']) + ); } /** @@ -909,8 +914,11 @@ public function shouldAddHook() )) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', - array('push_events' => true, 'issues_events' => true, 'merge_requests_events' => true))); + $this->assertEquals($expectedArray, $api->addHook( + 1, + 'http://www.example.com', + array('push_events' => true, 'issues_events' => true, 'merge_requests_events' => true) + )); } /** @@ -958,8 +966,10 @@ public function shouldUpdateHook() ->with('projects/1/hooks/3', array('url' => 'http://www.example-test.com', 'push_events' => false)) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->updateHook(1, 3, array('url' => 'http://www.example-test.com', 'push_events' => false))); + $this->assertEquals( + $expectedArray, + $api->updateHook(1, 3, array('url' => 'http://www.example-test.com', 'push_events' => false)) + ); } /** @@ -1213,8 +1223,10 @@ public function shouldUpdateLabel() ->with('projects/1/labels', array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff')) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->updateLabel(1, array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff'))); + $this->assertEquals( + $expectedArray, + $api->updateLabel(1, array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff')) + ); } /** @@ -1500,8 +1512,10 @@ public function shouldAddVariableWithEnvironment() ->with('projects/1/variables', $expectedArray) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging')); + $this->assertEquals( + $expectedArray, + $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging') + ); } /** @@ -1522,8 +1536,10 @@ public function shouldAddVariableWithProtectionAndEnvironment() ->with('projects/1/variables', $expectedArray) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging')); + $this->assertEquals( + $expectedArray, + $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging') + ); } /** @@ -1582,12 +1598,16 @@ public function shouldUpdateVariableWithEnvironment() $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/variables/DEPLOY_SERVER', - array('value' => 'stage.example.com', 'environment_scope' => 'staging')) + ->with( + 'projects/1/variables/DEPLOY_SERVER', + array('value' => 'stage.example.com', 'environment_scope' => 'staging') + ) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging')); + $this->assertEquals( + $expectedArray, + $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging') + ); } /** @@ -1605,12 +1625,16 @@ public function shouldUpdateVariableWithProtectedAndEnvironment() $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/variables/DEPLOY_SERVER', - array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging')) + ->with( + 'projects/1/variables/DEPLOY_SERVER', + array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging') + ) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging')); + $this->assertEquals( + $expectedArray, + $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging') + ); } /** @@ -1776,8 +1800,10 @@ public function shouldAddBadge() ->with('projects/1/badges', array('link_url' => $link_url, 'image_url' => $image_url)) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, - $api->addBadge(1, array('link_url' => $link_url, 'image_url' => $image_url))); + $this->assertEquals( + $expectedArray, + $api->addBadge(1, array('link_url' => $link_url, 'image_url' => $image_url)) + ); } /** @@ -1833,8 +1859,10 @@ public function shouldAddProtectedBranch() $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') - ->with('projects/1/protected_branches', - array('name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30)) + ->with( + 'projects/1/protected_branches', + array('name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30) + ) ->will($this->returnValue($expectedArray)); $this->assertEquals($expectedArray, $api->addProtectedBranch(1, array('name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30))); } diff --git a/test/Gitlab/Tests/Api/ScheduleTest.php b/test/Gitlab/Tests/Api/ScheduleTest.php index 9472329df..d7dfab36e 100644 --- a/test/Gitlab/Tests/Api/ScheduleTest.php +++ b/test/Gitlab/Tests/Api/ScheduleTest.php @@ -38,7 +38,8 @@ public function shouldCreateSchedule() ]) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->create(1, + $this->assertEquals($expectedArray, $api->create( + 1, [ "id" => 13, "description" => "Test schedule pipeline", diff --git a/test/Gitlab/Tests/Api/TestCase.php b/test/Gitlab/Tests/Api/TestCase.php index 32dc404e3..1b8f729de 100644 --- a/test/Gitlab/Tests/Api/TestCase.php +++ b/test/Gitlab/Tests/Api/TestCase.php @@ -1,8 +1,8 @@ getMockBuilder(HttpClient::class) + $httpClient = $this->getMockBuilder(ClientInterface::class) ->setMethods(array('sendRequest')) ->getMock(); $httpClient diff --git a/test/Gitlab/Tests/Api/UsersTest.php b/test/Gitlab/Tests/Api/UsersTest.php index ecba9580b..ab1f21f3e 100644 --- a/test/Gitlab/Tests/Api/UsersTest.php +++ b/test/Gitlab/Tests/Api/UsersTest.php @@ -123,7 +123,7 @@ public function shouldShowUsersProjects() $this->assertEquals($expectedArray, $api->usersProjects(1)); } - + /** * @test */ @@ -132,7 +132,7 @@ public function shouldShowUsersProjectsWithLimit() $expectedArray = [$this->getUsersProjectsData()[0]]; $api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['per_page' => 1]); - + $this->assertEquals($expectedArray, $api->usersProjects(1, ['per_page' => 1])); } @@ -143,11 +143,16 @@ public function shouldGetAllUsersProjectsSortedByName() { $expectedArray = $this->getUsersProjectsData(); - $api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, - ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']); + $api = $this->getUsersProjectsRequestMock( + 'users/1/projects', + $expectedArray, + ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc'] + ); - $this->assertEquals($expectedArray, - $api->usersProjects(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc'])); + $this->assertEquals( + $expectedArray, + $api->usersProjects(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']) + ); } /** diff --git a/test/Gitlab/Tests/Api/WikiTest.php b/test/Gitlab/Tests/Api/WikiTest.php index 7813d664e..566f2ca2a 100644 --- a/test/Gitlab/Tests/Api/WikiTest.php +++ b/test/Gitlab/Tests/Api/WikiTest.php @@ -27,7 +27,8 @@ public function shouldCreateWiki() ]) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->create(1, + $this->assertEquals($expectedArray, $api->create( + 1, [ "format" => "markdown", "title" => "Test Wiki", diff --git a/test/Gitlab/Tests/HttpClient/BuilderTest.php b/test/Gitlab/Tests/HttpClient/BuilderTest.php index 7fdeea5f1..dd233d7ba 100644 --- a/test/Gitlab/Tests/HttpClient/BuilderTest.php +++ b/test/Gitlab/Tests/HttpClient/BuilderTest.php @@ -5,10 +5,10 @@ use Gitlab\HttpClient\Builder; use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin; -use Http\Client\HttpClient; use Http\Message\RequestFactory; use Http\Message\StreamFactory; use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientInterface; /** * @author Fabien Bourigault @@ -23,7 +23,7 @@ class BuilderTest extends TestCase public function setUp() { $this->subject = new Builder( - $this->getMockBuilder(HttpClient::class)->getMock(), + $this->getMockBuilder(ClientInterface::class)->getMock(), $this->getMockBuilder(RequestFactory::class)->getMock(), $this->getMockBuilder(StreamFactory::class)->getMock() ); diff --git a/test/Gitlab/Tests/Model/ProjectTest.php b/test/Gitlab/Tests/Model/ProjectTest.php index 16fbec63a..6392762cb 100644 --- a/test/Gitlab/Tests/Model/ProjectTest.php +++ b/test/Gitlab/Tests/Model/ProjectTest.php @@ -112,10 +112,14 @@ public function testFromArray() $this->assertSame($data['runners_token'], $project->runners_token); $this->assertSame($data['public_jobs'], $project->public_jobs); $this->assertCount(0, $project->shared_with_groups); - $this->assertSame($data['only_allow_merge_if_pipeline_succeeds'], - $project->only_allow_merge_if_pipeline_succeeds); - $this->assertSame($data['only_allow_merge_if_all_discussions_are_resolved'], - $project->only_allow_merge_if_all_discussions_are_resolved); + $this->assertSame( + $data['only_allow_merge_if_pipeline_succeeds'], + $project->only_allow_merge_if_pipeline_succeeds + ); + $this->assertSame( + $data['only_allow_merge_if_all_discussions_are_resolved'], + $project->only_allow_merge_if_all_discussions_are_resolved + ); $this->assertSame($data['request_access_enabled'], $project->request_access_enabled); $this->assertSame($data['merge_method'], $project->merge_method); $this->assertSame($data['approvals_before_merge'], $project->approvals_before_merge); diff --git a/test/Gitlab/Tests/ResultPagerTest.php b/test/Gitlab/Tests/ResultPagerTest.php index 509245cfd..ae4f85ef9 100644 --- a/test/Gitlab/Tests/ResultPagerTest.php +++ b/test/Gitlab/Tests/ResultPagerTest.php @@ -50,12 +50,12 @@ public function testFetchAll() ->getMock() ; - $response1 = (new Response)->withHeader('Link', '; rel="next",'); - $response2 = (new Response)->withHeader('Link', '; rel="next",') + $response1 = (new Response())->withHeader('Link', '; rel="next",'); + $response2 = (new Response())->withHeader('Link', '; rel="next",') ->withHeader('Content-Type', 'application/json') ->withBody(stream_for('["project3", "project4"]')) ; - $response3 = (new Response)->withHeader('Content-Type', 'application/json') + $response3 = (new Response())->withHeader('Content-Type', 'application/json') ->withBody(stream_for('["project5", "project6"]')) ; From 367546b91fbaba108abdb0496c3df926b4180d32 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 2 Jul 2020 13:36:50 +0100 Subject: [PATCH 2/3] Switched to PSR-17 --- README.md | 35 ++++++++------------ composer.json | 19 +++++++---- lib/Gitlab/Api/AbstractApi.php | 12 +++---- lib/Gitlab/Client.php | 4 +-- lib/Gitlab/HttpClient/Builder.php | 25 +++++++------- test/Gitlab/Tests/HttpClient/BuilderTest.php | 8 ++--- 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 67d4f2e88..cb35530ff 100644 --- a/README.md +++ b/README.md @@ -12,44 +12,39 @@ Based on [php-github-api](https://github.com/m4tthumphrey/php-github-api) and co Installation ------------ -Via [Composer](https://getcomposer.org). +Via [Composer](https://getcomposer.org). You will also need to install packages that "provide" [`psr/http-client-implementation`](https://packagist.org/providers/psr/http-client-implementation) and [`psr/http-factory-implementation`](https://packagist.org/providers/psr/http-factory-implementation). -### PHP 7.2+: +### PHP 7.1+: ```bash -composer require m4tthumphrey/php-gitlab-api guzzlehttp/guzzle:^7.0.1 +composer require m4tthumphrey/php-gitlab-api:^10.0 php-http/guzzle6-adapter:^2.0.1 http-interop/http-factory-guzzle:^1.0 ``` -### PHP 7.1+: +### PHP 7.2+: ```bash -composer require m4tthumphrey/php-gitlab-api php-http/guzzle6-adapter:^2.0.1 +composer require m4tthumphrey/php-gitlab-api:^10.0 guzzlehttp/guzzle:^7.0.1 http-interop/http-factory-guzzle:^1.0 ``` -### Laravel 5.5+: +### Laravel 6+: ```bash -composer require graham-campbell/gitlab guzzlehttp/guzzle:^7.0.1 +composer require graham-campbell/gitlab:^4.0 guzzlehttp/guzzle:^7.0.1 http-interop/http-factory-guzzle:^1.0 ``` -### Symfony 3.4+: +### Symfony 4.4: ```bash -composer require zeichen32/gitlabapibundle guzzlehttp/guzzle:^7.0.1 +composer require zeichen32/gitlabapibundle:^5.0 symfony/http-client:^4.4 nyholm/psr7:^1.3 ``` -We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). You can visit [HTTPlug for library users](https://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages. [graham-campbell/gitlab](https://github.com/GrahamCampbell/Laravel-GitLab) is by [Graham Campbell](https://github.com/GrahamCampbell) and [zeichen32/gitlabapibundle](https://github.com/Zeichen32/GitLabApiBundle) is by [Jens Averkamp](https://github.com/Zeichen32). - -Versioning ----------- +### Symfony 5: -Depending on your Gitlab server version, you must choose the right version of this library. -Please refer to the following table to pick the right one. +```bash +composer require zeichen32/gitlabapibundle:^5.0 symfony/http-client:^5.0 nyholm/psr7:^1.3 +``` -|Version|Gitlab API Version|Gitlab Version| -|-------|------------------|--------------| -|9.x | V4 | >= 9.0 | -|8.x | V3 | < 9.5 | +We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). You can visit [HTTPlug for library users](https://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages. [graham-campbell/gitlab](https://github.com/GrahamCampbell/Laravel-GitLab) is by [Graham Campbell](https://github.com/GrahamCampbell) and [zeichen32/gitlabapibundle](https://github.com/Zeichen32/GitLabApiBundle) is by [Jens Averkamp](https://github.com/Zeichen32). General API Usage ----------------- @@ -85,8 +80,6 @@ $issues = $pager->fetchAll($client->api('issues'),'all',[null, ['state' => 'clos ``` - - Model Usage ----------- diff --git a/composer.json b/composer.json index 282acb869..505e22428 100644 --- a/composer.json +++ b/composer.json @@ -23,19 +23,24 @@ "require": { "php": "^7.1", "ext-xml": "*", - "php-http/client-common": "^2.1", + "php-http/client-common": "^2.2", "php-http/cache-plugin": "^1.7", - "php-http/discovery": "^1.7", + "php-http/discovery": "^1.9", "php-http/httplug": "^2.1", "php-http/multipart-stream-builder": "^1.1", + "psr/http-client-implementation": "^1.0", + "psr/http-factory-implementation": "^1.0", + "psr/http-message": "^1.0", "symfony/options-resolver": "^3.4 || ^4.0 || ^5.0" }, "require-dev": { - "guzzlehttp/psr7": "^1.2", - "php-http/guzzle6-adapter": "^2.0", - "php-http/mock-client": "^1.2", + "guzzlehttp/psr7": "^1.5.2", + "php-http/guzzle6-adapter": "^2.0.1", + "http-interop/http-factory-guzzle": "^1.0", "phpunit/phpunit": "^7.5.15", - "phpstan/phpstan": "^0.12.23" + "phpstan/phpstan": "^0.12.32", + "phpstan/extension-installer": "^1.0.4", + "phpstan/phpstan-deprecation-rules": "^0.12.4" }, "autoload": { "psr-4": { "Gitlab\\": "lib/Gitlab/" } @@ -45,7 +50,7 @@ }, "extra": { "branch-alias": { - "dev-master": "9.17-dev" + "dev-master": "10.0-dev" } } } diff --git a/lib/Gitlab/Api/AbstractApi.php b/lib/Gitlab/Api/AbstractApi.php index faae682d8..97a3b185d 100644 --- a/lib/Gitlab/Api/AbstractApi.php +++ b/lib/Gitlab/Api/AbstractApi.php @@ -4,10 +4,10 @@ use Gitlab\HttpClient\Message\QueryStringBuilder; use Gitlab\HttpClient\Message\ResponseMediator; use Gitlab\Tests\HttpClient\Message\QueryStringBuilderTest; -use Http\Discovery\StreamFactoryDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; use Http\Message\MultipartStream\MultipartStreamBuilder; -use Http\Message\StreamFactory; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -28,18 +28,18 @@ abstract class AbstractApi implements ApiInterface protected $client; /** - * @var StreamFactory + * @var StreamFactoryInterface */ private $streamFactory; /** * @param Client $client - * @param StreamFactory|null $streamFactory + * @param StreamFactoryInterface|null $streamFactory */ - public function __construct(Client $client, StreamFactory $streamFactory = null) + public function __construct(Client $client, StreamFactoryInterface $streamFactory = null) { $this->client = $client; - $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); + $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); } /** diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php index 7f19a0ba6..40086544f 100644 --- a/lib/Gitlab/Client.php +++ b/lib/Gitlab/Client.php @@ -12,7 +12,7 @@ use Http\Client\Common\Plugin\HeaderDefaultsPlugin; use Http\Client\Common\Plugin\HistoryPlugin; use Http\Client\Common\Plugin\RedirectPlugin; -use Http\Discovery\UriFactoryDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; use Psr\Http\Client\ClientInterface; /** @@ -441,7 +441,7 @@ public function authenticate($token, $authMethod = self::AUTH_URL_TOKEN, $sudo = public function setUrl($url) { $this->httpClientBuilder->removePlugin(AddHostPlugin::class); - $this->httpClientBuilder->addPlugin(new AddHostPlugin(UriFactoryDiscovery::find()->createUri($url))); + $this->httpClientBuilder->addPlugin(new AddHostPlugin(Psr17FactoryDiscovery::findUrlFactory()->createUri($url))); return $this; } diff --git a/lib/Gitlab/HttpClient/Builder.php b/lib/Gitlab/HttpClient/Builder.php index 6803ed691..b03d31856 100644 --- a/lib/Gitlab/HttpClient/Builder.php +++ b/lib/Gitlab/HttpClient/Builder.php @@ -6,12 +6,11 @@ use Http\Client\Common\Plugin; use Http\Client\Common\PluginClient; use Http\Client\Common\PluginClientFactory; -use Http\Discovery\MessageFactoryDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; use Http\Discovery\Psr18ClientDiscovery; -use Http\Discovery\StreamFactoryDiscovery; -use Http\Message\RequestFactory; -use Http\Message\StreamFactory; use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; /** * A builder that builds the API client. @@ -36,12 +35,12 @@ class Builder private $pluginClient; /** - * @var RequestFactory + * @var RequestFactoryInterface */ private $requestFactory; /** - * @var StreamFactory + * @var StreamFactoryInterface */ private $streamFactory; @@ -58,18 +57,18 @@ class Builder private $plugins = []; /** - * @param ClientInterface $httpClient - * @param RequestFactory $requestFactory - * @param StreamFactory $streamFactory + * @param ClientInterface|null $httpClient + * @param RequestFactoryInterface|null $requestFactory + * @param StreamFactoryInterface|null $streamFactory */ public function __construct( ClientInterface $httpClient = null, - RequestFactory $requestFactory = null, - StreamFactory $streamFactory = null + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null ) { $this->httpClient = $httpClient ?: Psr18ClientDiscovery::find(); - $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); - $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); + $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); } /** diff --git a/test/Gitlab/Tests/HttpClient/BuilderTest.php b/test/Gitlab/Tests/HttpClient/BuilderTest.php index dd233d7ba..0e2f13888 100644 --- a/test/Gitlab/Tests/HttpClient/BuilderTest.php +++ b/test/Gitlab/Tests/HttpClient/BuilderTest.php @@ -5,10 +5,10 @@ use Gitlab\HttpClient\Builder; use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\Plugin; -use Http\Message\RequestFactory; -use Http\Message\StreamFactory; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; /** * @author Fabien Bourigault @@ -24,8 +24,8 @@ public function setUp() { $this->subject = new Builder( $this->getMockBuilder(ClientInterface::class)->getMock(), - $this->getMockBuilder(RequestFactory::class)->getMock(), - $this->getMockBuilder(StreamFactory::class)->getMock() + $this->getMockBuilder(RequestFactoryInterface::class)->getMock(), + $this->getMockBuilder(StreamFactoryInterface::class)->getMock() ); } From 433f0d88e8ad7b30a32a36c3851cfbabcbd9fbaa Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 2 Jul 2020 14:45:59 +0100 Subject: [PATCH 3/3] Cache and methods client improvements --- composer.json | 5 +- lib/Gitlab/Api/AbstractApi.php | 2 +- lib/Gitlab/Client.php | 6 +- lib/Gitlab/HttpClient/Builder.php | 73 ++++++++++++++++++-- test/Gitlab/Tests/HttpClient/BuilderTest.php | 9 ++- test/Gitlab/Tests/ResultPagerTest.php | 10 +-- 6 files changed, 81 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 505e22428..0337366fa 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "php-http/discovery": "^1.9", "php-http/httplug": "^2.1", "php-http/multipart-stream-builder": "^1.1", + "psr/cache": "^1.0", "psr/http-client-implementation": "^1.0", "psr/http-factory-implementation": "^1.0", "psr/http-message": "^1.0", @@ -37,10 +38,10 @@ "guzzlehttp/psr7": "^1.5.2", "php-http/guzzle6-adapter": "^2.0.1", "http-interop/http-factory-guzzle": "^1.0", - "phpunit/phpunit": "^7.5.15", "phpstan/phpstan": "^0.12.32", "phpstan/extension-installer": "^1.0.4", - "phpstan/phpstan-deprecation-rules": "^0.12.4" + "phpstan/phpstan-deprecation-rules": "^0.12.4", + "phpunit/phpunit": "^7.5.15" }, "autoload": { "psr-4": { "Gitlab\\": "lib/Gitlab/" } diff --git a/lib/Gitlab/Api/AbstractApi.php b/lib/Gitlab/Api/AbstractApi.php index 97a3b185d..40a3eb6a5 100644 --- a/lib/Gitlab/Api/AbstractApi.php +++ b/lib/Gitlab/Api/AbstractApi.php @@ -39,7 +39,7 @@ abstract class AbstractApi implements ApiInterface public function __construct(Client $client, StreamFactoryInterface $streamFactory = null) { $this->client = $client; - $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); } /** diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php index 40086544f..38c5eddb2 100644 --- a/lib/Gitlab/Client.php +++ b/lib/Gitlab/Client.php @@ -7,7 +7,7 @@ use Gitlab\HttpClient\Plugin\History; use Gitlab\HttpClient\Plugin\Authentication; use Gitlab\HttpClient\Plugin\GitlabExceptionThrower; -use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\HttpMethodsClientInterface; use Http\Client\Common\Plugin\AddHostPlugin; use Http\Client\Common\Plugin\HeaderDefaultsPlugin; use Http\Client\Common\Plugin\HistoryPlugin; @@ -78,7 +78,7 @@ class Client public function __construct(Builder $httpClientBuilder = null) { $this->responseHistory = new History(); - $this->httpClientBuilder = $httpClientBuilder ?: new Builder(); + $this->httpClientBuilder = $httpClientBuilder ?? new Builder(); $this->httpClientBuilder->addPlugin(new GitlabExceptionThrower()); $this->httpClientBuilder->addPlugin(new HistoryPlugin($this->responseHistory)); @@ -456,7 +456,7 @@ public function __get($api) } /** - * @return HttpMethodsClient + * @return HttpMethodsClientInterface */ public function getHttpClient() { diff --git a/lib/Gitlab/HttpClient/Builder.php b/lib/Gitlab/HttpClient/Builder.php index b03d31856..5890c5694 100644 --- a/lib/Gitlab/HttpClient/Builder.php +++ b/lib/Gitlab/HttpClient/Builder.php @@ -3,20 +3,26 @@ namespace Gitlab\HttpClient; use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\HttpMethodsClientInterface; use Http\Client\Common\Plugin; use Http\Client\Common\PluginClient; +use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator; +use Http\Client\Common\Plugin\CachePlugin; use Http\Client\Common\PluginClientFactory; use Http\Discovery\Psr17FactoryDiscovery; use Http\Discovery\Psr18ClientDiscovery; +use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; /** * A builder that builds the API client. + * * This will allow you to fluently add and remove plugins. * * @author Tobias Nyholm + * @author Graham Campbell */ class Builder { @@ -30,16 +36,20 @@ class Builder /** * A HTTP client with all our plugins. * - * @var HttpMethodsClient + * @var HttpMethodsClientInterface */ private $pluginClient; /** + * The HTTP request factory. + * * @var RequestFactoryInterface */ private $requestFactory; /** + * The HTTP stream factory. + * * @var StreamFactoryInterface */ private $streamFactory; @@ -52,11 +62,24 @@ class Builder private $httpClientModified = true; /** + * The currently registered plugins. + * * @var Plugin[] */ private $plugins = []; /** + * The cache plugin to use. + * + * This plugin is specially treated because it has to be the very last plugin. + * + * @var CachePlugin|null + */ + private $cachePlugin; + + /** + * Create a new http client builder instance. + * * @param ClientInterface|null $httpClient * @param RequestFactoryInterface|null $requestFactory * @param StreamFactoryInterface|null $streamFactory @@ -66,21 +89,26 @@ public function __construct( RequestFactoryInterface $requestFactory = null, StreamFactoryInterface $streamFactory = null ) { - $this->httpClient = $httpClient ?: Psr18ClientDiscovery::find(); - $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); - $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); + $this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); } /** - * @return HttpMethodsClient + * @return HttpMethodsClientInterface */ public function getHttpClient() { if ($this->httpClientModified) { $this->httpClientModified = false; + $plugins = $this->plugins; + if ($this->cachePlugin !== null) { + $plugins[] = $this->cachePlugin; + } + $this->pluginClient = new HttpMethodsClient( - (new PluginClientFactory())->createClient($this->httpClient, $this->plugins), + (new PluginClientFactory())->createClient($this->httpClient, $plugins), $this->requestFactory ); } @@ -92,6 +120,8 @@ public function getHttpClient() * Add a new plugin to the end of the plugin chain. * * @param Plugin $plugin + * + * @return void */ public function addPlugin(Plugin $plugin) { @@ -103,6 +133,8 @@ public function addPlugin(Plugin $plugin) * Remove a plugin by its fully qualified class name (FQCN). * * @param string $fqcn + * + * @return void */ public function removePlugin($fqcn) { @@ -113,4 +145,33 @@ public function removePlugin($fqcn) } } } + + /** + * Add a cache plugin to cache responses locally. + * + * @param CacheItemPoolInterface $cachePool + * @param array $config + * + * @return void + */ + public function addCache(CacheItemPoolInterface $cachePool, array $config = []) + { + if (!isset($config['cache_key_generator'])) { + $config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']); + } + + $this->cachePlugin = CachePlugin::clientCache($cachePool, $this->streamFactory, $config); + $this->httpClientModified = true; + } + + /** + * Remove the cache plugin. + * + * @return void + */ + public function removeCache() + { + $this->cachePlugin = null; + $this->httpClientModified = true; + } } diff --git a/test/Gitlab/Tests/HttpClient/BuilderTest.php b/test/Gitlab/Tests/HttpClient/BuilderTest.php index 0e2f13888..c851150ca 100644 --- a/test/Gitlab/Tests/HttpClient/BuilderTest.php +++ b/test/Gitlab/Tests/HttpClient/BuilderTest.php @@ -3,7 +3,7 @@ namespace Gitlab\Tests\HttpClient; use Gitlab\HttpClient\Builder; -use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\HttpMethodsClientInterface; use Http\Client\Common\Plugin; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientInterface; @@ -20,7 +20,10 @@ class BuilderTest extends TestCase */ private $subject; - public function setUp() + /** + * @before + */ + public function initBuilder() { $this->subject = new Builder( $this->getMockBuilder(ClientInterface::class)->getMock(), @@ -51,6 +54,6 @@ public function testRemovePluginShouldInvalidateHttpClient() public function testHttpClientShouldBeAnHttpMethodsClient() { - $this->assertInstanceOf(HttpMethodsClient::class, $this->subject->getHttpClient()); + $this->assertInstanceOf(HttpMethodsClientInterface::class, $this->subject->getHttpClient()); } } diff --git a/test/Gitlab/Tests/ResultPagerTest.php b/test/Gitlab/Tests/ResultPagerTest.php index ae4f85ef9..8a04ff06d 100644 --- a/test/Gitlab/Tests/ResultPagerTest.php +++ b/test/Gitlab/Tests/ResultPagerTest.php @@ -8,7 +8,6 @@ use Gitlab\ResultPager; use GuzzleHttp\Psr7\Response; use function GuzzleHttp\Psr7\stream_for; -use Http\Client\Common\HttpMethodsClient; use Http\Client\Common\HttpMethodsClientInterface; use PHPUnit\Framework\TestCase; @@ -72,14 +71,7 @@ public function testFetchAll() )) ; - if (interface_exists(HttpMethodsClientInterface::class)) { - $httpClient = $this->createMock(HttpMethodsClientInterface::class); - } else { - $httpClient = $this->getMockBuilder(HttpMethodsClient::class) - ->disableOriginalConstructor() - ->getMock() - ; - } + $httpClient = $this->createMock(HttpMethodsClientInterface::class); $httpClient->expects($this->exactly(2)) ->method('get')