Skip to content

Commit

Permalink
Merge pull request #34 from DaveLiddament/fix/examples
Browse files Browse the repository at this point in the history
FIX README and TestTag examples
  • Loading branch information
DaveLiddament authored Aug 12, 2024
2 parents 17ff755 + 2128043 commit b64b35e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ $person = new Person();

## MustUseResult

Add #[MustUseResult] attribute that can be used on methods. This enforces the result from the method call must be used.
A `#[MustUseResult]` attribute can be used on methods. This enforces the result from the method call must be used.

E.g. if you have a class like this:

Expand All @@ -158,7 +158,7 @@ You might misuse the `add` method in this way:

```php
$cost = new Money(5);
$cost->add(6); // ERROR - This statement has no effect.
$cost->add(6); // ERROR - The call to the add method has no effect.
```

But this would be OK:
Expand Down
18 changes: 15 additions & 3 deletions examples/testTag/testTagOnClass.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TestTagOnConstructor;
namespace TestTagOnClass;

use DaveLiddament\PhpLanguageExtensions\TestTag;

Expand All @@ -11,9 +11,16 @@ public function __construct()
{
}

public static function create(): Person // No Error, class can interact with itself
public static function create(): Person // OK, class can interact with itself
{
return new Person(); // No Error, class can interact with itself
return new Person(); // OK, class can interact with itself
}

public function aMethod(int $number): void
{
if ($number > 0) {
$this->aMethod($number - 1); // OK, class can interact with itself
}
}
}

Expand All @@ -28,4 +35,9 @@ public function buildPerson(): Person
{
return Person::create(); // ERROR
}

public function aMethod(Person $person): void
{
$person->aMethod(1); // ERROR
}
}
19 changes: 14 additions & 5 deletions examples/testTag/testTagOnClassIgnoredInTestClass.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TestTagOnConstructor;
namespace TestTagOnClassIgnoredInTestClass;

use DaveLiddament\PhpLanguageExtensions\TestTag;

Expand All @@ -11,21 +11,30 @@ public function __construct()
{
}

public static function create(): Person // No Error, class can interact with itself
public static function create(): Person // OK, class can interact with itself
{
return new Person(); // OK, class can interact with itself
}

public function aMethod(): void
{
return new Person(); // No Error, class can interact with itself
}
}

class PersonTest
{
public function newInstance(): Person
{
return new Person(); // No Error
return new Person(); // OK, call from test class
}

public function buildPerson(): Person
{
return Person::create(); // No error
return Person::create(); // OK, call from test class
}

public function aMethod(Person $person): void
{
$this->aMethod(); // OK, call from test class
}
}

0 comments on commit b64b35e

Please sign in to comment.