2019-12-29 16:08:26 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\CodeQuality\Rector\Include_ ;
2019-12-29 16:08:26 +00:00
2024-04-01 16:51:34 +00:00
use RectorPrefix202404\Nette\Utils\Strings ;
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\BinaryOp\Concat ;
use PhpParser\Node\Expr\Include_ ;
use PhpParser\Node\Scalar\MagicConst\Dir ;
use PhpParser\Node\Scalar\String_ ;
2024-01-02 02:40:38 +00:00
use Rector\PhpParser\Node\Value\ValueResolver ;
use Rector\Rector\AbstractRector ;
2022-06-07 09:18:30 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-12-29 16:08:26 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// github . com / symplify / CodingStandard #includerequire-should-be-followed-by-absolute-path
2019-12-29 16:08:26 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\AbsolutizeRequireAndIncludePathRectorTest
2019-12-29 16:08:26 +00:00
*/
2022-06-07 08:22:29 +00:00
final class AbsolutizeRequireAndIncludePathRector extends AbstractRector
2019-12-29 16:08:26 +00:00
{
2023-09-20 12:53:23 +00:00
/**
* @ readonly
2024-01-02 02:40:38 +00:00
* @ var \Rector\PhpParser\Node\Value\ValueResolver
2023-09-20 12:53:23 +00:00
*/
private $valueResolver ;
public function __construct ( ValueResolver $valueResolver )
{
$this -> valueResolver = $valueResolver ;
}
2022-06-07 08:22:29 +00:00
public function getRuleDefinition () : RuleDefinition
2019-12-29 16:08:26 +00:00
{
2022-06-07 08:22:29 +00:00
return new RuleDefinition ( 'include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require being changed depends on the current working directory.' , [ new CodeSample ( <<< 'CODE_SAMPLE'
2019-12-29 16:08:26 +00:00
class SomeClass
{
public function run ()
{
require 'autoload.php' ;
require $variable ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-12-29 16:08:26 +00:00
class SomeClass
{
public function run ()
{
require __DIR__ . '/autoload.php' ;
require $variable ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-12-29 16:08:26 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-12-29 16:08:26 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-12-29 16:08:26 +00:00
{
2022-06-07 08:22:29 +00:00
return [ Include_ :: class ];
2019-12-29 16:08:26 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param Include_ $node
2019-12-29 16:08:26 +00:00
*/
2022-06-07 08:22:29 +00:00
public function refactor ( Node $node ) : ? Node
2019-12-29 16:08:26 +00:00
{
2022-06-07 08:22:29 +00:00
if ( $node -> expr instanceof Concat && $node -> expr -> left instanceof String_ && $this -> isRefactorableStringPath ( $node -> expr -> left )) {
2022-04-10 20:11:02 +00:00
$node -> expr -> left = $this -> prefixWithDirConstant ( $node -> expr -> left );
return $node ;
}
2022-06-07 08:22:29 +00:00
if ( ! $node -> expr instanceof String_ ) {
2019-12-29 16:08:26 +00:00
return null ;
}
2022-04-10 20:11:02 +00:00
if ( ! $this -> isRefactorableStringPath ( $node -> expr )) {
return null ;
}
2019-12-29 16:08:26 +00:00
/** @var string $includeValue */
2021-01-30 23:20:05 +00:00
$includeValue = $this -> valueResolver -> getValue ( $node -> expr );
2021-03-15 13:19:57 +00:00
// skip phar
2021-05-30 10:12:56 +00:00
if ( \strncmp ( $includeValue , 'phar://' , \strlen ( 'phar://' )) === 0 ) {
2021-03-15 13:19:57 +00:00
return null ;
}
// skip absolute paths
2021-05-30 10:12:56 +00:00
if ( \strncmp ( $includeValue , '/' , \strlen ( '/' )) === 0 ) {
2019-12-29 16:08:26 +00:00
return null ;
}
2022-04-07 20:59:55 +00:00
if ( \strpos ( $includeValue , 'config/' ) !== \false ) {
return null ;
}
2019-12-29 16:08:26 +00:00
// add preslash to string
2023-09-20 12:53:23 +00:00
$node -> expr -> value = \strncmp ( $includeValue , './' , \strlen ( './' )) === 0 ? Strings :: substring ( $includeValue , 1 ) : '/' . $includeValue ;
2022-04-10 20:11:02 +00:00
$node -> expr = $this -> prefixWithDirConstant ( $node -> expr );
2019-12-29 16:08:26 +00:00
return $node ;
}
2022-06-07 08:22:29 +00:00
private function isRefactorableStringPath ( String_ $string ) : bool
2022-04-10 20:11:02 +00:00
{
return \strncmp ( $string -> value , 'phar://' , \strlen ( 'phar://' )) !== 0 ;
}
2022-06-07 08:22:29 +00:00
private function prefixWithDirConstant ( String_ $string ) : Concat
2022-04-10 20:11:02 +00:00
{
$this -> removeExtraDotSlash ( $string );
$this -> prependSlashIfMissing ( $string );
2022-06-07 08:22:29 +00:00
return new Concat ( new Dir (), $string );
2022-04-10 20:11:02 +00:00
}
/**
* Remove " ./ " which would break the path
*/
2022-06-07 08:22:29 +00:00
private function removeExtraDotSlash ( String_ $string ) : void
2022-04-10 20:11:02 +00:00
{
if ( \strncmp ( $string -> value , './' , \strlen ( './' )) !== 0 ) {
return ;
}
2022-06-07 08:22:29 +00:00
$string -> value = Strings :: replace ( $string -> value , '#^\\.\\/#' , '/' );
2022-04-10 20:11:02 +00:00
}
2022-06-07 08:22:29 +00:00
private function prependSlashIfMissing ( String_ $string ) : void
2022-04-10 20:11:02 +00:00
{
if ( \strncmp ( $string -> value , '/' , \strlen ( '/' )) === 0 ) {
return ;
}
$string -> value = '/' . $string -> value ;
}
2019-12-29 16:08:26 +00:00
}