Skip to content

Commit

Permalink
Merge pull request #16 from DaveLiddament/feature/add-namespace-visib…
Browse files Browse the repository at this point in the history
…ility

Add NamespaceVisibility attribute
  • Loading branch information
DaveLiddament authored Jan 5, 2023
2 parents dbc62ca + f7401db commit fa07440
Show file tree
Hide file tree
Showing 17 changed files with 769 additions and 141 deletions.
322 changes: 182 additions & 140 deletions README.md

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions examples/namespaceVisibility/namespaceVisibilityOnClass.php
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.
}
}
}
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.
}
}
}
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 examples/namespaceVisibility/namespaceVisibilityOnConstructor.php
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
}
}
}
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
}
}
}

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 examples/namespaceVisibility/namespaceVisibilityOnMethod.php
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
}
}
}
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
}
}
}

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.
}
}
}

Loading

0 comments on commit fa07440

Please sign in to comment.