Comments (7)
If the getStore()
method already param type hinted, it will converted with typeDeclarations
set, see https://getrector.com/demo/7ae2d2a6-2697-4c30-b04b-54ed12d824c7
from rector.
@samsonasik Not sure how to adjust the piece of code:
RectorConfig::configure()
// A. whole set
->withPreparedSets(typeDeclarations: true)
// B. or few rules
->withRules([
TypedPropertyFromAssignsRector::class
]);
in my rector.php
file:
<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/app/code',
]);
# register rules
$rectorConfig->rules([
\Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector::class,
\Rector\Php74\Rector\Property\TypedPropertyRector::class,
\Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector::class,
\Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector::class,
ReturnTypeFromStrictNativeCallRector::class,
ReturnTypeFromStrictScalarReturnExprRector::class,
\Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector::class,
]);
$rectorConfig->skip([
\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class
]);
#define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81
]);
};
from rector.
You can add type declaration with sets()
if you use old config, eg:
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
+ \Rector\Set\ValueObject\SetList::TYPE_DECLARATION
]);
from rector.
Hi @samsonasik, it's not working.
FYI, I am using Rector v0.14.8
Also, this refactoring is not working:
<?php
class Handler
{
/**
* @var string
*/
protected $fileName = '/var/log/filename.log';
/**
* @var int
*/
protected $loggerType = \Monolog\Logger::INFO;
}
to
<?php
class Handler
{
protected string $fileName = '/var/log/filename.log';
protected int $loggerType = \Monolog\Logger::INFO;
}
from rector.
Update protected property may cause BC break on child classes, that's why it disabled by default, you can either make it private first:
https://getrector.com/demo/1a5cc52e-b860-434e-b4d2-805f57df040e
of use TypedPropertyFromAssignsRector
configurable inline public:
https://getrector.com/demo/a55f7384-e2b1-412e-9038-650537961e4f
https://getrector.com/demo/22c90383-a98f-459d-bc34-631429aa17c4
with beware: it may cause bc break. You can learn configurable example on the rule itself
from rector.
@samsonasik
Still, it's not working for me.
I have this class file & rector file:
test.php
:
<?php
class MyClass
{
/**
* @var MyOtherClass
*/
private $myOtherClass;
public function __construct(
MyOtherClass $myOtherClass
) {
$this->myOtherClass = $myOtherClass;
}
/**
* @param int|null $storeId
*/
public function getBaseUrl($storeId = null): string
{
return $this->myOtherClass->getStore($storeId)->getBaseUrl();
}
}
And rector.php
:
<?php
declare(strict_types=1);
use Rector\Set\ValueObject\SetList;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/app/code',
]);
# Register rules
$rectorConfig->rules([
\Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector::class,
\Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector::class,
\Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector::class,
\Rector\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector::class,
\Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector::class,
\Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector::class,
\Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector::class,
\Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector::class,
\Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector::class,
\Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector::class,
\Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector::class,
\Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector::class,
\Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector::class,
\Rector\Php54\Rector\Array_\LongArrayToShortArrayRector::class,
\Rector\Php71\Rector\List_\ListToArrayDestructRector::class,
\Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector::class,
\Rector\Php74\Rector\Property\TypedPropertyRector::class,
\Rector\Php80\Rector\FunctionLike\UnionTypesRector::class,
\Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector::class,
\Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector::class,
\Rector\Php80\Rector\Class_\StringableForToStringRector::class,
\Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector::class,
\Rector\Php80\Rector\FunctionLike\MixedTypeRector::class,
\Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector::class,
\Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector::class,
]);
$rectorConfig->skip([
\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class,
\Rector\Php81\Rector\Property\ReadOnlyPropertyRector::class,
\Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector::class,
\Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector::class,
\Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector::class,
\Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector::class,
]);
# define rules with custom configuration
/*$rectorConfig->ruleWithConfiguration(\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::class, [
'json_encode' => 'jsonEncode',
'json_decode' => 'jsonDecode',
]);*/
# define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::PHP_81,
SetList::DEAD_CODE,
SetList::NAMING,
SetList::TYPE_DECLARATION,
SetList::TYPE_DECLARATION_STRICT,
SetList::EARLY_RETURN,
SetList::PRIVATIZATION,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
]);
};
from rector.
You may have the target getStore()
method on MyOtherClass
not typed hint, eg: by docblock, we mostly don't convert docblock to type hint as by assumption.
When you want to be typed hinted, the MyOtherClass::getStore()
need to have param type hint in the first place so the caller must be typed hint as well.
see again my original example https://getrector.com/demo/7ae2d2a6-2697-4c30-b04b-54ed12d824c7
from rector.
Related Issues (20)
- New rule proposal: incorrect use of parenthesescan can cause unexpected behavior in code HOT 1
- Some public const enum values `types` are added on php 8.3 but on some it doesn't work. HOT 2
- Incorrect behavior of AssertRegExpRector HOT 1
- FirstClassCallableRector should also change simple function names
- Incorrect behavior of DowngradeThrowExprRector HOT 6
- Incorrect behavior of RemoveUnusedVariableAssignRector
- Incorrect behavior of NewInInitializerRector
- @param mixed type removed although there is a comment HOT 1
- [ERROR] Could not process "vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/Lib/V16/InfoRedactor.php" file, due HOT 9
- Explore way Rector or another tool can process blade templates
- [DX] Allow more granulated return type completion, to separate from closures and makes easier to review
- v1.1.0 is running 8x slower than v1.0.4 HOT 3
- Incorrect behavior of RemoveUnusedPublicMethodParameterRector HOT 11
- BC Break - Class removed in patch version HOT 1
- Could not process "/home/pdavie/src2/html/includes/HTML/QuickForm/Rule/Compare.php" HOT 5
- [DX] Add rename attribute configurable rule HOT 1
- StringableForToStringRector adding cast to incorrect place
- ForeachToInArrayRector changing code that cannot be changed to in_array approach
- ThrowWithPreviousExceptionRector not using named arguments if they are already used
- LocallyCalledStaticMethodToNonStaticRector does not skip when doing static call using class name
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rector.