Skip to content

Closes #177 - use the magic __call method for IDE completion #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,41 @@

use Github\Api\ApiInterface;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;
use Github\HttpClient\HttpClient;
use Github\HttpClient\HttpClientInterface;

/**
* 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 <stloyd@gmail.com>
*
* Website: http://github.com/KnpLabs/php-github-api
Expand Down Expand Up @@ -84,6 +113,7 @@ public function api($name)
switch ($name) {
case 'me':
case 'current_user':
case 'currentUser':
$api = new Api\CurrentUser($this);
break;

Expand All @@ -94,6 +124,7 @@ public function api($name)

case 'git':
case 'git_data':
case 'gitData':
$api = new Api\GitData($this);
break;

Expand All @@ -117,7 +148,9 @@ public function api($name)
break;

case 'pr':
case 'pullRequest':
case 'pull_request':
case 'pullRequests':
case 'pull_requests':
$api = new Api\PullRequest($this);
break;
Expand Down Expand Up @@ -274,4 +307,19 @@ public function getSupportedApiVersions()
{
return array('v3', 'beta');
}

/**
* @param string $name
*
* @return ApiInterface
*
* @throws InvalidArgumentException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should throw a BadMethodCallException instead for __call when calling the wrong method (you should catch the InvalidArgumentException and throw another exception)

*/
public function __call($name, $args) {
try {
return $this->api($name);
} catch (InvalidArgumentException $e) {
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name));
}
}
}
13 changes: 13 additions & 0 deletions lib/Github/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Github\Exception;

/**
* BadMethodCallException
*
* @author James Brooks <jbrooksuk@me.com>
*/
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{

}
26 changes: 26 additions & 0 deletions test/Github/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Github\Client;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;

class ClientTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -124,6 +125,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
Expand All @@ -134,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(
Expand All @@ -142,9 +164,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'),
Expand All @@ -163,7 +187,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'),
Expand Down