Skip to content

Commit 414457e

Browse files
committed
Merge pull request KnpLabs#180 from jbrooksuk/magic-api
Closes KnpLabs#177 - use the magic __call method for IDE completion
2 parents 4c5fe77 + b84ee93 commit 414457e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

lib/Github/Client.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,41 @@
44

55
use Github\Api\ApiInterface;
66
use Github\Exception\InvalidArgumentException;
7+
use Github\Exception\BadMethodCallException;
78
use Github\HttpClient\HttpClient;
89
use Github\HttpClient\HttpClientInterface;
910

1011
/**
1112
* Simple yet very cool PHP GitHub client
1213
*
14+
* @method Api\CurrentUser currentUser()
15+
* @method Api\CurrentUser me()
16+
* @method Api\Enterprise ent()
17+
* @method Api\Enterprise enterprise()
18+
* @method Api\GitData git()
19+
* @method Api\GitData gitData()
20+
* @method Api\Gists gist()
21+
* @method Api\Gists gists()
22+
* @method Api\Issue issue()
23+
* @method Api\Issue issues()
24+
* @method Api\Markdown markdown()
25+
* @method Api\Organization organization()
26+
* @method Api\Organization organizations()
27+
* @method Api\PullRequest pr()
28+
* @method Api\PullRequest pullRequest()
29+
* @method Api\PullRequest pullRequests()
30+
* @method Api\Repo repo()
31+
* @method Api\Repo repos()
32+
* @method Api\Repo repository()
33+
* @method Api\Repo repositories()
34+
* @method Api\Organization team()
35+
* @method Api\Organization teams()
36+
* @method Api\User user()
37+
* @method Api\User users()
38+
* @method Api\Authorizations authorization()
39+
* @method Api\Authorizations authorizations()
40+
* @method Api\Meta meta()
41+
*
1342
* @author Joseph Bielawski <stloyd@gmail.com>
1443
*
1544
* Website: http://github.com/KnpLabs/php-github-api
@@ -84,6 +113,7 @@ public function api($name)
84113
switch ($name) {
85114
case 'me':
86115
case 'current_user':
116+
case 'currentUser':
87117
$api = new Api\CurrentUser($this);
88118
break;
89119

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

95125
case 'git':
96126
case 'git_data':
127+
case 'gitData':
97128
$api = new Api\GitData($this);
98129
break;
99130

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

119150
case 'pr':
151+
case 'pullRequest':
120152
case 'pull_request':
153+
case 'pullRequests':
121154
case 'pull_requests':
122155
$api = new Api\PullRequest($this);
123156
break;
@@ -274,4 +307,19 @@ public function getSupportedApiVersions()
274307
{
275308
return array('v3', 'beta');
276309
}
310+
311+
/**
312+
* @param string $name
313+
*
314+
* @return ApiInterface
315+
*
316+
* @throws InvalidArgumentException
317+
*/
318+
public function __call($name, $args) {
319+
try {
320+
return $this->api($name);
321+
} catch (InvalidArgumentException $e) {
322+
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name));
323+
}
324+
}
277325
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Github\Exception;
4+
5+
/**
6+
* BadMethodCallException
7+
*
8+
* @author James Brooks <jbrooksuk@me.com>
9+
*/
10+
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
11+
{
12+
13+
}

test/Github/Tests/ClientTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Github\Client;
66
use Github\Exception\InvalidArgumentException;
7+
use Github\Exception\BadMethodCallException;
78

89
class ClientTest extends \PHPUnit_Framework_TestCase
910
{
@@ -124,6 +125,17 @@ public function shouldGetApiInstance($apiName, $class)
124125
$this->assertInstanceOf($class, $client->api($apiName));
125126
}
126127

128+
/**
129+
* @test
130+
* @dataProvider getApiClassesProvider
131+
*/
132+
public function shouldGetMagicApiInstance($apiName, $class)
133+
{
134+
$client = new Client();
135+
136+
$this->assertInstanceOf($class, $client->$apiName());
137+
}
138+
127139
/**
128140
* @test
129141
* @expectedException InvalidArgumentException
@@ -134,6 +146,16 @@ public function shouldNotGetApiInstance()
134146
$client->api('do_not_exist');
135147
}
136148

149+
/**
150+
* @test
151+
* @expectedException BadMethodCallException
152+
*/
153+
public function shouldNotGetMagicApiInstance()
154+
{
155+
$client = new Client();
156+
$client->doNotExist();
157+
}
158+
137159
public function getApiClassesProvider()
138160
{
139161
return array(
@@ -142,9 +164,11 @@ public function getApiClassesProvider()
142164

143165
array('me', 'Github\Api\CurrentUser'),
144166
array('current_user', 'Github\Api\CurrentUser'),
167+
array('currentUser', 'Github\Api\CurrentUser'),
145168

146169
array('git', 'Github\Api\GitData'),
147170
array('git_data', 'Github\Api\GitData'),
171+
array('gitData', 'Github\Api\GitData'),
148172

149173
array('gist', 'Github\Api\Gists'),
150174
array('gists', 'Github\Api\Gists'),
@@ -163,7 +187,9 @@ public function getApiClassesProvider()
163187
array('repositories', 'Github\Api\Repo'),
164188

165189
array('pr', 'Github\Api\PullRequest'),
190+
array('pullRequest', 'Github\Api\PullRequest'),
166191
array('pull_request', 'Github\Api\PullRequest'),
192+
array('pullRequests', 'Github\Api\PullRequest'),
167193
array('pull_requests', 'Github\Api\PullRequest'),
168194

169195
array('authorization', 'Github\Api\Authorizations'),

0 commit comments

Comments
 (0)