sspat / reserved-words
Validates strings against reserved words of a specific php version
Installs: 14 988
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.3|^8.0
- thecodingmachine/safe: ^1.3|^2
Requires (Dev)
- doctrine/coding-standard: ^9.0
- infection/infection: ^0.18|^0.26.6
- phpstan/phpstan: ^1.4
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.1
- phpunit/phpunit: ^9.5.5
- squizlabs/php_codesniffer: ^3.6
- thecodingmachine/phpstan-safe-rule: ^1.2
- vimeo/psalm: ^4.20
This package is auto-updated.
Last update: 2024-12-11 16:04:29 UTC
README
About
This package allows checking strings for being a PHP reserved word and also the possibility of using the string as a PHP namespace/class/interface/trait, function, method or constant name.
By default checks are performed for your current runtime PHP version, but you can specify any version to check against.
This can come in handy during code generation for example, when PHP code is generated based on some user input.
Reserved words in the PHP documentation:
Installation
composer require sspat/reserved-words
Usage
<?php use sspat\ReservedWords\ReservedWords; $reservedWords = new ReservedWords(); $word = 'list'; /** * Checks that the word is reserved in any PHP version. * It is still possible to use reserved words in php code in many places, but generally you should avoid it. * This method also returns true for words that are marked as "soft" reserved in the PHP docs and may * become reserved in future versions of the language. */ $isReserved = $reservedWords->isReserved($word); /** * Checks that the word cannot be used as a constant name in your current php version. */ $cannotUseAsConstantName = $reservedWords->isReservedConstantName($word); /** * Checks that the word cannot be used as a namespace part in your current php version. * * This is used for checking parts of namespaces, not full namespace strings. * E.g. calling this with `Some\Namespace\String` is incorrect, you should make three separate calls * with `Some`, `Namespace` and `String`. */ $cannotUseAsNamespaceName = $reservedWords->isReservedNamespaceName($word); /** * Checks that the word cannot be used as a class/interface/trait name in your current php version. */ $cannotUseAsNamespaceName = $reservedWords->isReservedClassName($word); /** * Checks that the word cannot be used as a function name in your current php version. */ $cannotUseAsFunctionName = $reservedWords->isReservedFunctionName($word); /** * Checks that the word cannot be used as a method name in your current php version. */ $cannotUseAsMethodName = $reservedWords->isReservedMethodName($word); /** * The following methods also accept a second parameter, to check against a PHP version different than your current runtime */ $cannotUseAsConstantName = $reservedWords->isReservedConstantName($word, '5.6'); $cannotUseAsNamespaceName = $reservedWords->isReservedNamespaceName($word, '5.6.1'); $cannotUseAsNamespaceName = $reservedWords->isReservedClassName($word, '5.6.1'); $cannotUseAsFunctionName = $reservedWords->isReservedFunctionName($word, '8.0'); $cannotUseAsMethodName = $reservedWords->isReservedMethodName($word, '7.4.2');