From 81aa33e822c949e2a58d2aee8bb510f64ac643e6 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Aug 2014 10:12:35 +0100 Subject: [PATCH 1/5] Closes #177 - use the magic __call method for IDE completion --- lib/Github/Client.php | 110 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 5c102ba11f5..686791cc079 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -10,6 +10,34 @@ /** * Simple yet very cool PHP GitHub client * + * @method Api\CurrentUser currentUser() + * @method Api\CurrentUser me() + * @method Api\Enterprise ent() + * @method Api\Enterprise enterprise() + * @method Api\GitData git() + * @method Api\GitData gitData() + * @method Api\Gists gist() + * @method Api\Gists gists() + * @method Api\Issue issue() + * @method Api\Issue issues() + * @method Api\Markdown markdown() + * @method Api\Organization organization() + * @method Api\Organization organizations() + * @method Api\PullRequest pr() + * @method Api\PullRequest pullRequest() + * @method Api\PullRequest pullRequests() + * @method Api\Repo repo() + * @method Api\Repo repos() + * @method Api\Repo repository() + * @method Api\Repo repositories() + * @method Api\Organization team() + * @method Api\Organization teams() + * @method Api\User user() + * @method Api\User users() + * @method Api\Authorizations authorization() + * @method Api\Authorizations authorizations() + * @method Api\Meta meta() + * * @author Joseph Bielawski * * Website: http://github.com/KnpLabs/php-github-api @@ -274,4 +302,86 @@ public function getSupportedApiVersions() { return array('v3', 'beta'); } + + /** + * @param string $name + * + * @return ApiInterface + * + * @throws InvalidArgumentException + */ + public function __call($name) { + switch ($name) { + case 'me': + case 'currentUser': + $api = new Api\CurrentUser($this); + break; + + case 'ent': + case 'enterprise': + $api = new Api\Enterprise($this); + break; + + case 'git': + case 'gitData': + $api = new Api\GitData($this); + break; + + case 'gist': + case 'gists': + $api = new Api\Gists($this); + break; + + case 'issue': + case 'issues': + $api = new Api\Issue($this); + break; + + case 'markdown': + $api = new Api\Markdown($this); + break; + + case 'organization': + case 'organizations': + $api = new Api\Organization($this); + break; + + case 'pr': + case 'pullRequest': + case 'pullRequests': + $api = new Api\PullRequest($this); + break; + + case 'repo': + case 'repos': + case 'repository': + case 'repositories': + $api = new Api\Repo($this); + break; + + case 'team': + case 'teams': + $api = new Api\Organization\Teams($this); + break; + + case 'user': + case 'users': + $api = new Api\User($this); + break; + + case 'authorization': + case 'authorizations': + $api = new Api\Authorizations($this); + break; + + case 'meta': + $api = new Api\Meta($this); + break; + + default: + throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name)); + } + + return $api; + } } From 34b5186a76cc22fb88532a1673e34066776e01aa Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Aug 2014 10:14:05 +0100 Subject: [PATCH 2/5] Forgot about the missing args param --- lib/Github/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 686791cc079..e3e9939ebc6 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -310,7 +310,7 @@ public function getSupportedApiVersions() * * @throws InvalidArgumentException */ - public function __call($name) { + public function __call($name, $args) { switch ($name) { case 'me': case 'currentUser': From af024a609c9e878b66e833a73ea132fa22b928d9 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Aug 2014 10:15:48 +0100 Subject: [PATCH 3/5] Make use of the api method and move the camelCase aliases into it --- lib/Github/Client.php | 77 +++---------------------------------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index e3e9939ebc6..bac289f0d29 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -112,6 +112,7 @@ public function api($name) switch ($name) { case 'me': case 'current_user': + case 'currentUser': $api = new Api\CurrentUser($this); break; @@ -122,6 +123,7 @@ public function api($name) case 'git': case 'git_data': + case 'gitData': $api = new Api\GitData($this); break; @@ -145,7 +147,9 @@ public function api($name) break; case 'pr': + case 'pullRequest': case 'pull_request': + case 'pullRequests': case 'pull_requests': $api = new Api\PullRequest($this); break; @@ -311,77 +315,6 @@ public function getSupportedApiVersions() * @throws InvalidArgumentException */ public function __call($name, $args) { - switch ($name) { - case 'me': - case 'currentUser': - $api = new Api\CurrentUser($this); - break; - - case 'ent': - case 'enterprise': - $api = new Api\Enterprise($this); - break; - - case 'git': - case 'gitData': - $api = new Api\GitData($this); - break; - - case 'gist': - case 'gists': - $api = new Api\Gists($this); - break; - - case 'issue': - case 'issues': - $api = new Api\Issue($this); - break; - - case 'markdown': - $api = new Api\Markdown($this); - break; - - case 'organization': - case 'organizations': - $api = new Api\Organization($this); - break; - - case 'pr': - case 'pullRequest': - case 'pullRequests': - $api = new Api\PullRequest($this); - break; - - case 'repo': - case 'repos': - case 'repository': - case 'repositories': - $api = new Api\Repo($this); - break; - - case 'team': - case 'teams': - $api = new Api\Organization\Teams($this); - break; - - case 'user': - case 'users': - $api = new Api\User($this); - break; - - case 'authorization': - case 'authorizations': - $api = new Api\Authorizations($this); - break; - - case 'meta': - $api = new Api\Meta($this); - break; - - default: - throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name)); - } - - return $api; + return $this->api($name); } } From 3e0839f0036d2663a1bb256aea44a884f700cd7a Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Aug 2014 10:32:25 +0100 Subject: [PATCH 4/5] Tests and catching of InvalidArgumentException within __call --- lib/Github/Client.php | 6 +++++- test/Github/Tests/ClientTest.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Github/Client.php b/lib/Github/Client.php index bac289f0d29..a46bb9fe749 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -315,6 +315,10 @@ public function getSupportedApiVersions() * @throws InvalidArgumentException */ public function __call($name, $args) { - return $this->api($name); + try { + return $this->api($name); + } catch (InvalidArgumentException $e) { + throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name)); + } } } diff --git a/test/Github/Tests/ClientTest.php b/test/Github/Tests/ClientTest.php index 8ebe27f785f..c9e2f447e41 100644 --- a/test/Github/Tests/ClientTest.php +++ b/test/Github/Tests/ClientTest.php @@ -124,6 +124,17 @@ public function shouldGetApiInstance($apiName, $class) $this->assertInstanceOf($class, $client->api($apiName)); } + /** + * @test + * @dataProvider getApiClassesProvider + */ + public function shouldGetMagicApiInstance($apiName, $class) + { + $client = new Client(); + + $this->assertInstanceOf($class, $client->$apiName()); + } + /** * @test * @expectedException InvalidArgumentException @@ -142,9 +153,11 @@ public function getApiClassesProvider() array('me', 'Github\Api\CurrentUser'), array('current_user', 'Github\Api\CurrentUser'), + array('currentUser', 'Github\Api\CurrentUser'), array('git', 'Github\Api\GitData'), array('git_data', 'Github\Api\GitData'), + array('gitData', 'Github\Api\GitData'), array('gist', 'Github\Api\Gists'), array('gists', 'Github\Api\Gists'), @@ -163,7 +176,9 @@ public function getApiClassesProvider() array('repositories', 'Github\Api\Repo'), array('pr', 'Github\Api\PullRequest'), + array('pullRequest', 'Github\Api\PullRequest'), array('pull_request', 'Github\Api\PullRequest'), + array('pullRequests', 'Github\Api\PullRequest'), array('pull_requests', 'Github\Api\PullRequest'), array('authorization', 'Github\Api\Authorizations'), From b84ee93f2c03e6282a7b6a8e39ef21bfa61535ce Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 21 Aug 2014 10:36:28 +0100 Subject: [PATCH 5/5] New BadMethodCallException exception. Tests for BadMethodCallException too. --- lib/Github/Client.php | 1 + lib/Github/Exception/BadMethodCallException.php | 13 +++++++++++++ test/Github/Tests/ClientTest.php | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/Github/Exception/BadMethodCallException.php diff --git a/lib/Github/Client.php b/lib/Github/Client.php index a46bb9fe749..6909264ed66 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -4,6 +4,7 @@ use Github\Api\ApiInterface; use Github\Exception\InvalidArgumentException; +use Github\Exception\BadMethodCallException; use Github\HttpClient\HttpClient; use Github\HttpClient\HttpClientInterface; diff --git a/lib/Github/Exception/BadMethodCallException.php b/lib/Github/Exception/BadMethodCallException.php new file mode 100644 index 00000000000..5c43d20a485 --- /dev/null +++ b/lib/Github/Exception/BadMethodCallException.php @@ -0,0 +1,13 @@ + + */ +class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface +{ + +} diff --git a/test/Github/Tests/ClientTest.php b/test/Github/Tests/ClientTest.php index c9e2f447e41..04b3a710397 100644 --- a/test/Github/Tests/ClientTest.php +++ b/test/Github/Tests/ClientTest.php @@ -4,6 +4,7 @@ use Github\Client; use Github\Exception\InvalidArgumentException; +use Github\Exception\BadMethodCallException; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -145,6 +146,16 @@ public function shouldNotGetApiInstance() $client->api('do_not_exist'); } + /** + * @test + * @expectedException BadMethodCallException + */ + public function shouldNotGetMagicApiInstance() + { + $client = new Client(); + $client->doNotExist(); + } + public function getApiClassesProvider() { return array(