-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from DaveLiddament/feature/add-namespace-visib…
…ility Add NamespaceVisibility attribute
- Loading branch information
Showing
17 changed files
with
769 additions
and
141 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
examples/namespaceVisibility/namespaceVisibilityOnClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnClass { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
|
||
#[NamespaceVisibility] | ||
class Person | ||
{ | ||
public function updateName(): void | ||
{ | ||
} | ||
|
||
public function update(): void | ||
{ | ||
$this->updateName(); // OK: Calls to same class allowed | ||
} | ||
} | ||
|
||
class Updater | ||
{ | ||
public function updater(Person $person): void | ||
{ | ||
$person->updateName(); // OK: Calls within same namespace allowed | ||
} | ||
} | ||
|
||
$person = new Person(); | ||
$person->updateName(); // OK: Calls within same namespace allowed | ||
|
||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnClass\SubNamesapce { | ||
|
||
use NamespaceVisibilityOnClass\Person; | ||
|
||
class AnotherClass | ||
{ | ||
public function update(): void | ||
{ | ||
$person = new Person(); | ||
$person->updateName(); // OK: Calls within the same subnamespace allowed. | ||
} | ||
} | ||
} | ||
|
||
|
||
namespace NamespaceOnClass2 { | ||
|
||
use PackageOnClass\Person; | ||
|
||
class AnotherUpdater | ||
{ | ||
public function update(): void | ||
{ | ||
$person = new Person(); | ||
$person->updateName(); // ERROR: Call to Person::update method which has namespace visibility. | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/namespaceVisibility/namespaceVisibilityOnClassExcludeSubNamespaces.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnClassExcludeSubNamespaces { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
|
||
#[NamespaceVisibility(excludeSubNamespaces: true)] | ||
class Person | ||
{ | ||
public function updateName(): void | ||
{ | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnClassExcludeSubNamespace\SubNamesapce { | ||
|
||
use NamespaceVisibilityOnClassExcludeSubNamespaces\Person; | ||
|
||
class AnotherClass | ||
{ | ||
public function update(): void | ||
{ | ||
$person = new Person(); | ||
$person->updateName(); // ERROR: Call to Person::updateName in a sub namespace, where sub namespace is not allowed. | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/namespaceVisibility/namespaceVisibilityOnClassForDifferentNamespaces.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnClassForDifferentNamespaces { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
|
||
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnClassDifferentNamespace')] | ||
class Person | ||
{ | ||
public function updateName(): void | ||
{ | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnClassDifferentNamespace { | ||
|
||
use NamespaceVisibilityOnClassForDifferentNamespaces\Person; | ||
|
||
class AnotherClass | ||
{ | ||
public function update(): void | ||
{ | ||
$person = new Person(); | ||
$person->updateName(); // OK: Call to Person::updateName is in the allowed namespace. | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/namespaceVisibility/namespaceVisibilityOnConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace NamespaceVisibilityOnConstructor { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility] | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function create(): Person | ||
{ | ||
return new Person(); // OK: Calls to same class allowed. | ||
} | ||
} | ||
|
||
class PersonBuilder | ||
{ | ||
public function build(): Person | ||
{ | ||
return new Person(); // OK: Calls within the same namespace allowed. | ||
} | ||
} | ||
|
||
new Person(); // OK: Calls withing the same namespace allowed | ||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnConstructor\SubNamespace { | ||
|
||
use NamespaceVisibilityOnConstructor\Person; | ||
|
||
class AnotherPersonBuilder | ||
{ | ||
public function create(): void | ||
{ | ||
new Person(); // OK, sub namespace of NamespaceVisibilityOnConstructor | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
namespace NamespaceVisibilityOnConstructor2 { | ||
|
||
use NamespaceVisibilityOnConstructor\Person; | ||
|
||
class Exam | ||
{ | ||
public function addPerson(): void | ||
{ | ||
new Person(); // ERROR: Call to Person::__construct has namespace visibility, this is a different namespace | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
examples/namespaceVisibility/namespaceVisibilityOnConstructorExcludeSubNamespaces.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace NamespaceVisibilityOnConstructorExcludeSubNamespaces { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility(excludeSubNamespaces: true)] | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function create(): Person | ||
{ | ||
return new Person(); // OK: Calls to same class allowed. | ||
} | ||
} | ||
|
||
class PersonBuilder | ||
{ | ||
public function build(): Person | ||
{ | ||
return new Person(); // OK: Calls within the same namespace allowed. | ||
} | ||
} | ||
|
||
new Person(); // OK: Calls withing the same namespace allowed | ||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnConstructorExcludeSubNamespace\SubNamespace { | ||
|
||
use NamespaceVisibilityOnConstructorExcludeSubNamespaces\Person; | ||
|
||
class AnotherPersonBuilder | ||
{ | ||
public function create(): void | ||
{ | ||
new Person(); // Error, sub namespace of NamespaceVisibilityOnConstructor and this is not allowed | ||
} | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
examples/namespaceVisibility/namespaceVisibilityOnConstructorForDifferentNamespaces.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace NamespaceVisibilityOnConstructorForDifferentNamespaces { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnConstructorDifferentNamespace')] | ||
public function __construct() | ||
{ | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnConstructorDifferentNamespace { | ||
|
||
use NamespaceVisibilityOnConstructorForDifferentNamespaces\Person; | ||
|
||
class AnotherPersonBuilder | ||
{ | ||
public function create(): void | ||
{ | ||
new Person(); // OK, called to Person::__construct from allowed namespace. | ||
} | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
examples/namespaceVisibility/namespaceVisibilityOnMethod.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnMethod { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility] | ||
public function updateName(): void | ||
{ | ||
} | ||
|
||
public function update(): void | ||
{ | ||
$this->updateName(); // OK | ||
} | ||
} | ||
|
||
class Updater | ||
{ | ||
public function updater(Person $person): void | ||
{ | ||
$person->updateName(); // OK | ||
} | ||
} | ||
|
||
$person = new Person(); | ||
$person->updateName(); // OK | ||
} | ||
|
||
|
||
|
||
namespace NamespaceVisibilityOnMethod\SubNamespace { | ||
|
||
use NamespaceVisibilityOnMethod\Person; | ||
class AnotherPersonUpdater | ||
{ | ||
public function update(Person $person): void | ||
{ | ||
$person->updateName(); // OK - Subnamespace of NamespaceVisibilityOnMethod, which is allowed | ||
} | ||
} | ||
} | ||
|
||
|
||
namespace NamespaceVisibilityOnMethod2 { | ||
|
||
use NamespaceVisibilityOnMethod\Person; | ||
|
||
class AnotherUpdater | ||
{ | ||
public function update(): void | ||
{ | ||
$person = new Person(); | ||
$person->updateName(); // ERROR: Call to Person::updateName which has namespaceVisibility visibility | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/namespaceVisibility/namespaceVisibilityOnMethodExcludeSubNamespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnMethodExcludeSubNamespace { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility(excludeSubNamespaces: true)] | ||
public function updateName(): void | ||
{ | ||
} | ||
|
||
public function update(): void | ||
{ | ||
$this->updateName(); // OK | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
namespace NamespaceVisibilityOnMethodExluceSubNamespace\SubNamespace { | ||
|
||
use NamespaceVisibilityOnMethodExcludeSubNamespace\Person; | ||
class AnotherPersonUpdater | ||
{ | ||
public function update(Person $person): void | ||
{ | ||
$person->updateName(); // ERROR - Subnamespace of NamespaceVisibilityOnMethod, which is not allowed | ||
} | ||
} | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
examples/namespaceVisibility/namespaceVisibilityOnMethodForDifferentNamespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NamespaceVisibilityOnMethodForDifferentNamespace { | ||
|
||
use DaveLiddament\PhpLanguageExtensions\NamespaceVisibility; | ||
|
||
class Person | ||
{ | ||
#[NamespaceVisibility(namespace: 'NamespaceVisibilityOnMethodDifferentNamespace')] | ||
public function updateName(): void | ||
{ | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
namespace NamespaceVisibilityOnMethodDifferentNamespace { | ||
|
||
use NamespaceVisibilityOnMethodForDifferentNamespace\Person; | ||
class AnotherPersonUpdater | ||
{ | ||
public function update(Person $person): void | ||
{ | ||
$person->updateName(); // OK, call to Person::updateName from allowed namespace. | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.