Passed
Pull Request — master (#370)
by Valentin
04:51
created
src/Security/tests/PermissionManagerTest.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -90,8 +90,8 @@
 block discarded – undo
90 90
         $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION));
91 91
 
92 92
         // test pattern permission
93
-        $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class));
94
-        $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION));
93
+        $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION.'.*', AllowRule::class));
94
+        $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION.'.'.static::PERMISSION));
95 95
 
96 96
         $this->assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION));
97 97
         $this->assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION));
Please login to merge, or discard this patch.
src/Security/tests/Traits/GuardedTraitTest.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -100,7 +100,7 @@
 block discarded – undo
100 100
         $this->assertEquals(static::OPERATION, $guarded->resolvePermission(static::OPERATION));
101 101
 
102 102
         $guarded = new GuardedWithNamespace();
103
-        $resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE . '.' . static::OPERATION;
103
+        $resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE.'.'.static::OPERATION;
104 104
         $this->assertEquals($resolvedPermission, $guarded->resolvePermission(static::OPERATION));
105 105
     }
106 106
 }
Please login to merge, or discard this patch.
src/Security/tests/RuleManagerTest.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@
 block discarded – undo
60 60
         // other rule types
61 61
         $manager->set('RuleInterface', $this->rule);
62 62
         $this->assertEquals($this->rule, $manager->get('RuleInterface'));
63
-        $manager->set('Closure', function () {
63
+        $manager->set('Closure', function (){
64 64
             return true;
65 65
         });
66 66
         $this->assertTrue($manager->get('Closure') instanceof CallableRule);
Please login to merge, or discard this patch.
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -60,7 +60,8 @@
 block discarded – undo
60 60
         // other rule types
61 61
         $manager->set('RuleInterface', $this->rule);
62 62
         $this->assertEquals($this->rule, $manager->get('RuleInterface'));
63
-        $manager->set('Closure', function () {
63
+        $manager->set('Closure', function ()
64
+        {
64 65
             return true;
65 66
         });
66 67
         $this->assertTrue($manager->get('Closure') instanceof CallableRule);
Please login to merge, or discard this patch.
src/Security/src/PermissionManager.php 2 patches
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
      */
73 73
     public function addRole(string $role): PermissionManager
74 74
     {
75
-        if ($this->hasRole($role)) {
75
+        if ($this->hasRole($role)){
76 76
             throw new RoleException("Role '{$role}' already exists");
77 77
         }
78 78
 
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
      */
91 91
     public function removeRole(string $role): PermissionManager
92 92
     {
93
-        if (!$this->hasRole($role)) {
93
+        if (!$this->hasRole($role)){
94 94
             throw new RoleException("Undefined role '{$role}'");
95 95
         }
96 96
 
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
      */
113 113
     public function getPermissions(string $role): array
114 114
     {
115
-        if (!$this->hasRole($role)) {
115
+        if (!$this->hasRole($role)){
116 116
             throw new RoleException("Undefined role '{$role}'");
117 117
         }
118 118
 
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
      */
125 125
     public function getRule(string $role, string $permission): RuleInterface
126 126
     {
127
-        if (!$this->hasRole($role)) {
127
+        if (!$this->hasRole($role)){
128 128
             throw new RoleException("Undefined role '{$role}'");
129 129
         }
130 130
 
@@ -139,11 +139,11 @@  discard block
 block discarded – undo
139 139
      */
140 140
     public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager
141 141
     {
142
-        if (!$this->hasRole($role)) {
142
+        if (!$this->hasRole($role)){
143 143
             throw new RoleException("Undefined role '{$role}'");
144 144
         }
145 145
 
146
-        if (!$this->rules->has($rule)) {
146
+        if (!$this->rules->has($rule)){
147 147
             throw new PermissionException("Undefined rule '{$rule}'");
148 148
         }
149 149
 
@@ -176,14 +176,14 @@  discard block
 block discarded – undo
176 176
      */
177 177
     private function findRule(string $role, string $permission): string
178 178
     {
179
-        if (isset($this->permissions[$role][$permission])) {
179
+        if (isset($this->permissions[$role][$permission])){
180 180
             //O(1) check
181 181
             return $this->permissions[$role][$permission];
182 182
         }
183 183
 
184 184
         //Matching using star syntax
185
-        foreach ($this->permissions[$role] as $pattern => $rule) {
186
-            if ($this->matcher->matches($permission, $pattern)) {
185
+        foreach ($this->permissions[$role] as $pattern => $rule){
186
+            if ($this->matcher->matches($permission, $pattern)){
187 187
                 return $rule;
188 188
             }
189 189
         }
Please login to merge, or discard this patch.
Braces   +18 added lines, -9 removed lines patch added patch discarded remove patch
@@ -72,7 +72,8 @@  discard block
 block discarded – undo
72 72
      */
73 73
     public function addRole(string $role): PermissionManager
74 74
     {
75
-        if ($this->hasRole($role)) {
75
+        if ($this->hasRole($role))
76
+        {
76 77
             throw new RoleException("Role '{$role}' already exists");
77 78
         }
78 79
 
@@ -90,7 +91,8 @@  discard block
 block discarded – undo
90 91
      */
91 92
     public function removeRole(string $role): PermissionManager
92 93
     {
93
-        if (!$this->hasRole($role)) {
94
+        if (!$this->hasRole($role))
95
+        {
94 96
             throw new RoleException("Undefined role '{$role}'");
95 97
         }
96 98
 
@@ -112,7 +114,8 @@  discard block
 block discarded – undo
112 114
      */
113 115
     public function getPermissions(string $role): array
114 116
     {
115
-        if (!$this->hasRole($role)) {
117
+        if (!$this->hasRole($role))
118
+        {
116 119
             throw new RoleException("Undefined role '{$role}'");
117 120
         }
118 121
 
@@ -124,7 +127,8 @@  discard block
 block discarded – undo
124 127
      */
125 128
     public function getRule(string $role, string $permission): RuleInterface
126 129
     {
127
-        if (!$this->hasRole($role)) {
130
+        if (!$this->hasRole($role))
131
+        {
128 132
             throw new RoleException("Undefined role '{$role}'");
129 133
         }
130 134
 
@@ -139,11 +143,13 @@  discard block
 block discarded – undo
139 143
      */
140 144
     public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager
141 145
     {
142
-        if (!$this->hasRole($role)) {
146
+        if (!$this->hasRole($role))
147
+        {
143 148
             throw new RoleException("Undefined role '{$role}'");
144 149
         }
145 150
 
146
-        if (!$this->rules->has($rule)) {
151
+        if (!$this->rules->has($rule))
152
+        {
147 153
             throw new PermissionException("Undefined rule '{$rule}'");
148 154
         }
149 155
 
@@ -176,14 +182,17 @@  discard block
 block discarded – undo
176 182
      */
177 183
     private function findRule(string $role, string $permission): string
178 184
     {
179
-        if (isset($this->permissions[$role][$permission])) {
185
+        if (isset($this->permissions[$role][$permission]))
186
+        {
180 187
             //O(1) check
181 188
             return $this->permissions[$role][$permission];
182 189
         }
183 190
 
184 191
         //Matching using star syntax
185
-        foreach ($this->permissions[$role] as $pattern => $rule) {
186
-            if ($this->matcher->matches($permission, $pattern)) {
192
+        foreach ($this->permissions[$role] as $pattern => $rule)
193
+        {
194
+            if ($this->matcher->matches($permission, $pattern))
195
+            {
187 196
                 return $rule;
188 197
             }
189 198
         }
Please login to merge, or discard this patch.
src/Security/src/Guard.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
         PermissionsInterface $permissions,
37 37
         ActorInterface $actor = null,
38 38
         array $roles = []
39
-    ) {
39
+    ){
40 40
         $this->roles = $roles;
41 41
         $this->actor = $actor;
42 42
         $this->permissions = $permissions;
@@ -48,8 +48,8 @@  discard block
 block discarded – undo
48 48
     public function allows(string $permission, array $context = []): bool
49 49
     {
50 50
         $allows = false;
51
-        foreach ($this->getRoles() as $role) {
52
-            if (!$this->permissions->hasRole($role)) {
51
+        foreach ($this->getRoles() as $role){
52
+            if (!$this->permissions->hasRole($role)){
53 53
                 continue;
54 54
             }
55 55
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
      */
96 96
     public function getActor(): ActorInterface
97 97
     {
98
-        if (empty($this->actor)) {
98
+        if (empty($this->actor)){
99 99
             throw new GuardException('Unable to get Guard Actor, no value set');
100 100
         }
101 101
 
Please login to merge, or discard this patch.
Braces   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -48,8 +48,10 @@  discard block
 block discarded – undo
48 48
     public function allows(string $permission, array $context = []): bool
49 49
     {
50 50
         $allows = false;
51
-        foreach ($this->getRoles() as $role) {
52
-            if (!$this->permissions->hasRole($role)) {
51
+        foreach ($this->getRoles() as $role)
52
+        {
53
+            if (!$this->permissions->hasRole($role))
54
+            {
53 55
                 continue;
54 56
             }
55 57
 
@@ -95,7 +97,8 @@  discard block
 block discarded – undo
95 97
      */
96 98
     public function getActor(): ActorInterface
97 99
     {
98
-        if (empty($this->actor)) {
100
+        if (empty($this->actor))
101
+        {
99 102
             throw new GuardException('Unable to get Guard Actor, no value set');
100 103
         }
101 104
 
Please login to merge, or discard this patch.
src/Security/src/Matcher.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -40,10 +40,10 @@
 block discarded – undo
40 40
      */
41 41
     public function matches(string $string, string $pattern): bool
42 42
     {
43
-        if ($string === $pattern) {
43
+        if ($string === $pattern){
44 44
             return true;
45 45
         }
46
-        if (!$this->isPattern($pattern)) {
46
+        if (!$this->isPattern($pattern)){
47 47
             return false;
48 48
         }
49 49
         return (bool)preg_match($this->getRegex($pattern), $string);
Please login to merge, or discard this patch.
Braces   +4 added lines, -2 removed lines patch added patch discarded remove patch
@@ -40,10 +40,12 @@
 block discarded – undo
40 40
      */
41 41
     public function matches(string $string, string $pattern): bool
42 42
     {
43
-        if ($string === $pattern) {
43
+        if ($string === $pattern)
44
+        {
44 45
             return true;
45 46
         }
46
-        if (!$this->isPattern($pattern)) {
47
+        if (!$this->isPattern($pattern))
48
+        {
47 49
             return false;
48 50
         }
49 51
         return (bool)preg_match($this->getRegex($pattern), $string);
Please login to merge, or discard this patch.
src/Security/src/Rule.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -63,20 +63,20 @@
 block discarded – undo
63 63
         $parameters = compact('actor', 'permission', 'context') + $context;
64 64
 
65 65
         //Mounting aliases
66
-        foreach (static::ALIASES as $target => $alias) {
66
+        foreach (static::ALIASES as $target => $alias){
67 67
             $parameters[$target] = $parameters[$alias];
68 68
         }
69 69
 
70
-        try {
70
+        try{
71 71
             $method = new \ReflectionMethod($this, static::CHECK_METHOD);
72 72
             $method->setAccessible(true);
73
-        } catch (\ReflectionException $e) {
73
+        }catch (\ReflectionException $e){
74 74
             throw new RuleException($e->getMessage(), $e->getCode(), $e);
75 75
         }
76 76
 
77
-        try {
77
+        try{
78 78
             return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters));
79
-        } catch (\Throwable $e) {
79
+        }catch (\Throwable $e){
80 80
             throw new RuleException(sprintf('[%s] %s', get_class($this), $e->getMessage()), $e->getCode(), $e);
81 81
         }
82 82
     }
Please login to merge, or discard this patch.
Braces   +12 added lines, -5 removed lines patch added patch discarded remove patch
@@ -63,20 +63,27 @@
 block discarded – undo
63 63
         $parameters = compact('actor', 'permission', 'context') + $context;
64 64
 
65 65
         //Mounting aliases
66
-        foreach (static::ALIASES as $target => $alias) {
66
+        foreach (static::ALIASES as $target => $alias)
67
+        {
67 68
             $parameters[$target] = $parameters[$alias];
68 69
         }
69 70
 
70
-        try {
71
+        try
72
+        {
71 73
             $method = new \ReflectionMethod($this, static::CHECK_METHOD);
72 74
             $method->setAccessible(true);
73
-        } catch (\ReflectionException $e) {
75
+        }
76
+        catch (\ReflectionException $e)
77
+        {
74 78
             throw new RuleException($e->getMessage(), $e->getCode(), $e);
75 79
         }
76 80
 
77
-        try {
81
+        try
82
+        {
78 83
             return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters));
79
-        } catch (\Throwable $e) {
84
+        }
85
+        catch (\Throwable $e)
86
+        {
80 87
             throw new RuleException(sprintf('[%s] %s', get_class($this), $e->getMessage()), $e->getCode(), $e);
81 88
         }
82 89
     }
Please login to merge, or discard this patch.
src/Security/src/RuleManager.php 2 patches
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -42,11 +42,11 @@  discard block
 block discarded – undo
42 42
      */
43 43
     public function set(string $name, $rule = null): RuleManager
44 44
     {
45
-        if (empty($rule)) {
45
+        if (empty($rule)){
46 46
             $rule = $name;
47 47
         }
48 48
 
49
-        if (!$this->validateRule($rule)) {
49
+        if (!$this->validateRule($rule)){
50 50
             throw new RuleException("Unable to set rule '{$name}', invalid rule body");
51 51
         }
52 52
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
      */
63 63
     public function remove(string $name): RuleManager
64 64
     {
65
-        if (!$this->has($name)) {
65
+        if (!$this->has($name)){
66 66
             throw new RuleException("Undefined rule '{$name}'");
67 67
         }
68 68
 
@@ -76,11 +76,11 @@  discard block
 block discarded – undo
76 76
      */
77 77
     public function has(string $name): bool
78 78
     {
79
-        if (isset($this->rules[$name])) {
79
+        if (isset($this->rules[$name])){
80 80
             return true;
81 81
         }
82 82
 
83
-        if (class_exists($name)) {
83
+        if (class_exists($name)){
84 84
             //We are allowing to use class names without direct registration
85 85
             return true;
86 86
         }
@@ -94,26 +94,26 @@  discard block
 block discarded – undo
94 94
      */
95 95
     public function get(string $name): RuleInterface
96 96
     {
97
-        if (!$this->has($name)) {
97
+        if (!$this->has($name)){
98 98
             throw new RuleException("Undefined rule '{$name}'");
99 99
         }
100 100
 
101
-        if (!isset($this->rules[$name])) {
101
+        if (!isset($this->rules[$name])){
102 102
             //Rule represented as class name
103 103
             $rule = $name;
104
-        } else {
104
+        }else{
105 105
             $rule = $this->rules[$name];
106 106
         }
107 107
 
108
-        if ($rule instanceof RuleInterface) {
108
+        if ($rule instanceof RuleInterface){
109 109
             return $rule;
110 110
         }
111 111
 
112
-        if (is_string($rule)) {
112
+        if (is_string($rule)){
113 113
             //We are expecting that rule points to
114 114
             $rule = $this->container->get($rule);
115 115
 
116
-            if (!$rule instanceof RuleInterface) {
116
+            if (!$rule instanceof RuleInterface){
117 117
                 throw new RuleException(sprintf(
118 118
                     "Rule '%s' must point to RuleInterface, '%s' given",
119 119
                     $name,
@@ -136,18 +136,18 @@  discard block
 block discarded – undo
136 136
      */
137 137
     private function validateRule($rule): bool
138 138
     {
139
-        if ($rule instanceof \Closure || $rule instanceof RuleInterface) {
139
+        if ($rule instanceof \Closure || $rule instanceof RuleInterface){
140 140
             return true;
141 141
         }
142 142
 
143
-        if (is_array($rule)) {
143
+        if (is_array($rule)){
144 144
             return is_callable($rule, true);
145 145
         }
146 146
 
147
-        if (is_string($rule) && class_exists($rule)) {
148
-            try {
147
+        if (is_string($rule) && class_exists($rule)){
148
+            try{
149 149
                 $reflection = new \ReflectionClass($rule);
150
-            } catch (\ReflectionException $e) {
150
+            }catch (\ReflectionException $e){
151 151
                 return false;
152 152
             }
153 153
 
Please login to merge, or discard this patch.
Braces   +34 added lines, -16 removed lines patch added patch discarded remove patch
@@ -42,11 +42,13 @@  discard block
 block discarded – undo
42 42
      */
43 43
     public function set(string $name, $rule = null): RuleManager
44 44
     {
45
-        if (empty($rule)) {
45
+        if (empty($rule))
46
+        {
46 47
             $rule = $name;
47 48
         }
48 49
 
49
-        if (!$this->validateRule($rule)) {
50
+        if (!$this->validateRule($rule))
51
+        {
50 52
             throw new RuleException("Unable to set rule '{$name}', invalid rule body");
51 53
         }
52 54
 
@@ -62,7 +64,8 @@  discard block
 block discarded – undo
62 64
      */
63 65
     public function remove(string $name): RuleManager
64 66
     {
65
-        if (!$this->has($name)) {
67
+        if (!$this->has($name))
68
+        {
66 69
             throw new RuleException("Undefined rule '{$name}'");
67 70
         }
68 71
 
@@ -76,11 +79,13 @@  discard block
 block discarded – undo
76 79
      */
77 80
     public function has(string $name): bool
78 81
     {
79
-        if (isset($this->rules[$name])) {
82
+        if (isset($this->rules[$name]))
83
+        {
80 84
             return true;
81 85
         }
82 86
 
83
-        if (class_exists($name)) {
87
+        if (class_exists($name))
88
+        {
84 89
             //We are allowing to use class names without direct registration
85 90
             return true;
86 91
         }
@@ -94,26 +99,33 @@  discard block
 block discarded – undo
94 99
      */
95 100
     public function get(string $name): RuleInterface
96 101
     {
97
-        if (!$this->has($name)) {
102
+        if (!$this->has($name))
103
+        {
98 104
             throw new RuleException("Undefined rule '{$name}'");
99 105
         }
100 106
 
101
-        if (!isset($this->rules[$name])) {
107
+        if (!isset($this->rules[$name]))
108
+        {
102 109
             //Rule represented as class name
103 110
             $rule = $name;
104
-        } else {
111
+        }
112
+        else
113
+        {
105 114
             $rule = $this->rules[$name];
106 115
         }
107 116
 
108
-        if ($rule instanceof RuleInterface) {
117
+        if ($rule instanceof RuleInterface)
118
+        {
109 119
             return $rule;
110 120
         }
111 121
 
112
-        if (is_string($rule)) {
122
+        if (is_string($rule))
123
+        {
113 124
             //We are expecting that rule points to
114 125
             $rule = $this->container->get($rule);
115 126
 
116
-            if (!$rule instanceof RuleInterface) {
127
+            if (!$rule instanceof RuleInterface)
128
+            {
117 129
                 throw new RuleException(sprintf(
118 130
                     "Rule '%s' must point to RuleInterface, '%s' given",
119 131
                     $name,
@@ -136,18 +148,24 @@  discard block
 block discarded – undo
136 148
      */
137 149
     private function validateRule($rule): bool
138 150
     {
139
-        if ($rule instanceof \Closure || $rule instanceof RuleInterface) {
151
+        if ($rule instanceof \Closure || $rule instanceof RuleInterface)
152
+        {
140 153
             return true;
141 154
         }
142 155
 
143
-        if (is_array($rule)) {
156
+        if (is_array($rule))
157
+        {
144 158
             return is_callable($rule, true);
145 159
         }
146 160
 
147
-        if (is_string($rule) && class_exists($rule)) {
148
-            try {
161
+        if (is_string($rule) && class_exists($rule))
162
+        {
163
+            try
164
+            {
149 165
                 $reflection = new \ReflectionClass($rule);
150
-            } catch (\ReflectionException $e) {
166
+            }
167
+            catch (\ReflectionException $e)
168
+            {
151 169
                 return false;
152 170
             }
153 171
 
Please login to merge, or discard this patch.
src/Security/src/Traits/GuardedTrait.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
28 28
     public function getGuard(): GuardInterface
29 29
     {
30 30
         $container = ContainerScope::getContainer();
31
-        if (empty($container) || !$container->has(GuardInterface::class)) {
31
+        if (empty($container) || !$container->has(GuardInterface::class)){
32 32
             throw new ScopeException(
33 33
                 'Unable to get `GuardInterface`, binding is missing or container scope is not set'
34 34
             );
@@ -65,9 +65,9 @@  discard block
 block discarded – undo
65 65
      */
66 66
     protected function resolvePermission(string $permission): string
67 67
     {
68
-        if (defined('static::GUARD_NAMESPACE')) {
68
+        if (defined('static::GUARD_NAMESPACE')){
69 69
             // Yay! Isolation
70
-            $permission = constant(get_called_class() . '::' . 'GUARD_NAMESPACE') . '.' . $permission;
70
+            $permission = constant(get_called_class().'::'.'GUARD_NAMESPACE').'.'.$permission;
71 71
         }
72 72
 
73 73
         return $permission;
Please login to merge, or discard this patch.
Braces   +4 added lines, -2 removed lines patch added patch discarded remove patch
@@ -28,7 +28,8 @@  discard block
 block discarded – undo
28 28
     public function getGuard(): GuardInterface
29 29
     {
30 30
         $container = ContainerScope::getContainer();
31
-        if (empty($container) || !$container->has(GuardInterface::class)) {
31
+        if (empty($container) || !$container->has(GuardInterface::class))
32
+        {
32 33
             throw new ScopeException(
33 34
                 'Unable to get `GuardInterface`, binding is missing or container scope is not set'
34 35
             );
@@ -65,7 +66,8 @@  discard block
 block discarded – undo
65 66
      */
66 67
     protected function resolvePermission(string $permission): string
67 68
     {
68
-        if (defined('static::GUARD_NAMESPACE')) {
69
+        if (defined('static::GUARD_NAMESPACE'))
70
+        {
69 71
             // Yay! Isolation
70 72
             $permission = constant(get_called_class() . '::' . 'GUARD_NAMESPACE') . '.' . $permission;
71 73
         }
Please login to merge, or discard this patch.