You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/allow-in-class-with-attributes.md
+57-2Lines changed: 57 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,15 @@
1
1
## Allow in class with given attributes
2
2
3
-
It is possible to allow a previously disallowed function, method call or an attribute usage when done in a class with specified attributes.
3
+
It is possible to allow a previously disallowed item when done in a class with specified attributes.
4
4
You can use the `allowInClassWithAttributes` configuration option.
5
5
6
+
This is supported for the following items:
7
+
- function calls
8
+
- method calls
9
+
- attribute usages
10
+
- namespace usages
11
+
- classname usages
12
+
6
13
For example, if you'd have a configuration like this:
7
14
8
15
```neon
@@ -27,7 +34,7 @@ class Foo
27
34
}
28
35
```
29
36
30
-
On the other hand, if you need to disallow a method call, a function call, or an attribute usage only when present in a method from a class with a given attribute,
37
+
On the other hand, if you need to disallow an item only when present in a method from a class with a given attribute,
31
38
use `allowExceptInClassWithAttributes` (or the `disallowInClassWithAttributes` alias):
32
39
33
40
```neon
@@ -65,3 +72,51 @@ class Foo
65
72
```
66
73
67
74
The attribute names in the _allow_ directives support [fnmatch()](https://www.php.net/function.fnmatch) patterns.
75
+
76
+
### Allow namespace or classname use in `use` imports
77
+
78
+
You can allow a namespace or a classname to be used in `use` imports with `allowInUse: true`.
79
+
This can be useful when you want to disallow a namespace usage in a class with an attribute (with `allowExceptInClassWithAttributes` or `disallowInClassWithAttributes`),
80
+
but don't want the error to be reported on line with the `use` statement.
81
+
82
+
Let's have a class like this:
83
+
84
+
```php
85
+
use Foo\Bar\DisallowedClass; // line 1
86
+
87
+
#[MyAttribute]
88
+
class Waldo
89
+
{
90
+
91
+
public function fred(DisallowedClass $param) // line 7
92
+
{
93
+
}
94
+
95
+
}
96
+
```
97
+
98
+
Then with a configuration like this:
99
+
100
+
```neon
101
+
parameters:
102
+
disallowedNamespace:
103
+
-
104
+
namespace: 'Foo\Bar\DisallowedClass'
105
+
allowExceptInClassWithAttributes:
106
+
- MyAttribute
107
+
```
108
+
109
+
the error would be reported both on line 1, because `use Foo\Bar\DisallowedClass;` uses a disallowed namespace, and on line 7 because `$param` has the disallowed type.
110
+
But maybe you'd expect the error to be reported only on line 7, because _that_ is a disallowed class used in a class with the `MyAttribute` attribute.
111
+
112
+
To omit the `use` finding, you can add the `allowInUse` line, like this:
0 commit comments