Skip to content

Commit

Permalink
used generics in phpDoc [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 10, 2023
1 parent 7429075 commit 25fcc93
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ComponentModel/ArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

/**
* Implementation of \ArrayAccess for IContainer.
* @template T of IComponent
*/
trait ArrayAccess
{
/**
* Adds the component to the container.
* @param string|int $name
* @param IComponent $component
* @param T $component
*/
public function offsetSet($name, $component): void
{
Expand All @@ -32,6 +33,7 @@ public function offsetSet($name, $component): void
/**
* Returns component specified by name. Throws exception if component doesn't exist.
* @param string|int $name
* @return T
* @throws Nette\InvalidArgumentException
*/
public function offsetGet($name): IComponent
Expand Down
7 changes: 6 additions & 1 deletion src/ComponentModel/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*
* Components are objects implementing IComponent. They has parent component and own name.
*
* @template T of IContainer
* @implements IComponent<T>
* @property-read string $name
* @property-read IContainer|null $parent
* @property-read T|null $parent
*/
abstract class Component implements IComponent
{
Expand Down Expand Up @@ -151,6 +153,7 @@ final public function getName(): ?string

/**
* Returns the parent container if any.
* @return T
*/
final public function getParent(): ?IContainer
{
Expand All @@ -161,6 +164,7 @@ final public function getParent(): ?IContainer
/**
* Sets or removes the parent of this component. This method is managed by containers and should
* not be called by applications
* @param T $parent
* @throws Nette\InvalidStateException
* @internal
*/
Expand Down Expand Up @@ -202,6 +206,7 @@ public function setParent(?IContainer $parent, ?string $name = null): static
/**
* Is called by a component when it is about to be set new parent. Descendant can
* override this method to disallow a parent change by throwing an Nette\InvalidStateException
* @param T $parent
* @throws Nette\InvalidStateException
*/
protected function validateParent(IContainer $parent): void
Expand Down
2 changes: 2 additions & 0 deletions src/ComponentModel/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/**
* ComponentContainer is default implementation of IContainer.
*
* @template T of IComponent
* @implements IContainer<T>
* @property-read iterable $components
*/
class Container extends Component implements IContainer
Expand Down
3 changes: 3 additions & 0 deletions src/ComponentModel/IComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Provides functionality required by all components.
* @template T of IContainer
*/
interface IComponent
{
Expand All @@ -25,11 +26,13 @@ function getName(): ?string;

/**
* Returns the parent container if any.
* @return ?T
*/
function getParent(): ?IContainer;

/**
* Sets the parent of this component.
* @param ?T $parent
*/
function setParent(?IContainer $parent, ?string $name = null): static;
}
6 changes: 5 additions & 1 deletion src/ComponentModel/IContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@

/**
* Containers are objects that logically contain zero or more IComponent components.
* @template T of IComponent
*/
interface IContainer extends IComponent
{
/**
* Adds the component to the container.
* @param T $component
* @return static
*/
function addComponent(IComponent $component, ?string $name);

/**
* Removes the component from the container.
* @param T $component
*/
function removeComponent(IComponent $component): void;

/**
* Returns component specified by name or path.
* @return T
* @throws Nette\InvalidArgumentException if component doesn't exist
*/
function getComponent(string $name): ?IComponent;

/**
* Iterates over descendants components.
* @return iterable<int|string,IComponent>
* @return iterable<int|string,T>
*/
function getComponents(): iterable;
}

0 comments on commit 25fcc93

Please sign in to comment.