Skip to content

Commit fa07440

Browse files
Merge pull request #16 from DaveLiddament/feature/add-namespace-visibility
Add NamespaceVisibility attribute
2 parents dbc62ca + f7401db commit fa07440

17 files changed

Lines changed: 769 additions & 141 deletions

README.md

Lines changed: 182 additions & 140 deletions
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnClass {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
10+
#[NamespaceVisibility]
11+
class Person
12+
{
13+
public function updateName(): void
14+
{
15+
}
16+
17+
public function update(): void
18+
{
19+
$this->updateName(); // OK: Calls to same class allowed
20+
}
21+
}
22+
23+
class Updater
24+
{
25+
public function updater(Person $person): void
26+
{
27+
$person->updateName(); // OK: Calls within same namespace allowed
28+
}
29+
}
30+
31+
$person = new Person();
32+
$person->updateName(); // OK: Calls within same namespace allowed
33+
34+
}
35+
36+
37+
namespace NamespaceVisibilityOnClass\SubNamesapce {
38+
39+
use NamespaceVisibilityOnClass\Person;
40+
41+
class AnotherClass
42+
{
43+
public function update(): void
44+
{
45+
$person = new Person();
46+
$person->updateName(); // OK: Calls within the same subnamespace allowed.
47+
}
48+
}
49+
}
50+
51+
52+
namespace NamespaceOnClass2 {
53+
54+
use PackageOnClass\Person;
55+
56+
class AnotherUpdater
57+
{
58+
public function update(): void
59+
{
60+
$person = new Person();
61+
$person->updateName(); // ERROR: Call to Person::update method which has namespace visibility.
62+
}
63+
}
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnClassExcludeSubNamespaces {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
10+
#[NamespaceVisibility(excludeSubNamespaces: true)]
11+
class Person
12+
{
13+
public function updateName(): void
14+
{
15+
}
16+
}
17+
18+
}
19+
20+
21+
namespace NamespaceVisibilityOnClassExcludeSubNamespace\SubNamesapce {
22+
23+
use NamespaceVisibilityOnClassExcludeSubNamespaces\Person;
24+
25+
class AnotherClass
26+
{
27+
public function update(): void
28+
{
29+
$person = new Person();
30+
$person->updateName(); // ERROR: Call to Person::updateName in a sub namespace, where sub namespace is not allowed.
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnClassForDifferentNamespaces {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
10+
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnClassDifferentNamespace')]
11+
class Person
12+
{
13+
public function updateName(): void
14+
{
15+
}
16+
}
17+
18+
}
19+
20+
21+
namespace NamespaceVisibilityOnClassDifferentNamespace {
22+
23+
use NamespaceVisibilityOnClassForDifferentNamespaces\Person;
24+
25+
class AnotherClass
26+
{
27+
public function update(): void
28+
{
29+
$person = new Person();
30+
$person->updateName(); // OK: Call to Person::updateName is in the allowed namespace.
31+
}
32+
}
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace NamespaceVisibilityOnConstructor {
4+
5+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
6+
7+
class Person
8+
{
9+
#[NamespaceVisibility]
10+
public function __construct()
11+
{
12+
}
13+
14+
public static function create(): Person
15+
{
16+
return new Person(); // OK: Calls to same class allowed.
17+
}
18+
}
19+
20+
class PersonBuilder
21+
{
22+
public function build(): Person
23+
{
24+
return new Person(); // OK: Calls within the same namespace allowed.
25+
}
26+
}
27+
28+
new Person(); // OK: Calls withing the same namespace allowed
29+
}
30+
31+
32+
namespace NamespaceVisibilityOnConstructor\SubNamespace {
33+
34+
use NamespaceVisibilityOnConstructor\Person;
35+
36+
class AnotherPersonBuilder
37+
{
38+
public function create(): void
39+
{
40+
new Person(); // OK, sub namespace of NamespaceVisibilityOnConstructor
41+
}
42+
}
43+
}
44+
45+
46+
47+
namespace NamespaceVisibilityOnConstructor2 {
48+
49+
use NamespaceVisibilityOnConstructor\Person;
50+
51+
class Exam
52+
{
53+
public function addPerson(): void
54+
{
55+
new Person(); // ERROR: Call to Person::__construct has namespace visibility, this is a different namespace
56+
}
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace NamespaceVisibilityOnConstructorExcludeSubNamespaces {
4+
5+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
6+
7+
class Person
8+
{
9+
#[NamespaceVisibility(excludeSubNamespaces: true)]
10+
public function __construct()
11+
{
12+
}
13+
14+
public static function create(): Person
15+
{
16+
return new Person(); // OK: Calls to same class allowed.
17+
}
18+
}
19+
20+
class PersonBuilder
21+
{
22+
public function build(): Person
23+
{
24+
return new Person(); // OK: Calls within the same namespace allowed.
25+
}
26+
}
27+
28+
new Person(); // OK: Calls withing the same namespace allowed
29+
}
30+
31+
32+
namespace NamespaceVisibilityOnConstructorExcludeSubNamespace\SubNamespace {
33+
34+
use NamespaceVisibilityOnConstructorExcludeSubNamespaces\Person;
35+
36+
class AnotherPersonBuilder
37+
{
38+
public function create(): void
39+
{
40+
new Person(); // Error, sub namespace of NamespaceVisibilityOnConstructor and this is not allowed
41+
}
42+
}
43+
}
44+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace NamespaceVisibilityOnConstructorForDifferentNamespaces {
4+
5+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
6+
7+
class Person
8+
{
9+
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnConstructorDifferentNamespace')]
10+
public function __construct()
11+
{
12+
}
13+
14+
}
15+
}
16+
17+
18+
namespace NamespaceVisibilityOnConstructorDifferentNamespace {
19+
20+
use NamespaceVisibilityOnConstructorForDifferentNamespaces\Person;
21+
22+
class AnotherPersonBuilder
23+
{
24+
public function create(): void
25+
{
26+
new Person(); // OK, called to Person::__construct from allowed namespace.
27+
}
28+
}
29+
}
30+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnMethod {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
class Person
10+
{
11+
#[NamespaceVisibility]
12+
public function updateName(): void
13+
{
14+
}
15+
16+
public function update(): void
17+
{
18+
$this->updateName(); // OK
19+
}
20+
}
21+
22+
class Updater
23+
{
24+
public function updater(Person $person): void
25+
{
26+
$person->updateName(); // OK
27+
}
28+
}
29+
30+
$person = new Person();
31+
$person->updateName(); // OK
32+
}
33+
34+
35+
36+
namespace NamespaceVisibilityOnMethod\SubNamespace {
37+
38+
use NamespaceVisibilityOnMethod\Person;
39+
class AnotherPersonUpdater
40+
{
41+
public function update(Person $person): void
42+
{
43+
$person->updateName(); // OK - Subnamespace of NamespaceVisibilityOnMethod, which is allowed
44+
}
45+
}
46+
}
47+
48+
49+
namespace NamespaceVisibilityOnMethod2 {
50+
51+
use NamespaceVisibilityOnMethod\Person;
52+
53+
class AnotherUpdater
54+
{
55+
public function update(): void
56+
{
57+
$person = new Person();
58+
$person->updateName(); // ERROR: Call to Person::updateName which has namespaceVisibility visibility
59+
}
60+
}
61+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnMethodExcludeSubNamespace {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
class Person
10+
{
11+
#[NamespaceVisibility(excludeSubNamespaces: true)]
12+
public function updateName(): void
13+
{
14+
}
15+
16+
public function update(): void
17+
{
18+
$this->updateName(); // OK
19+
}
20+
}
21+
}
22+
23+
24+
25+
namespace NamespaceVisibilityOnMethodExluceSubNamespace\SubNamespace {
26+
27+
use NamespaceVisibilityOnMethodExcludeSubNamespace\Person;
28+
class AnotherPersonUpdater
29+
{
30+
public function update(Person $person): void
31+
{
32+
$person->updateName(); // ERROR - Subnamespace of NamespaceVisibilityOnMethod, which is not allowed
33+
}
34+
}
35+
}
36+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NamespaceVisibilityOnMethodForDifferentNamespace {
6+
7+
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility;
8+
9+
class Person
10+
{
11+
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnMethodDifferentNamespace')]
12+
public function updateName(): void
13+
{
14+
}
15+
16+
}
17+
}
18+
19+
20+
21+
namespace NamespaceVisibilityOnMethodDifferentNamespace {
22+
23+
use NamespaceVisibilityOnMethodForDifferentNamespace\Person;
24+
class AnotherPersonUpdater
25+
{
26+
public function update(Person $person): void
27+
{
28+
$person->updateName(); // OK, call to Person::updateName from allowed namespace.
29+
}
30+
}
31+
}
32+

0 commit comments

Comments
 (0)