2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2019-09-22 18:57:03 +00:00
namespace Rector\Php73\Rector\ConstFetch ;
2018-10-12 05:03:37 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\ConstFetch ;
2021-04-13 16:01:31 +00:00
use PhpParser\Node\Name ;
2018-10-12 05:03:37 +00:00
use PhpParser\Node\Name\FullyQualified ;
2021-04-13 16:01:31 +00:00
use PHPStan\Reflection\ReflectionProvider ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-09-24 14:11:08 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
2021-04-13 16:01:31 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2021-09-24 14:11:08 +00:00
use Rector\VersionBonding\Contract\MinPhpVersionInterface ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2018-10-12 05:03:37 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// wiki . php . net / rfc / case_insensitive_constant_deprecation
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Php73\Rector\ConstFetch\SensitiveConstantNameRector\SensitiveConstantNameRectorTest
2018-10-12 05:03:37 +00:00
*/
2021-09-24 14:11:08 +00:00
final class SensitiveConstantNameRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
2018-10-12 05:03:37 +00:00
{
/**
* @ see http :// php . net / manual / en / reserved . constants . php
* @ var string []
*/
2021-05-09 20:15:43 +00:00
private const PHP_RESERVED_CONSTANTS = [ 'PHP_VERSION' , 'PHP_MAJOR_VERSION' , 'PHP_MINOR_VERSION' , 'PHP_RELEASE_VERSION' , 'PHP_VERSION_ID' , 'PHP_EXTRA_VERSION' , 'PHP_ZTS' , 'PHP_DEBUG' , 'PHP_MAXPATHLEN' , 'PHP_OS' , 'PHP_OS_FAMILY' , 'PHP_SAPI' , 'PHP_EOL' , 'PHP_INT_MAX' , 'PHP_INT_MIN' , 'PHP_INT_SIZE' , 'PHP_FLOAT_DIG' , 'PHP_FLOAT_EPSILON' , 'PHP_FLOAT_MIN' , 'PHP_FLOAT_MAX' , 'DEFAULT_INCLUDE_PATH' , 'PEAR_INSTALL_DIR' , 'PEAR_EXTENSION_DIR' , 'PHP_EXTENSION_DIR' , 'PHP_PREFIX' , 'PHP_BINDIR' , 'PHP_BINARY' , 'PHP_MANDIR' , 'PHP_LIBDIR' , 'PHP_DATADIR' , 'PHP_SYSCONFDIR' , 'PHP_LOCALSTATEDIR' , 'PHP_CONFIG_FILE_PATH' , 'PHP_CONFIG_FILE_SCAN_DIR' , 'PHP_SHLIB_SUFFIX' , 'PHP_FD_SETSIZE' , 'E_ERROR' , 'E_WARNING' , 'E_PARSE' , 'E_NOTICE' , 'E_CORE_ERROR' , 'E_CORE_WARNING' , 'E_COMPILE_ERROR' , 'E_COMPILE_WARNING' , 'E_USER_ERROR' , 'E_USER_WARNING' , 'E_USER_NOTICE' , 'E_RECOVERABLE_ERROR' , 'E_DEPRECATED' , 'E_USER_DEPRECATED' , 'E_ALL' , 'E_STRICT' , '__COMPILER_HALT_OFFSET__' , 'TRUE' , 'FALSE' , 'NULL' ];
2021-04-13 16:01:31 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-08-23 00:20:32 +00:00
* @ var \PHPStan\Reflection\ReflectionProvider
2021-04-13 16:01:31 +00:00
*/
private $reflectionProvider ;
2021-05-10 22:23:08 +00:00
public function __construct ( \PHPStan\Reflection\ReflectionProvider $reflectionProvider )
2021-04-13 16:01:31 +00:00
{
$this -> reflectionProvider = $reflectionProvider ;
}
2021-09-24 14:11:08 +00:00
public function provideMinPhpVersion () : int
{
return \Rector\Core\ValueObject\PhpVersionFeature :: DEPRECATE_INSENSITIVE_CONSTANT_NAME ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-12 05:03:37 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes case insensitive constants to sensitive ones.' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2018-10-12 05:03:37 +00:00
define ( 'FOO' , 42 , true );
var_dump ( FOO );
var_dump ( foo );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-10-12 05:03:37 +00:00
define ( 'FOO' , 42 , true );
var_dump ( FOO );
var_dump ( FOO );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2018-10-12 05:03:37 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-10-12 05:03:37 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-10-12 05:03:37 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\ConstFetch :: class ];
2018-10-12 05:03:37 +00:00
}
/**
2021-12-10 09:57:54 +00:00
* @ param \PhpParser\Node $node
2018-10-12 05:03:37 +00:00
*/
2021-12-10 09:57:54 +00:00
public function refactor ( $node ) : ? \PhpParser\Node
2018-10-12 05:03:37 +00:00
{
2019-02-21 11:05:20 +00:00
$constantName = $this -> getName ( $node );
if ( $constantName === null ) {
return null ;
}
2021-05-09 20:15:43 +00:00
$uppercasedConstantName = \strtoupper ( $constantName );
2018-10-12 05:03:37 +00:00
// is system constant?
2021-05-09 20:15:43 +00:00
if ( \in_array ( $uppercasedConstantName , self :: PHP_RESERVED_CONSTANTS , \true )) {
2018-10-21 10:19:14 +00:00
return null ;
2018-10-12 05:03:37 +00:00
}
2019-02-21 11:05:20 +00:00
// constant is defined in current lower/upper case
2021-05-10 22:23:08 +00:00
$scope = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
if ( $this -> reflectionProvider -> hasConstant ( new \PhpParser\Node\Name ( $constantName ), $scope )) {
2019-02-21 11:05:20 +00:00
return null ;
2019-02-21 00:03:08 +00:00
}
2018-10-12 05:03:37 +00:00
// is uppercase, all good
2019-02-21 11:05:20 +00:00
if ( $constantName === $uppercasedConstantName ) {
2018-10-21 10:19:14 +00:00
return null ;
2018-10-12 05:03:37 +00:00
}
2021-10-18 08:52:15 +00:00
if ( \strpos ( $uppercasedConstantName , '\\' ) !== \false ) {
return null ;
}
2021-05-10 22:23:08 +00:00
$node -> name = new \PhpParser\Node\Name\FullyQualified ( $uppercasedConstantName );
2018-10-15 04:36:58 +00:00
return $node ;
2018-10-12 05:03:37 +00:00
}
}