entropyphp / utils
Utility classes for PHP
v0.1.0
2025-07-29 10:11 UTC
Requires
- php: ^8.2
- koriym/attributes: ^1.0
Requires (Dev)
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: >=3.13
- symfony/var-dumper: ^v7.0
README
Utility classes for PHP. A collection of PHP utility classes for file operations, attribute handling, and PHP code parsing.
Requirements
- PHP 8.2 or higher
- Composer
Installation
Install via Composer:
composer require entropyphp/utils
Components
AttributeLoader
A utility class for working with PHP 8 attributes (annotations). It provides methods to retrieve attributes from classes and methods.
Usage
use Koriym\Attributes\AttributeReader; use Pg\Utils\Attribute\AttributeLoader; use ReflectionClass; use ReflectionMethod; // Initialize the AttributeLoader $attributeLoader = new AttributeLoader(new AttributeReader()); // Get a single attribute from a method $reflectionMethod = new ReflectionMethod(MyClass::class, 'myMethod'); $attribute = $attributeLoader->getMethodAttribute($reflectionMethod, MyAttribute::class); // Get multiple attributes from a method $attributes = $attributeLoader->getMethodAttributes($reflectionMethod, MyAttribute::class); foreach ($attributes as $attribute) { // Process each attribute } // Get a single attribute from a class $reflectionClass = new ReflectionClass(MyClass::class); $attribute = $attributeLoader->getClassAttribute($reflectionClass, MyAttribute::class); // Get multiple attributes from a class $attributes = $attributeLoader->getClassAttributes($reflectionClass, MyAttribute::class); foreach ($attributes as $attribute) { // Process each attribute } // Access the underlying AttributeReader $reader = $attributeLoader->getReader();
FileUtils
A utility class for file system operations.
Usage
use Pg\Utils\File\FileUtils; // Get all PHP files in a directory (recursively) $files = FileUtils::getFiles('/path/to/directory', 'php'); foreach ($files as $file) { echo $file->getPathname() . PHP_EOL; } // Get all PHP files excluding those containing 'test' in the filename $files = FileUtils::getFiles('/path/to/directory', 'php', 'test'); // Find the project root directory (where composer.json is located) $projectDir = FileUtils::getProjectDir(__DIR__); // Get the root path of the installed package $rootPath = FileUtils::getRootPath();
PhpTokenParser
A utility class for parsing PHP code to extract class information.
Usage
use Pg\Utils\Parser\PhpTokenParser; // Get the fully qualified class name from PHP code $phpCode = file_get_contents('/path/to/file.php'); $className = PhpTokenParser::findClass($phpCode); if ($className) { echo "Found class: " . $className . PHP_EOL; } else { echo "No class found in the file." . PHP_EOL; }
Testing
Run the test suite:
composer run tests
Generate code coverage report:
composer run coverage
License
This project is licensed under the MIT License - see the LICENSE file for details.