-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transform Fully Qualified parameters and return types to short version #3276
Transform Fully Qualified parameters and return types to short version #3276
Conversation
what about going even forward and having input like:
so even add imports when they are missing |
@@ -0,0 +1,219 @@ | |||
<?php | |||
|
|||
declare(strict_types=1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please drop it. we supports 5.6
Going forward doesnt seems like a good idea since I am changing more tokens into less tokens right? |
@keradus : Is it possible to partially mark a fixer for a specific PHP version? Strict return types are only available from PHP 7.0 on. Should I target PHP >= 7.0 or maybe it's better to split the fixer in 2 different fixers. how do you see this? |
@keradus I think it would raise a lot the complexity, since the fixer would also need to check already imported class to avoid conflicts, like: use Foo\Bar;
var_dump(\Other\Bar::class); @veewee I suggest to try to fix also:
use Foo\Bar;
// From
var_dump(\Foo\Bar\Baz::class);
// To
var_dump(Bar\Baz::class);
namespace Foo\Bar;
// From
var_dump(\Foo\Bar\Baz::class);
// To
var_dump(Baz::class); |
very well, let us keep moving FQCN into imports as separated fixer (so feature request that would not be solved in your PR) |
15fcf17
to
6b4d736
Compare
@Slamdunk : implemented the partial use + namespaces |
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return PHP_VERSION_ID >= 70000 && $tokens->isTokenKindFound(T_FUNCTION); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't fix params on 5.x ? definition says it shall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See question above. I changed the code to make it as PHP 5.6 compatible for parameters only.
{ | ||
if (PHP_VERSION_ID < 70000) { | ||
$this->markTestSkipped('The strict return type : operator is only avaiable from PHP 7.0.'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using the dedicated method DocBlock
/**
* @requires PHP 7.0
*/
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -548,6 +548,10 @@ Choose from the list of available rules: | |||
PHP code must use the long ``<?php`` tags or short-echo ``<?=`` tags and not | |||
other tag variations. | |||
|
|||
* **fully_qualified_strict_types** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add it to local .php_cs.dist
file
'name_index' => 3, | ||
'type' => '', | ||
'type_index_start' => -1, | ||
'type_index_end' => -1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 is invalid index
'default' => 'array(1,2)', | ||
'name' => '$b', | ||
'name_index' => 6, | ||
'type' => '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use null instead of empty string.
* @param int $argumentStart | ||
* @param int $argumentEnd | ||
* | ||
* @return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this array looks complicated. I must admit that looking on implementation I have no idea what will be the output - like is name
obligatory, or could be empty. when i will get -1 instead of proper index.
probably new DTO object would be good.
/** | ||
* @internal | ||
*/ | ||
class NamespaceUsesAnalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not final ?
/** | ||
* @param string $shortType | ||
* | ||
* @return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Token[]
perhaps ?
*/ | ||
final class FullyQualifiedStrictTypesFixerTest extends AbstractFixerTestCase | ||
{ | ||
public function testImportedStrictTypesFixWithoutReturn() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use single test method with data provider.
info in names is nice indeed, yet you could as well put that info in data provider
* @param string $code | ||
* @param int $methodIndex | ||
* @param array $expected | ||
* @dataProvider provideFunctionsWithArgumentsCases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please separate params form data provider with empty line
code in PR is nice, yet reviewing it requires some time. I wonder, maybe you could extract sth from this PR to separated one (like extracting refactoring of analyzers) ? |
5902ad5
to
74fe243
Compare
Hi @keradus, I've applied the remarks. I would prefer to keep both the fixer and the analyzers as one PR. Otherwise it would take me too much time to split them up and bring them together afterwards again. Take all the time you need to review :) (BTW: not sure why circle fails. You might want to add more memory?) |
OK, thanks for update. Please be aware that I will rather not be able to do it this week. Sorry for that :( (CircleCI: we faced the limit when we recently added new tests on 2.2, CirlceCI still works, merged 2.2->master, too much tests, CircleCI crashed. If you are willing to adjust memory, please raise new PR, thanks !) |
74fe243
to
1e82d05
Compare
@keradus : I don't understand why the test is failing. It worked before rebasing against master :) |
Hi @veewee, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@veewee I'm very hapy to see your work. Lot of inspiration for my code 👍
'int', | ||
'iteratable', | ||
'float', | ||
'string', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So beautiful :)
I'd also add mixed
and object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://php.net/manual/pl/function.is-scalar.php
scalar in PHP has a meaning already, reusing scalar word with different definition what is a scalar type actually gonna be super misleading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_scalar()
works only on variables, not strings
This is rather to exclude PHP-reserved types to avoid potential bugs and improve reuse of such class.
I got a fixer that changes varaible name to it's type.
And without mixed
/object
as scalar (or PHP-reserved types) it made mess like:
/**
* @var mixed
*/
-private $variable;
+private $mixed;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not saying anything about calling the is_scalar
.
i'm saying that scalar term already has a meaning in PHP as pseudotype, and it doesn't cover objects. reusing the term but with different meaning is super misleading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What term would you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change used term to some non-established in php internals and document what it is.
for now, i must admit that i have no clue what is this info suppose to be.
reverse of collection? but then why not resource
? no clue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@veewee Nice, thanks for the references in comments!
@keradus : I would love to finish this one up. What's the state of this PR from your point of view? |
LGTM. What needs to be done here? |
@veewee , I see amazing job of yours here and I do appreciate it fully ! It looks really great, and I know this fixer was highly awaited by community! @TomasVotruba , thanks for big help with reviewing the PR, it's highly appreciated as well! |
Finally, merging :D |
…o short version (veewee, keradus) This PR was squashed before being merged into the 2.11-dev branch (closes #3276). Discussion ---------- Transform Fully Qualified parameters and return types to short version This PR removes fully qualified parameters and return types to the short versions when they are being imported / in de same namespace: Result: ```php <?php use Foo\Bar; use Foo\Bar\Baz; class SomeClass { public function doSomething(Bar $foo): Baz { } } ``` Source: ```php <?php use Foo\Bar; use Foo\Bar\Baz; class SomeClass { public function doSomething(\Foo\Bar $foo): \Foo\Bar\Baz { } } ``` In older codebases (early namespaces), this was frequently used. Besides that, it can be used to fix autogenerated code from zend-code (zendframework/zend-code#76) and crappy docblocks in the #3110 PR I am working on: Commits ------- d01075c Transform Fully Qualified parameters and return types to short version
Thanks for all the feedback! Really appreciate it! |
This PR was squashed before being merged into the 2.12-dev branch (closes #3640). Discussion ---------- Add PhpdocToReturnTypeFixer ```diff /** * @return \My\Bar */ -function my_foo() +function my_foo(): \My\Bar {} ``` - [x] Mark as *risky* - [x] Require ^7.0 - [x] Fix ^7.1 [*void return*, *iterable*] - [x] Fix ^7.2 [*object type*] - [x] Fix ^7.1 [*nullable types*] - [x] Config to fix scalar types, default to **false** because of unexpected results if *stryct_types* are enabled or not - [x] `@return static` => `: self` - [x] ~Config to overwrite existing mismatching types: PHPDoc wins~ can't do this, because `FunctionNotation\ReturnTypeDeclarationFixer` changes the output of this fixer - [x] Ignore mixed types - [x] Ignore [generics](https://wiki.php.net/rfc/generics) and array of types - [x] Run before `FunctionNotation\ReturnTypeDeclarationFixer` #2207 - [x] Run before `Import\FullyQualifiedStrictTypesFixer` #3276 - [x] Run ~before~ integration test with `FunctionNotation\VoidReturnFixer` #2740 - [x] Run after `Phpdoc\PhpdocScalarFixer` Suggestions for the name of the fixer are welcome. Commits ------- d7a409c Add PhpdocToReturnTypeFixer
This PR removes fully qualified parameters and return types to the short versions when they are being imported / in de same namespace:
Result:
Source:
In older codebases (early namespaces), this was frequently used. Besides that, it can be used to fix autogenerated code from zend-code (zendframework/zend-code#76) and crappy docblocks in the #3110 PR I am working on: