BurningFlipside /
CommonCode
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Flipside\Auth\OAuth2; |
||
| 3 | |||
| 4 | class GitLabAuthenticator extends OAuth2Authenticator |
||
| 5 | { |
||
| 6 | protected $app_id; |
||
| 7 | protected $app_secret; |
||
| 8 | |||
| 9 | public function __construct($params) |
||
| 10 | { |
||
| 11 | parent::__construct($params); |
||
| 12 | $this->app_id = $params['app_id']; |
||
| 13 | $this->app_secret = $params['app_secret']; |
||
| 14 | } |
||
| 15 | |||
| 16 | public function getHostName() |
||
| 17 | { |
||
| 18 | return 'gitlab.com'; |
||
|
0 ignored issues
–
show
|
|||
| 19 | } |
||
| 20 | |||
| 21 | public function getAuthorizationUrl() |
||
| 22 | { |
||
| 23 | return 'https://gitlab.com/oauth/authorize?client_id='.$this->app_id.'&redirect_uri='.urlencode($this->redirect_uri).'&response_type=code'; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function getAccessTokenUrl() |
||
| 27 | { |
||
| 28 | return 'https://gitlab.com/oauth/token?client_id='.$this->app_id.'&client_secret='.$this->app_secret.'&grant_type=authorization_code&redirect_uri='.urlencode($this->redirect_uri); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getUserFromToken($token) |
||
| 32 | { |
||
| 33 | if($token === false) |
||
| 34 | { |
||
| 35 | $token = \FlipSession::getVar('OAuthToken'); |
||
| 36 | } |
||
| 37 | $resp = \Httpful\Request::get('https://gitlab.com/api/v3/user')->addHeader('Authorization', 'Bearer '.$token->access_token)->send(); |
||
| 38 | $gitlab_user = $resp->body; |
||
| 39 | $user = new \Auth\PendingUser(); |
||
| 40 | $user->mail = $gitlab_user->email; |
||
| 41 | $name = explode(' ', $gitlab_user->name); |
||
| 42 | $user->givenName = $name[0]; |
||
| 43 | $user->sn = $name[1]; |
||
| 44 | $user->addLoginProvider($this->getHostName()); |
||
| 45 | return $user; |
||
|
0 ignored issues
–
show
The return type of
return $user; (Auth\PendingUser) is incompatible with the return type declared by the abstract method Flipside\Auth\OAuth2\OAu...cator::getUserFromToken of type Auth\User.
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function Loading history...
|
|||
| 46 | } |
||
| 47 | } |
||
| 48 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.