Skip to content

Fix @return statements to use $this or static when relevant #21054

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

Merged
merged 1 commit into from
Dec 27, 2016

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Dec 26, 2016

Q A
Branch? 2.7
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #20290
License MIT
Doc PR n/a

see #20290

@ro0NL
Copy link
Contributor

ro0NL commented Dec 26, 2016

With #21047 i also spotted some cases where @return FQCN|$this is used. (FQCN from the current class file, ie. the same ;-))

I have no clear ref on this, but PhpStorm really doesnt seem to like it. Or at least not when used inconsistently so it seems 😕 (1 class mixing $this, FQCN and/or FQCN|$this[|...]).

Perhaps worth looking at.

@@ -42,7 +42,7 @@ public function __construct(Scope $parent = null)
/**
* Opens a new child scope.
*
* @return Scope
* @return $this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be @return self right?
this - return exactly the same instance
self - return new instance of exactly same type
static - return new instance of same type or any child of type

@fabpot fabpot force-pushed the return-stmt branch 6 times, most recently from 21c14d3 to 78f726a Compare December 26, 2016 09:47
@fabpot fabpot changed the base branch from master to 2.7 December 26, 2016 09:49
@fabpot fabpot force-pushed the return-stmt branch 2 times, most recently from c66a477 to 683eaf2 Compare December 26, 2016 10:46
@fabpot
Copy link
Member Author

fabpot commented Dec 26, 2016

I would appreciate some careful review before merging as re-reading the PR several times allowed me to fix some issues I introduced. I know, this is quite painful to do, but let's try to do it well.

@@ -38,7 +38,7 @@ public function __construct(NodeDefinition $node)
* @param string $key The key to remap
* @param string $plural The plural of the key in case of irregular plural
*
* @return NormalizationBuilder
* @return $this
*/
public function remap($key, $plural = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before() (below) can be $this|ExprBuilder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -57,7 +57,7 @@ public function import($resource, $type = null)
* @param mixed $resource A resource
* @param string|null $type The resource type or null if unknown
*
* @return LoaderInterface A LoaderInterface instance
* @return $this|self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be $this|LoaderInterface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -35,7 +35,7 @@ public function setHelperSet(HelperSet $helperSet = null)
/**
* Gets the helper set associated with this helper.
*
* @return HelperSet A HelperSet instance
* @return HelperSet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be HelperSet|null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -81,7 +81,7 @@ public function setDispatcher(EventDispatcherInterface $dispatcher)
/**
* Returns the EventDispatcher that dispatches this Event.
*
* @return EventDispatcherInterface
* @return EventDispatcher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventDispatcherInterface was right? The @var is wrong..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -27,7 +27,7 @@ class Expression implements ValueInterface
/**
* @param string $expr
*
* @return Expression
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -42,7 +42,7 @@ public function __construct(Scope $parent = null)
/**
* Opens a new child scope.
*
* @return Scope
* @return self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be inconsistent with a lot of changes below.. where it goes to static for new self.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If new self is used, it should be self anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

@@ -55,7 +55,7 @@ class Regex implements ValueInterface
/**
* @param string $expr
*
* @return Regex
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self. Im stopping here for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -52,7 +52,7 @@ public function enter()
/**
* Closes current scope and returns parent one.
*
* @return Scope|null
* @return self|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should prefer using self over the class/interface name. IMHO, we should only distinguish $this, static and classname usages, but self does not bring anything more, so it isn't worth it.

Copy link
Contributor

@ro0NL ro0NL Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should prefer using self over the class/interface name.

Personally, i prefer it... so while we're at it 👍 for doing this as well.

Copy link
Contributor

@ogizanagi ogizanagi Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, i prefer it...

I don't 😆

I just don't understand the motivations behind, and there are many places where this was not applied (FormFactoryBuilderInterface::getFormFactory() for instance).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid duplicating terms. But you're right, if not done consistently it's not really worth it anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep self as in the code, that's also what we are using.

@@ -93,7 +93,7 @@ public static function setStyleDefinition($name, TableStyle $style)
*
* @param string $name The style name
*
* @return TableStyle A TableStyle instance
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks wrong to me. self::$styles is an array of TableStyle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -27,7 +27,7 @@ class SyntaxErrorException extends ParseException
* @param string $expectedValue
* @param Token $foundToken
*
* @return SyntaxErrorException
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be self (same below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -38,7 +38,7 @@ public function add($child, $type = null, array $options = array());
* @param string|FormTypeInterface $type The type of the form or null if name is a property
* @param array $options The options
*
* @return FormBuilderInterface The created builder
* @return $this
Copy link
Contributor

@ogizanagi ogizanagi Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks wrong to me. It doesn't return the current instance, but creates another one, for a child for instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -47,7 +47,7 @@ public function create($name, $type = null, array $options = array());
*
* @param string $name The name of the child
*
* @return FormBuilderInterface The builder for the child
* @return $this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. It returns a child. Not the same instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -715,7 +715,7 @@ public function setInheritData($inheritData)
*
* @param bool $inheritData Whether the form should inherit its parent's data
*
* @return FormConfigBuilder The configuration object
* @return $this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't return anything actually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -349,7 +349,7 @@ public function getInheritData()
/**
* Alias of {@link getInheritData()}.
*
* @return FormConfigBuilder The configuration object
* @return $this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. It returns a boolean actually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed :)

Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm done 😅
Sorry, I've left a lot of comment related to what seems misleading usages of static to me (see #21054 (comment)), in order to keep track of it. If you don't agree, I'll remove them.

Anyway, what a colossal job you've achieved here!

@@ -36,7 +36,7 @@ public function setParent(FormInterface $parent = null);
/**
* Returns the parent form.
*
* @return FormInterface|null The parent form or null if there is none
* @return $this|null The parent form or null if there is none
Copy link
Contributor

@ogizanagi ogizanagi Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong change. The returned instance is the form parent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be self|null then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -89,7 +89,7 @@ public function remove($name);
/**
* Returns all children in this group.
*
* @return FormInterface[] An array of FormInterface instances
* @return FormInterface[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not self[] then? (Related to #21054 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -70,7 +70,7 @@
*
* @param Guess[] $guesses An array of guesses
*
* @return Guess|null The guess with the highest confidence
* @return static|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should keep Guess|null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or self|null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -48,7 +48,7 @@ public function __construct(array $items)
*
* @param string $headerValue
*
* @return AcceptHeader
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -57,7 +57,7 @@ public function __construct($value, array $attributes = array())
*
* @param string $itemValue
*
* @return AcceptHeaderItem
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -302,7 +302,7 @@ public function __construct($locale = 'en', $style = null, $pattern = null)
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation
*
* @return NumberFormatter
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -21,7 +21,7 @@
/**
* Creates a property accessor with the default configuration.
*
* @return PropertyAccessor The new property accessor
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks wrong. (same for other changes in this class)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -52,7 +52,7 @@ public function __construct($identifier, $type)
*
* @param object $domainObject
*
* @return ObjectIdentity
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -52,7 +52,7 @@ public function __construct($username, $class)
*
* @param UserInterface $user
*
* @return UserSecurityIdentity
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -64,7 +64,7 @@ public static function fromAccount(UserInterface $user)
*
* @param TokenInterface $token
*
* @return UserSecurityIdentity
* @return static
*/
public static function fromToken(TokenInterface $token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did just a quick review, these are the glitches I found.

@@ -61,7 +61,7 @@ public function __toString()
*
* @param Command|null $parent Parent command
*
* @return Command New Command instance
* @return static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -157,7 +157,7 @@ public function cmd($esc)
*
* @param string $label The unique label
*
* @return Command The current Command instance
* @return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be self|string, according to add().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -194,7 +194,7 @@ public function get($label)
/**
* Returns parent command (if any).
*
* @return Command Parent command
* @return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self|null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so as it throws an exception is parent is null.

@@ -178,7 +178,7 @@ public function ins($label)
*
* @param string $label
*
* @return Command The labeled command
* @return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self|string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -36,7 +36,7 @@ public function setParent(FormInterface $parent = null);
/**
* Returns the parent form.
*
* @return FormInterface|null The parent form or null if there is none
* @return $this|null The parent form or null if there is none
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be self|null then

@@ -80,7 +80,7 @@ public function has($name);
*
* @param string $name The name of the child to remove
*
* @return FormInterface The form instance
* @return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually $this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -70,7 +70,7 @@
*
* @param Guess[] $guesses An array of guesses
*
* @return Guess|null The guess with the highest confidence
* @return static|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or self|null

@@ -191,7 +191,7 @@ public function getStatusCode()
/**
* Finds children profilers.
*
* @return Profile[] An array of Profile
* @return Profile[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self[] ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Dec 26, 2016
@fabpot
Copy link
Member Author

fabpot commented Dec 26, 2016

@wouterj @ogizanagi @ro0NL Thank you very much for your reviews. I think we're good now.

@ro0NL
Copy link
Contributor

ro0NL commented Dec 26, 2016

@fabpot are you planning to do all branches?

@fabpot fabpot merged commit 3c0693d into symfony:2.7 Dec 27, 2016
fabpot added a commit that referenced this pull request Dec 27, 2016
…ant (fabpot)

This PR was merged into the 2.7 branch.

Discussion
----------

Fix @return statements to use $this or static when relevant

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20290
| License       | MIT
| Doc PR        | n/a

see #20290

Commits
-------

3c0693d fixed @return when returning this or static
fabpot added a commit that referenced this pull request Dec 29, 2016
This PR was merged into the 2.8 branch.

Discussion
----------

fixed @return when returning this or static

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Follow-up of #21054 for the 2.8 branch.

Commits
-------

7808b67 fixed @return when returning this or static
nicolas-grekas added a commit that referenced this pull request Jan 2, 2017
…terface (wouterj)

This PR was merged into the 2.7 branch.

Discussion
----------

Fixed `@return self` with `$this` in FormConfigBuilderInterface

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Fixes a tiny bug introduced by #21054 . `FormConfigBuilder` methods actually return `$this` and not `self`. This is important, as the `FormBuilder` (extending `FormConfigBuilder`) methods can also be called (e.g. `$builder->setDataMapper(...)->add()` is perfectly possible).

Commits
-------

505e84d Fixed `@return self` with `$this`
@fabpot fabpot deleted the return-stmt branch January 7, 2017 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants