Failed Conditions
Push — issue#772 ( a6e519...751086 )
by Guilherme
08:19
created

ScopeManager::findScopeByScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Manager;
12
13
use Doctrine\ORM\EntityManager;
14
use LoginCidadao\OpenIDBundle\Entity\Scope;
0 ignored issues
show
Bug introduced by
The type LoginCidadao\OpenIDBundle\Entity\Scope was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OAuth2\ServerBundle\Manager\ScopeManagerInterface;
16
use OAuth2\ServerBundle\Manager\ScopeManager as BaseManager;
17
18
class ScopeManager extends BaseManager implements ScopeManagerInterface
19
{
20
    private $em;
21
22
    /** @var \OAuth2\ServerBundle\Entity\Scope[] */
23
    private $scopes = [];
24
25 4
    public function __construct(EntityManager $entityManager)
26
    {
27 4
        parent::__construct($entityManager);
28 4
        $this->em = $entityManager;
29 4
    }
30
31 4
    public function setScopes($scopes)
32
    {
33 4
        if (!is_array($scopes)) {
34 4
            $scopes = explode(' ', $scopes);
35
        }
36
37 4
        $this->scopes = array();
38 4
        foreach ($scopes as $scope) {
39 4
            $scopeObj = new \OAuth2\ServerBundle\Entity\Scope();
40 4
            $scopeObj->setScope($scope);
41 4
            $scopeObj->setDescription($scope);
42
43 4
            $this->scopes[$scope] = $scopeObj;
44
        }
45 4
    }
46
47
    /**
48
     * Find a single scope by the scope
49
     *
50
     * @param $scope
51
     * @return \OAuth2\ServerBundle\Entity\Scope
52
     */
53 3
    public function findScopeByScope($scope)
54
    {
55 3
        if (preg_match('/^tag:/', $scope) === 1) {
56 1
            $scopeObj = new \OAuth2\ServerBundle\Entity\Scope();
57 1
            $scopeObj->setScope($scope);
58 1
            $scopeObj->setDescription($scope);
59
60 1
            return $scopeObj;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $scopeObj returns the type OAuth2\ServerBundle\Entity\Scope which is incompatible with the return type mandated by OAuth2\ServerBundle\Mana...ace::findScopeByScope() of OAuth2\ServerBundle\Manager\Scope.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
61
        }
62
63 3
        return $this->scopes[$scope];
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->scopes[$scope] returns the type OAuth2\ServerBundle\Entity\Scope which is incompatible with the return type mandated by OAuth2\ServerBundle\Mana...ace::findScopeByScope() of OAuth2\ServerBundle\Manager\Scope.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
64
    }
65
66
    /**
67
     * Find all the scopes by an array of scopes
68
     *
69
     * @param array $scopes
70
     * @return \OAuth2\ServerBundle\Entity\Scope[]
71
     */
72 1
    public function findScopesByScopes(array $scopes)
73
    {
74 1
        $result = array();
75 1
        foreach ($this->scopes as $scope => $obj) {
76 1
            if (array_search($scope, $scopes) !== false) {
77 1
                $result[$scope] = $obj;
78
            }
79
        }
80
81 1
        return $result;
82
    }
83
}
84