Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strong type NameTrait::$name and TrackableTrait::$shortName properties #400

Merged
merged 7 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Form
{
use CollectionTrait;

protected $fields = [];
protected array $fields = [];

public function addField(string $name, $seed = [])
{
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using containers:
The natural solution to the problem is to create array like this:

```
public $fields = [];
public array $fields = [];
```

After that you would need to create code for adding objects into container, removing,
Expand All @@ -46,7 +46,7 @@ class Form
{
use CollectionTrait;

public $fields = [];
public array $fields = [];

public function addField(string $name, $seed = [])
{
Expand Down
4 changes: 2 additions & 2 deletions docs/initializer.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FormField

class FormField_Input extends FormField
{
public $value = null;
public string $value;

protected function init(): void
{
Expand Down Expand Up @@ -64,7 +64,7 @@ class FormField
use TrackableTrait;
use NameTrait;

public $value = null;
public string $value;

protected function init(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function _addIntoCollection(string $name, object $item, string $collec
$item->shortName = $name;
$item->setOwner($this);
if (TraitUtil::hasTrackableTrait($this) && TraitUtil::hasNameTrait($this) && TraitUtil::hasNameTrait($item)) {
$item->name = $this->_shortenMl($this->name . '-' . $collection, $item->shortName, $item->name); // @phpstan-ignore-line
$item->name = $this->_shortenMl(($this->name ?? '') . '-' . $collection, $item->shortName, $item->name ?? null); // @phpstan-ignore-line
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait ContainerTrait
public array $elements = [];

/** @var array<string, int> */
private $_elementNameCounts = [];
private array $_elementNameCounts = [];

/**
* Returns unique element name based on desired name.
Expand Down Expand Up @@ -86,7 +86,7 @@ protected function _addContainer(object $element, array $args): void
} elseif (isset($args['name'])) {
$name = $args['name'];
unset($args['name']);
} elseif ($element->shortName !== null) {
} elseif (($element->shortName ?? null) !== null) {
$name = $this->_uniqueElementName($element->shortName);
} else {
$desiredName = $element->getDesiredName();
Expand All @@ -110,7 +110,7 @@ protected function _addContainer(object $element, array $args): void
$element->setOwner($this);
$element->shortName = $name;
if (TraitUtil::hasTrackableTrait($this) && TraitUtil::hasNameTrait($this) && TraitUtil::hasNameTrait($element)) {
$element->name = $this->_shorten($this->name ?: '', $element->shortName, $element->name); // @phpstan-ignore-line
$element->name = $this->_shorten($this->name ?? '', $element->shortName, $element->name ?? null); // @phpstan-ignore-line
}

$this->elements[$element->shortName] = $element;
Expand Down
3 changes: 1 addition & 2 deletions src/InitializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
*/
trait InitializerTrait
{
/** @var bool */
private $_initialized = false;
private bool $_initialized = false;

/**
* Initialize object. Always call parent::init(). Do not call directly.
Expand Down
4 changes: 2 additions & 2 deletions src/NameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
*/
trait NameTrait
{
/** @var string Unique object name. */
public $name;
/** @var non-falsy-string Unique object name. */
public string $name;
}
4 changes: 2 additions & 2 deletions src/TrackableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ trait TrackableTrait
/** @var QuietObjectWrapper<object>|null Link to (owner) object into which we added this object. */
private ?QuietObjectWrapper $_owner = null;

/** @var string Name of the object in owner's element array. */
public $shortName;
/** @var non-falsy-string Name of the object in owner's element array. */
public string $shortName;

public function issetOwner(): bool
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CollectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CollectionMock
use CollectionTrait;

/** @var array<string, FieldMock> */
protected $fields = [];
protected array $fields = [];

/**
* @param array<mixed>|FieldMock|null $seed
Expand Down
9 changes: 4 additions & 5 deletions tests/CollectionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ public function testBasicWithApp(): void
{
$m = new CollectionMockWithApp();
$m->setApp(new class() {
/** @var string */
public $name = 'app';
/** @var int */
public $maxNameLength = 40;
public string $name = 'app';

public int $maxNameLength = 40;
/** @var array<string, string> */
public $uniqueNameHashes = [];
public array $uniqueNameHashes = [];
});
$m->name = 'form';

Expand Down
3 changes: 1 addition & 2 deletions tests/ConfigTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class ConfigTraitTest extends TestCase
{
/** @var string */
public $dir = __DIR__ . '/config_test';
public string $dir = __DIR__ . '/config_test';

public function testFileRead(): void
{
Expand Down
3 changes: 1 addition & 2 deletions tests/FieldMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ class FieldMock
{
use DiContainerTrait;

/** @var string */
public $name;
public string $name;
}
Loading