From aeafe2ec9929a406e349912eb1f7057941a3f021 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 17 Apr 2023 13:00:29 -0400 Subject: [PATCH] More strict types --- src/ConnectionManager.php | 4 +--- src/Events/Logger.php | 4 +--- src/Models/Attributes/Guid.php | 14 ++++---------- src/Models/OpenLDAP/User.php | 8 ++------ tests/Integration/GroupTest.php | 5 ++--- tests/Integration/UserTest.php | 5 ++--- tests/Unit/Events/DispatcherTest.php | 2 +- tests/Unit/Models/Attributes/EscapeValueTest.php | 4 ++-- tests/Unit/Models/Attributes/TimestampTest.php | 12 ++++++------ tests/Unit/Models/ModelAccessorMutatorTest.php | 12 +++++++----- tests/Unit/Models/ModelAttributeAppendsTest.php | 4 ++-- tests/Unit/Models/ModelHasManyInTest.php | 2 +- tests/Unit/Models/ModelHasManyTest.php | 2 +- tests/Unit/Models/ModelHasManyUsingTest.php | 2 +- tests/Unit/Models/ModelHasOneTest.php | 2 +- tests/Unit/Models/ModelRelationTest.php | 4 ++-- tests/Unit/Models/ModelTest.php | 4 ++-- tests/Unit/Query/BuilderTest.php | 2 +- tests/Unit/Query/Model/ActiveDirectoryTest.php | 2 +- 19 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index 70943cb4..22470acb 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -23,10 +23,8 @@ class ConnectionManager /** * The events to register listeners for during initialization. - * - * @var array */ - protected $listen = [ + protected array $listen = [ 'LdapRecord\Auth\Events\*', 'LdapRecord\Query\Events\*', 'LdapRecord\Models\Events\*', diff --git a/src/Events/Logger.php b/src/Events/Logger.php index 588e453c..98c02551 100644 --- a/src/Events/Logger.php +++ b/src/Events/Logger.php @@ -13,10 +13,8 @@ class Logger { /** * The logger instance. - * - * @var LoggerInterface|null */ - protected $logger; + protected ?LoggerInterface $logger; /** * Constructor. diff --git a/src/Models/Attributes/Guid.php b/src/Models/Attributes/Guid.php index 9a65b195..c1bf423e 100644 --- a/src/Models/Attributes/Guid.php +++ b/src/Models/Attributes/Guid.php @@ -8,10 +8,8 @@ class Guid { /** * The string GUID value. - * - * @var string */ - protected $value; + protected ?string $value = null; /** * The guid structure in order by section to parse using substr(). @@ -19,10 +17,8 @@ class Guid * @author Chad Sikorra * * @see https://github.com/ldaptools/ldaptools - * - * @var array */ - protected $guidSections = [ + protected array $guidSections = [ [[-26, 2], [-28, 2], [-30, 2], [-32, 2]], [[-22, 2], [-24, 2]], [[-18, 2], [-20, 2]], @@ -36,10 +32,8 @@ class Guid * @author Chad Sikorra * * @see https://github.com/ldaptools/ldaptools - * - * @var array */ - protected $octetSections = [ + protected array $octetSections = [ [6, 4, 2, 0], [10, 8], [14, 12], @@ -59,7 +53,7 @@ public static function isValid(string $guid): bool * * @throws InvalidArgumentException */ - public function __construct($value) + public function __construct(string $value) { if (static::isValid($value)) { $this->value = $value; diff --git a/src/Models/OpenLDAP/User.php b/src/Models/OpenLDAP/User.php index 0fe97ef3..81b158cb 100644 --- a/src/Models/OpenLDAP/User.php +++ b/src/Models/OpenLDAP/User.php @@ -14,17 +14,13 @@ class User extends Entry implements Authenticatable /** * The password's attribute name. - * - * @var string */ - protected $passwordAttribute = 'userpassword'; + protected string $passwordAttribute = 'userpassword'; /** * The password's hash method. - * - * @var string */ - protected $passwordHashMethod = 'ssha'; + protected string $passwordHashMethod = 'ssha'; /** * The object classes of the LDAP model. diff --git a/tests/Integration/GroupTest.php b/tests/Integration/GroupTest.php index 7732f2ff..49d17d93 100644 --- a/tests/Integration/GroupTest.php +++ b/tests/Integration/GroupTest.php @@ -8,8 +8,7 @@ class GroupTest extends TestCase { - /** @var OrganizationalUnit */ - protected $ou; + protected OrganizationalUnit $ou; protected function setUp(): void { @@ -33,7 +32,7 @@ protected function tearDown(): void parent::tearDown(); } - protected function createGroup(array $attributes = []) + protected function createGroup(array $attributes = []): Group { $group = (new Group) ->inside($this->ou) diff --git a/tests/Integration/UserTest.php b/tests/Integration/UserTest.php index 1d76ecb1..1ef1d0bd 100644 --- a/tests/Integration/UserTest.php +++ b/tests/Integration/UserTest.php @@ -11,8 +11,7 @@ class UserTest extends TestCase { - /** @var OrganizationalUnit */ - protected $ou; + protected OrganizationalUnit $ou; protected function setUp(): void { @@ -36,7 +35,7 @@ protected function tearDown(): void parent::tearDown(); } - protected function createUser(array $attributes = []) + protected function createUser(array $attributes = []): User { $user = (new User) ->inside($this->ou) diff --git a/tests/Unit/Events/DispatcherTest.php b/tests/Unit/Events/DispatcherTest.php index b96671a1..92e93d4b 100644 --- a/tests/Unit/Events/DispatcherTest.php +++ b/tests/Unit/Events/DispatcherTest.php @@ -225,7 +225,7 @@ class ExampleEvent class ExampleListener { - public function handle(SomeEventInterface $event) + public function handle(SomeEventInterface $event): void { $_SERVER['__event.test2'] = 'baar'; } diff --git a/tests/Unit/Models/Attributes/EscapeValueTest.php b/tests/Unit/Models/Attributes/EscapeValueTest.php index 9058199f..063aeebf 100644 --- a/tests/Unit/Models/Attributes/EscapeValueTest.php +++ b/tests/Unit/Models/Attributes/EscapeValueTest.php @@ -7,9 +7,9 @@ class EscapeValueTest extends TestCase { - protected $escapedDnCharacters = ['\\', ',', '=', '+', '<', '>', ';', '"', '#']; + protected array $escapedDnCharacters = ['\\', ',', '=', '+', '<', '>', ';', '"', '#']; - protected $escapedFilterCharacters = ['\\', '*', '(', ')', "\x00"]; + protected array $escapedFilterCharacters = ['\\', '*', '(', ')', "\x00"]; public function test_all_characters_are_escaped_by_default() { diff --git a/tests/Unit/Models/Attributes/TimestampTest.php b/tests/Unit/Models/Attributes/TimestampTest.php index 2df9e542..0dba2cf2 100644 --- a/tests/Unit/Models/Attributes/TimestampTest.php +++ b/tests/Unit/Models/Attributes/TimestampTest.php @@ -9,17 +9,17 @@ class TimestampTest extends TestCase { - protected $unixTimestamp = 1601605329; + protected int $unixTimestamp = 1601605329; - protected $utcLdapTimestamp = '20201002021244Z'; + protected string $utcLdapTimestamp = '20201002021244Z'; - protected $offsetLdapTimestamp = '20201002021244-0500'; + protected string $offsetLdapTimestamp = '20201002021244-0500'; - protected $utcWindowsTimestamp = '20201002021618.0Z'; + protected string $utcWindowsTimestamp = '20201002021618.0Z'; - protected $offsetWindowsTimestamp = '20201002021618.0-0500'; + protected string $offsetWindowsTimestamp = '20201002021618.0-0500'; - protected $windowsIntegerTime = '132460789290000000'; + protected string $windowsIntegerTime = '132460789290000000'; public function test_exception_is_thrown_when_invalid_type_given() { diff --git a/tests/Unit/Models/ModelAccessorMutatorTest.php b/tests/Unit/Models/ModelAccessorMutatorTest.php index 663092dd..03df46fe 100644 --- a/tests/Unit/Models/ModelAccessorMutatorTest.php +++ b/tests/Unit/Models/ModelAccessorMutatorTest.php @@ -172,19 +172,21 @@ class ModelAccessorStub extends Model 'foo-bar' => ['baz'], ]; - public function getFooAttribute($bar) + public function getFooAttribute($bar): string { return $bar[0].'baz'; } - public function getFooBarAttribute($baz) + public function getFooBarAttribute($baz): ?string { if ($baz) { return $baz[0].'-other'; } + + return null; } - public function getZaxAttribute($value) + public function getZaxAttribute($value): string { return 'zax'; } @@ -197,12 +199,12 @@ class ModelMutatorStub extends Model 'foo-bar' => ['baz'], ]; - public function setFooAttribute($bar) + public function setFooAttribute($bar): void { $this->attributes['foo'] = [$bar.'baz']; } - public function setFooBarAttribute($baz) + public function setFooBarAttribute($baz): void { $this->attributes['foo-bar'] = [$baz.'-other']; } diff --git a/tests/Unit/Models/ModelAttributeAppendsTest.php b/tests/Unit/Models/ModelAttributeAppendsTest.php index d6e9c040..39c4812b 100644 --- a/tests/Unit/Models/ModelAttributeAppendsTest.php +++ b/tests/Unit/Models/ModelAttributeAppendsTest.php @@ -49,12 +49,12 @@ class ModelAttributeAppendsTestStub extends Model { protected array $appends = ['foo']; - public function getFooAttribute() + public function getFooAttribute(): string { return 'bar'; } - public function getFooBarAttribute() + public function getFooBarAttribute(): string { return 'foo-bar'; } diff --git a/tests/Unit/Models/ModelHasManyInTest.php b/tests/Unit/Models/ModelHasManyInTest.php index d9118b19..34030fe8 100644 --- a/tests/Unit/Models/ModelHasManyInTest.php +++ b/tests/Unit/Models/ModelHasManyInTest.php @@ -54,7 +54,7 @@ public function test_get_with_alternate_foreign_key() class ModelHasManyInStub extends Model { - public function relation($mockBuilder = null, $foreignKey = 'dn') + public function relation($mockBuilder = null, $foreignKey = 'dn'): HasManyIn { $mockBuilder = $mockBuilder ?: m::mock(Builder::class); $mockBuilder->shouldReceive('clearFilters')->once()->withNoArgs()->andReturnSelf(); diff --git a/tests/Unit/Models/ModelHasManyTest.php b/tests/Unit/Models/ModelHasManyTest.php index d628e399..e21f5ca7 100644 --- a/tests/Unit/Models/ModelHasManyTest.php +++ b/tests/Unit/Models/ModelHasManyTest.php @@ -241,7 +241,7 @@ public function test_only_related_with_no_relation_object_classes() ); } - protected function getRelation() + protected function getRelation(): HasMany { $mockBuilder = m::mock(Builder::class); $mockBuilder->shouldReceive('clearFilters')->once()->withNoArgs()->andReturnSelf(); diff --git a/tests/Unit/Models/ModelHasManyUsingTest.php b/tests/Unit/Models/ModelHasManyUsingTest.php index d517ff11..b242a8d8 100644 --- a/tests/Unit/Models/ModelHasManyUsingTest.php +++ b/tests/Unit/Models/ModelHasManyUsingTest.php @@ -45,7 +45,7 @@ public function test_detach() $this->assertEquals($relation->detach('bar'), 'bar'); } - protected function getRelation() + protected function getRelation(): HasMany { $mockBuilder = m::mock(Builder::class); $mockBuilder->shouldReceive('clearFilters')->once()->withNoArgs()->andReturnSelf(); diff --git a/tests/Unit/Models/ModelHasOneTest.php b/tests/Unit/Models/ModelHasOneTest.php index a66a037a..347dd077 100644 --- a/tests/Unit/Models/ModelHasOneTest.php +++ b/tests/Unit/Models/ModelHasOneTest.php @@ -61,7 +61,7 @@ public function test_detach() $this->assertNull($relation->detach()); } - protected function getRelation() + protected function getRelation(): HasOne { $mockBuilder = m::mock(Builder::class); $mockBuilder->shouldReceive('clearFilters')->once()->withNoArgs()->andReturnSelf(); diff --git a/tests/Unit/Models/ModelRelationTest.php b/tests/Unit/Models/ModelRelationTest.php index a2920d35..c3c283c6 100644 --- a/tests/Unit/Models/ModelRelationTest.php +++ b/tests/Unit/Models/ModelRelationTest.php @@ -251,7 +251,7 @@ public function test_related_models_are_determined_with_out_of_order_object_clas class RelationTestStub extends Relation { - protected $results = []; + protected array $results = []; public function getResults(): Collection { @@ -260,7 +260,7 @@ public function getResults(): Collection ); } - public function setResults($results) + public function setResults($results): static { $this->results = $results; diff --git a/tests/Unit/Models/ModelTest.php b/tests/Unit/Models/ModelTest.php index 06154aca..ee81a2dc 100644 --- a/tests/Unit/Models/ModelTest.php +++ b/tests/Unit/Models/ModelTest.php @@ -703,12 +703,12 @@ class ModelWithDatesStub extends Model class ModelBootingTestStub extends Model { - public static function isBooted() + public static function isBooted(): bool { return array_key_exists(static::class, static::booted()); } - public static function booted() + public static function booted(): array { return static::$booted; } diff --git a/tests/Unit/Query/BuilderTest.php b/tests/Unit/Query/BuilderTest.php index 0ef34193..d7dacbda 100644 --- a/tests/Unit/Query/BuilderTest.php +++ b/tests/Unit/Query/BuilderTest.php @@ -16,7 +16,7 @@ class BuilderTest extends TestCase { - protected function newBuilder() + protected function newBuilder(): Builder { return new Builder(new Connection([], new LdapFake())); } diff --git a/tests/Unit/Query/Model/ActiveDirectoryTest.php b/tests/Unit/Query/Model/ActiveDirectoryTest.php index 5a376d32..b8956d52 100644 --- a/tests/Unit/Query/Model/ActiveDirectoryTest.php +++ b/tests/Unit/Query/Model/ActiveDirectoryTest.php @@ -10,7 +10,7 @@ class ActiveDirectoryTest extends TestCase { - protected function newBuilder() + protected function newBuilder(): ActiveDirectoryBuilder { return (new ActiveDirectoryBuilder(new Connection([], new LdapFake())))->setModel(new Entry()); }