Skip to content

Commit

Permalink
Element // improve return types by using "static" instead of the spec…
Browse files Browse the repository at this point in the history
…ific class or implemented interface.
  • Loading branch information
Chrico committed Jul 11, 2023
1 parent d52141d commit fbc016f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 61 deletions.
48 changes: 19 additions & 29 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/phpunit/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/phpunit/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tmp"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/phpunit/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
<report>
<html outputDirectory="tmp"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/phpunit/Unit</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
4 changes: 2 additions & 2 deletions src/Element/ChoiceElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ChoiceElement extends Element implements ChoiceElementInterface
/**
* @param ChoiceListInterface $list
*
* @return ChoiceElement
* @return static
*/
public function withChoices(ChoiceListInterface $list): ChoiceElement
public function withChoices(ChoiceListInterface $list): static
{
$this->list = $list;

Expand Down
12 changes: 6 additions & 6 deletions src/Element/CollectionElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class CollectionElement extends Element implements CollectionElementInterface
/**
* @param ElementInterface[] $elements
*
* @return CollectionElement
* @return static
*/
public function withElement(ElementInterface ...$elements): CollectionElement
public function withElement(ElementInterface ...$elements): static
{
array_walk(
$elements,
Expand Down Expand Up @@ -81,9 +81,9 @@ public function elementExists(string $name): bool
* @param string $key
* @param bool|int|string $value
*
* @return CollectionElement
* @return static
*/
public function withAttribute(string $key, $value): ElementInterface
public function withAttribute(string $key, $value): static
{
if ($key === 'value' && is_array($value)) {
foreach ($this->elements as $name => $element) {
Expand Down Expand Up @@ -130,9 +130,9 @@ public function elements(): array
*
* @param array $errors
*
* @return CollectionElement
* @return static
*/
public function withErrors(array $errors = []): CollectionElement
public function withErrors(array $errors = []): static
{
$this->allErrors = $errors;

Expand Down
4 changes: 2 additions & 2 deletions src/Element/DescriptionAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function description(): string
/**
* @param string $description
*
* @return self
* @return static
*/
public function withDescription(string $description)
public function withDescription(string $description): static
{
$this->description = $description;

Expand Down
28 changes: 14 additions & 14 deletions src/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function __construct(string $name)
* @param string $key
* @param bool|int|string $value
*
* @return Element
* @return static
*/
public function withAttribute(string $key, $value): ElementInterface
public function withAttribute(string $key, $value): static
{
$this->attributes[$key] = $value;

Expand Down Expand Up @@ -114,9 +114,9 @@ public function value()
/**
* @param string $value
*
* @return Element
* @return static
*/
public function withValue($value): Element
public function withValue($value): static
{
$this->withAttribute('value', $value);

Expand All @@ -134,9 +134,9 @@ public function attributes(): array
/**
* @param array $attributes
*
* @return Element
* @return static
*/
public function withAttributes(array $attributes = []): Element
public function withAttributes(array $attributes = []): static
{
$this->attributes = array_merge(
$this->attributes,
Expand All @@ -157,9 +157,9 @@ public function options(): array
/**
* @param array $options
*
* @return Element
* @return static
*/
public function withOptions(array $options = []): Element
public function withOptions(array $options = []): static
{
$this->options = array_merge(
$this->options,
Expand All @@ -173,9 +173,9 @@ public function withOptions(array $options = []): Element
* @param string $key
* @param int|string $value
*
* @return Element
* @return static
*/
public function withOption(string $key, $value): Element
public function withOption(string $key, $value): static
{
$this->options[$key] = $value;

Expand All @@ -199,9 +199,9 @@ public function option(string $key)
/**
* @param callable $callable
*
* @return $this
* @return static
*/
public function withFilter(callable $callable): Element
public function withFilter(callable $callable): static
{
$this->filter = $callable;

Expand All @@ -220,9 +220,9 @@ public function filter($value)
/**
* @param callable $callable
*
* @return $this
* @return static
*/
public function withValidator(callable $callable): Element
public function withValidator(callable $callable): static
{
$this->validator = $callable;

Expand Down
4 changes: 2 additions & 2 deletions src/Element/ErrorAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function errors(): array
/**
* @param array $errors
*
* @return self
* @return static
*/
public function withErrors(array $errors = [])
public function withErrors(array $errors = []): static
{
$this->errors = array_merge($this->errors, $errors);

Expand Down
8 changes: 4 additions & 4 deletions src/Element/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Form extends CollectionElement implements FormInterface
* @param string $key
* @param string|array $value
*
* @return ElementInterface
* @return static
* @throws LogicException
*
*/
public function withAttribute(string $key, $value): ElementInterface
public function withAttribute(string $key, $value): static
{
if ($key === 'value' && is_array($value)) {
$this->withData($value);
Expand All @@ -52,11 +52,11 @@ public function withAttribute(string $key, $value): ElementInterface
/**
* @param array $data
*
* @return FormInterface
* @return static
* @throws LogicException
*
*/
public function withData(array $data = []): FormInterface
public function withData(array $data = []): static
{
if ($this->isSubmitted) {
throw new LogicException('You cannot change data of a submitted form.');
Expand Down
2 changes: 0 additions & 2 deletions src/Element/LabelAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function labelAttributes(): array;

/**
* @param array $labelAttributes
*
* @return self
*/
public function withLabelAttributes(array $labelAttributes = []);
}
2 changes: 2 additions & 0 deletions tests/phpunit/Unit/View/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function testRender(): void
$expected_name = 'foo';

$element_stub = Mockery::mock(ElementInterface::class);
$element_stub->allows('id')
->andReturn($expected_name);
$element_stub->allows('name')
->andReturn($expected_name);
$element_stub->allows('type')
Expand Down

0 comments on commit fbc016f

Please sign in to comment.