diff --git a/examples/Handlers.php b/examples/Handlers.php index c60f103..35e7060 100644 --- a/examples/Handlers.php +++ b/examples/Handlers.php @@ -1,6 +1,8 @@ - - */ + /** + * Classes to ignore across all handlers. + * + * @var array + */ public $ignoredClasses = []; - /** - * Paths to check during automatic discovery. - * - * @var array - */ + /** + * Paths to check during automatic discovery. + * + * @var array + */ public $autoDiscover = []; - /** - * Number of seconds to cache discovered handlers. - * Null disables caching - * - * @var int|null - */ + /** + * Number of seconds to cache discovered handlers. + * Null disables caching + * + * @var int|null + */ public $cacheDuration = DAY; } diff --git a/src/BaseHandler.php b/src/BaseHandler.php index 532ecb7..42a4f30 100644 --- a/src/BaseHandler.php +++ b/src/BaseHandler.php @@ -1,4 +1,6 @@ -getConfig()->autoDiscover)) { CLI::write('No paths are set for automatic discovery. See the config file for Tatter\Handlers.', 'yellow'); + return; } @@ -32,6 +35,7 @@ public function run(array $params = []) if (! $classes = $handlers->all()) { CLI::write('No handlers detected.', 'yellow'); + continue; } diff --git a/src/Commands/HandlersRegister.php b/src/Commands/HandlersRegister.php index 6b5baa8..cbc0020 100644 --- a/src/Commands/HandlersRegister.php +++ b/src/Commands/HandlersRegister.php @@ -1,4 +1,6 @@ -getConfig()->autoDiscover)) { CLI::write('No paths are set for automatic discovery. See the config file for Tatter\Handlers.', 'yellow'); + return; } @@ -32,6 +35,7 @@ public function run(array $params = []) if (! $classes = $handlers->all()) { CLI::write('No new handlers registered.', 'yellow'); + continue; } diff --git a/src/Commands/HandlersReset.php b/src/Commands/HandlersReset.php index ca99de5..b1d8f0d 100644 --- a/src/Commands/HandlersReset.php +++ b/src/Commands/HandlersReset.php @@ -1,4 +1,6 @@ -getConfig()->autoDiscover)) { CLI::write('No paths are set for automatic discovery. See the config file for Tatter\Handlers.', 'yellow'); + return; } diff --git a/src/Config/Handlers.php b/src/Config/Handlers.php index 5249cab..75f6454 100644 --- a/src/Config/Handlers.php +++ b/src/Config/Handlers.php @@ -1,4 +1,6 @@ -path = $path; $this->config = $config ?? config('Handlers'); @@ -58,8 +56,6 @@ public function __construct(string $path = '', HandlersConfig $config = null, Ca /** * Returns the current configuration. - * - * @return HandlersConfig */ public function getConfig(): HandlersConfig { @@ -68,8 +64,6 @@ public function getConfig(): HandlersConfig /** * Returns the search path. - * - * @return string */ public function getPath(): string { @@ -79,8 +73,6 @@ public function getPath(): string /** * Sets the search path and resets discovery. * - * @param string $path - * * @return $this */ public function setPath(string $path): self @@ -96,10 +88,6 @@ public function setPath(string $path): self /** * Returns the attributes for a discovered class. - * - * @param string $class - * - * @return array|null */ public function getAttributes(string $class): ?array { @@ -156,12 +144,13 @@ public function reset(): self * Returns the first matched class. Short-circuits the namespace * traversal to optimize performance. * - * @return string|null The full class name, or null if none found + * @return string|null The full class name, or null if none found */ public function first(): ?string { $class = $this->filterHandlers()[0] ?? null; $this->reset(); + return $class; } @@ -174,6 +163,7 @@ public function findAll(): array { $classes = $this->filterHandlers(); $this->reset(); + return $classes; } @@ -183,7 +173,7 @@ public function findAll(): array * * @param string $name The name of the handler * - * @return string|null The full class name, or null if none found + * @return string|null The full class name, or null if none found */ public function find(string $name): ?string { @@ -227,7 +217,8 @@ public function find(string $name): ?string /** * Returns an array of all matched classes. * - * @return array + * @return array + * * @deprecated Use findAll() */ public function all(): array @@ -241,7 +232,8 @@ public function all(): array * * @param string $name The name of the handler * - * @return string|null The full class name, or null if none found + * @return string|null The full class name, or null if none found + * * @deprecated Use find() */ public function named(string $name): ?string @@ -254,8 +246,8 @@ public function named(string $name): ?string /** * Parses "where" $criteria and adds to $filters * - * @param array $criteria Array of 'key [operator]' => 'value' - * @param boolean $combine Whether the resulting filter should be combined with others + * @param array $criteria Array of 'key [operator]' => 'value' + * @param bool $combine Whether the resulting filter should be combined with others */ protected function parseCriteria(array $criteria, bool $combine): void { @@ -265,10 +257,9 @@ protected function parseCriteria(array $criteria, bool $combine): void $key = trim($key); if (strpos($key, ' ')) { - list($key, $operator) = explode(' ', $key); + [$key, $operator] = explode(' ', $key); } - else - { + else { $operator = '=='; } @@ -284,7 +275,7 @@ protected function parseCriteria(array $criteria, bool $combine): void /** * Iterates through discovered handlers and attempts to register them. * - * @return array Array of newly-registered classes + * @return array Array of newly-registered classes */ public function register(): array { @@ -298,6 +289,7 @@ public function register(): array service('timer')->start('Register ' . $this->path); $classes = []; + foreach ($this->discovered as $class => $attributes) { $instance = new $class(); @@ -318,12 +310,13 @@ public function register(): array /** * Filters discovered classes by the defined criteria. * - * @param integer|null $limit Limit on how many classes to match + * @param int|null $limit Limit on how many classes to match * - * @return array * @throws \RuntimeException + * + * @return array */ - protected function filterHandlers(int $limit = null): array + protected function filterHandlers(?int $limit = null): array { $this->discoverHandlers(); @@ -336,6 +329,7 @@ protected function filterHandlers(int $limit = null): array } $classes = []; + foreach ($this->discovered as $class => $attributes) { $result = true; @@ -344,11 +338,12 @@ protected function filterHandlers(int $limit = null): array foreach ($this->filters as $filter) { // Split out the array to make it easier to read - list($key, $operator, $value, $combine) = $filter; + [$key, $operator, $value, $combine] = $filter; if (! isset($attributes[$key])) { $result = false; + continue; } @@ -356,7 +351,7 @@ protected function filterHandlers(int $limit = null): array { case '==': case '=': - $test = $attributes[$key] == $value; + $test = $attributes[$key] === $value; break; case '===': @@ -381,7 +376,7 @@ protected function filterHandlers(int $limit = null): array // Assumes the attribute is a CSV case 'has': - $test = in_array($value, explode(',', $attributes[$key])); + $test = in_array($value, explode(',', $attributes[$key]), true); break; default: @@ -440,6 +435,7 @@ protected function discoverHandlers(): self // Scan each namespace $this->discovered = []; + foreach (Services::autoloader()->getNamespace() as $namespace => $paths) { // Check for files in $this->path for this namespace @@ -452,7 +448,7 @@ protected function discoverHandlers(): self } // Make sure it is not an ignored class - if (in_array($class, $this->config->ignoredClasses)) + if (in_array($class, $this->config->ignoredClasses, true)) { continue; } @@ -479,7 +475,7 @@ protected function discoverHandlers(): self * @param string $file Full path to the file in question * @param string $namespace The file's namespace * - * @return string|null The fully-namespaced class + * @return string|null The fully-namespaced class */ public function getHandlerClass(string $file, string $namespace): ?string { @@ -490,8 +486,7 @@ public function getHandlerClass(string $file, string $namespace): ?string } // Try to load the file - try - { + try { include_once $file; } catch (\Throwable $e) @@ -514,7 +509,7 @@ public function getHandlerClass(string $file, string $namespace): ?string return null; } - if (! in_array(HandlerInterface::class, $interfaces)) + if (! in_array(HandlerInterface::class, $interfaces, true)) { return null; } @@ -526,8 +521,6 @@ public function getHandlerClass(string $file, string $namespace): ?string /** * Returns a standardized caching key for the current path. - * - * @return string */ protected function cacheKey(): string { diff --git a/src/Helpers/handlers_helper.php b/src/Helpers/handlers_helper.php index 2418dd1..924f715 100644 --- a/src/Helpers/handlers_helper.php +++ b/src/Helpers/handlers_helper.php @@ -7,10 +7,6 @@ { /** * Returns the Handlers service set to the specified path. - * - * @param string $path - * - * @return Handlers */ function handlers(string $path = ''): Handlers { diff --git a/src/Interfaces/HandlerInterface.php b/src/Interfaces/HandlerInterface.php index bfb2ecf..2168e00 100644 --- a/src/Interfaces/HandlerInterface.php +++ b/src/Interfaces/HandlerInterface.php @@ -1,4 +1,6 @@ - 'Error loading file {0}: {1}', - 'missingClass' => 'Could not locate {1} in {0}', - 'invalidFormat' => 'Required properties missing from class: {0}', + 'loadFail' => 'Error loading file {0}: {1}', + 'missingClass' => 'Could not locate {1} in {0}', + 'invalidFormat' => 'Required properties missing from class: {0}', ]; diff --git a/src/Traits/HandlerTrait.php b/src/Traits/HandlerTrait.php index 11bcf44..0015df5 100644 --- a/src/Traits/HandlerTrait.php +++ b/src/Traits/HandlerTrait.php @@ -1,12 +1,12 @@ - 'bar', ]; }; - $this->assertEquals('bar', $handler->foo); + $this->assertSame('bar', $handler->foo); + } + + public function testIsset() + { + $handler = new class () extends BaseHandler { + protected $attributes = [ + 'fruit' => 'banana', + ]; + }; + + $this->assertTrue(isset($handler->fruit)); + $this->assertFalse(isset($handler->meat)); } } diff --git a/tests/unit/CacheTest.php b/tests/unit/CacheTest.php index 8119e4d..e578918 100644 --- a/tests/unit/CacheTest.php +++ b/tests/unit/CacheTest.php @@ -3,7 +3,10 @@ use Tatter\Handlers\Handlers; use Tests\Support\HandlerTestCase; -class CacheTest extends HandlerTestCase +/** + * @internal + */ +final class CacheTest extends HandlerTestCase { public function testDiscoveryUsesCache() { @@ -15,7 +18,7 @@ public function testDiscoveryUsesCache() $result = $this->handlers->first(); - $this->assertEquals($class, $result); + $this->assertSame($class, $result); } public function testDiscoveryIgnoresCache() @@ -31,6 +34,6 @@ public function testDiscoveryIgnoresCache() $result = $this->handlers->first(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } } diff --git a/tests/unit/CommandTest.php b/tests/unit/CommandTest.php index 94280c7..4c3f4a6 100644 --- a/tests/unit/CommandTest.php +++ b/tests/unit/CommandTest.php @@ -6,8 +6,10 @@ /** * @see https://github.com/codeigniter4/CodeIgniter4/blob/develop/tests/system/Commands/HelpCommandTest.php + * + * @internal */ -class CommandTest extends HandlerTestCase +final class CommandTest extends HandlerTestCase { private $streamFilter; diff --git a/tests/unit/HelperTest.php b/tests/unit/HelperTest.php index 3973770..e0438e2 100644 --- a/tests/unit/HelperTest.php +++ b/tests/unit/HelperTest.php @@ -3,7 +3,10 @@ use Tatter\Handlers\Handlers; use Tests\Support\HandlerTestCase; -class HelperTest extends HandlerTestCase +/** + * @internal + */ +final class HelperTest extends HandlerTestCase { protected function setUp(): void { @@ -26,6 +29,6 @@ public function testHelperRetainsPath() $result = handlers()->getPath(); - $this->assertEquals($path, $result); + $this->assertSame($path, $result); } } diff --git a/tests/unit/LibraryTest.php b/tests/unit/LibraryTest.php index c0a927b..513ba33 100644 --- a/tests/unit/LibraryTest.php +++ b/tests/unit/LibraryTest.php @@ -1,25 +1,27 @@ handlers->getConfig(); $this->assertInstanceOf(HandlersConfig::class, $result); - $this->assertEquals(MINUTE, $result->cacheDuration); + $this->assertSame(MINUTE, $result->cacheDuration); } public function testGetPathReturnsPath() { $result = $this->handlers->getPath(); - $this->assertEquals('Factories', $result); + $this->assertSame('Factories', $result); } public function testSetPathChangesPath() @@ -27,7 +29,7 @@ public function testSetPathChangesPath() $path = 'Daiquiris'; $result = $this->handlers->setPath($path)->getPath(); - $this->assertEquals($path, $result); + $this->assertSame($path, $result); } public function testWhereCombinesFilters() @@ -35,14 +37,14 @@ public function testWhereCombinesFilters() $this->handlers->where(['group' => 'East']); $result = $this->getPrivateProperty($this->handlers, 'filters'); - $this->assertEquals($result, [ + $this->assertSame($result, [ ['group', '==', 'East', true], ]); $this->handlers->where(['uid' => 'pop']); $result = $this->getPrivateProperty($this->handlers, 'filters'); - $this->assertEquals($result, [ + $this->assertSame($result, [ ['group', '==', 'East', true], ['uid', '==', 'pop', true], ]); @@ -54,7 +56,7 @@ public function testResetClearsFilters() $this->handlers->reset(); $result = $this->getPrivateProperty($this->handlers, 'filters'); - $this->assertEquals($result, []); + $this->assertSame($result, []); } public function testGetHandlerClassReturnsClass() @@ -64,7 +66,7 @@ public function testGetHandlerClassReturnsClass() $file = realpath(SUPPORTPATH . 'Factories/WidgetFactory.php'); $result = $this->handlers->getHandlerClass($file, 'Tests\Support'); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testGetHandlerClassFails() @@ -89,7 +91,7 @@ public function testRegisterCallsHandlerRegister() { $this->handlers->register(); - $this->assertEquals(true, session('didRegister')); + $this->assertTrue(session('didRegister')); } public function testGetAttributesReturnsAttributes() @@ -97,7 +99,7 @@ public function testGetAttributesReturnsAttributes() $result = $this->handlers->getAttributes(WidgetFactory::class); $this->assertIsArray($result); - $this->assertEquals('Widget Plant', $result['name']); + $this->assertSame('Widget Plant', $result['name']); } public function testGetAttributesReturnsNull() diff --git a/tests/unit/SearchTest.php b/tests/unit/SearchTest.php index 27b029a..2604aa3 100644 --- a/tests/unit/SearchTest.php +++ b/tests/unit/SearchTest.php @@ -1,21 +1,21 @@ handlers - ->where(['uid' => 'widget']) - ->findAll(); + ->where(['uid' => 'widget']) + ->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testWhereUsesOperators() @@ -23,10 +23,10 @@ public function testWhereUsesOperators() $expected = ['Tests\Support\Factories\WidgetFactory']; $result = $this->handlers - ->where(['cost >' => 5]) - ->findAll(); + ->where(['cost >' => 5]) + ->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testWhereSupportsCsv() @@ -34,19 +34,19 @@ public function testWhereSupportsCsv() $expected = ['Tests\Support\Factories\WidgetFactory']; $result = $this->handlers - ->where(['list has' => 'three']) - ->findAll(); + ->where(['list has' => 'three']) + ->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testWhereMissingAttribute() { $result = $this->handlers - ->where(['foo' => 'bar']) - ->findAll(); + ->where(['foo' => 'bar']) + ->findAll(); - $this->assertEquals([], $result); + $this->assertSame([], $result); } public function testOrWhereIgnoresOtherFilters() @@ -54,11 +54,11 @@ public function testOrWhereIgnoresOtherFilters() $expected = ['Tests\Support\Factories\WidgetFactory']; $result = $this->handlers - ->where(['foo' => 'bar']) - ->orWhere(['uid' => 'widget']) - ->findAll(); + ->where(['foo' => 'bar']) + ->orWhere(['uid' => 'widget']) + ->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } //-------------------------------------------------------------------- @@ -72,7 +72,7 @@ public function testFindAllDiscoversAll() $result = $this->handlers->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testFindAllRespectsFilters() @@ -83,7 +83,7 @@ public function testFindAllRespectsFilters() $result = $this->handlers->where(['uid' => 'widget'])->findAll(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testFindAllResetsFilters() @@ -92,7 +92,7 @@ public function testFindAllResetsFilters() $result = $this->getPrivateProperty($this->handlers, 'filters'); - $this->assertEquals([], $result); + $this->assertSame([], $result); } public function testFirstReturnsSingleton() @@ -101,7 +101,7 @@ public function testFirstReturnsSingleton() $result = $this->handlers->first(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testFirstRespectsFilters() @@ -110,7 +110,7 @@ public function testFirstRespectsFilters() $result = $this->handlers->where(['uid' => 'widget'])->first(); - $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); } public function testFirstResetsFilters() @@ -119,19 +119,22 @@ public function testFirstResetsFilters() $result = $this->getPrivateProperty($this->handlers, 'filters'); - $this->assertEquals([], $result); + $this->assertSame([], $result); } //-------------------------------------------------------------------- /** * @dataProvider provideNames + * + * @param mixed $name + * @param mixed $success */ public function testFindFindsMatch($name, $success) { $result = $this->handlers->find($name); - $this->assertEquals($success, (bool) $result); + $this->assertSame($success, (bool) $result); } public function provideNames() diff --git a/tests/unit/ServiceTest.php b/tests/unit/ServiceTest.php index cca5d19..11f7633 100644 --- a/tests/unit/ServiceTest.php +++ b/tests/unit/ServiceTest.php @@ -3,14 +3,17 @@ use Config\Services; use Tests\Support\HandlerTestCase; -class ServiceTest extends HandlerTestCase +/** + * @internal + */ +final class ServiceTest extends HandlerTestCase { public function testReturnsExpectedPath() { $foos = Services::handlers('foo'); $bars = Services::handlers('bar'); - $this->assertEquals('foo', $foos->getPath()); - $this->assertEquals('bar', $bars->getPath()); + $this->assertSame('foo', $foos->getPath()); + $this->assertSame('bar', $bars->getPath()); } }