Skip to content

Fix #124 Require parenthesis around clone()#137

Open
mbeccati wants to merge 1 commit intophp-fig:masterfrom
mbeccati:add-clone
Open

Fix #124 Require parenthesis around clone()#137
mbeccati wants to merge 1 commit intophp-fig:masterfrom
mbeccati:add-clone

Conversation

@mbeccati
Copy link
Copy Markdown

@mbeccati mbeccati commented May 5, 2026

Per request from @Crell on Discord ;-)

Resolves #124

Comment thread spec.md Outdated
Copy link
Copy Markdown
Collaborator

@Crell Crell 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 good with this. Waiting a few days for other WG members to comment if desired.

Comment thread spec.md Outdated
@mbeccati
Copy link
Copy Markdown
Author

mbeccati commented May 6, 2026

@jrfnl I'm not a native speaker, but to me it feels used would be more appropriate than called when referring to a language construct (vs function). Thoughts?

@jrfnl
Copy link
Copy Markdown
Contributor

jrfnl commented May 6, 2026

@jrfnl I'm not a native speaker, but to me it feels used would be more appropriate than called when referring to a language construct (vs function). Thoughts?

@mbeccati I agree, used reads better.

@jrfnl
Copy link
Copy Markdown
Contributor

jrfnl commented May 6, 2026

Question for the interpretation by tooling:

The examples have no spacing between the keyword and the open parenthesis and on the inside of the parentheses.

Can we conclude that the rule about this for function/method calls (line 781 - 785) also applies to these language constructs ?

When making a method or function call, there MUST NOT be a space between the
method or function name and the opening parenthesis, there MUST NOT be a space
after the opening parenthesis, and there MUST NOT be a space before the
closing parenthesis. In the argument list, there MUST NOT be a space before
each comma, and there MUST be one space after each comma.

Comment thread spec.md
```

The `clone` language construct MUST always be used with parenthesis, even when the optional `$withProperties` argument is
not provided. For example:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@jrfnl How about something along the lines of

Suggested change
not provided. For example:
not provided. As such, it follows the same rules as function calls. For example:

Copy link
Copy Markdown
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 it needs to be spelled out as this paragraph is within the "function and method call" section anyway, but I just wanted to clarify to avoid ending up with discussions about this in the PHPCS repo later on.

@Crell
Copy link
Copy Markdown
Collaborator

Crell commented May 6, 2026

Agreed with @jrfnl about the spacing. Pseudo-function language constructs should always be formatted like functions, without a space.

@mbeccati
Copy link
Copy Markdown
Author

mbeccati commented May 7, 2026

print is not a function but a language construct. Its argument is the expression following the print keyword, and is not delimited by parentheses.

and

Note: Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.

Yet, it has a return value and disguises as a function.

Should we also globally regulate that language constructs masking as functions should follow the same rules as functions?

@Crell
Copy link
Copy Markdown
Collaborator

Crell commented May 7, 2026

I'm tempted to say yes, but that would also mean always requiring print have parens, which I don't think anyone does currently. So mandating that globally wouldn't be advisable.

@jrfnl
Copy link
Copy Markdown
Contributor

jrfnl commented May 7, 2026

I'm tempted to say yes, but that would also mean always requiring print have parens, which I don't think anyone does currently. So mandating that globally wouldn't be advisable.

Print is a "pure" language construct, so the parentheses have no function, other than to group whatever is being printed.
Exit/die/clone have over the last few years been changed to "language construct masking as functions" (which allows for fully qualified use, named arguments etc).

To me, there is a difference, though I can imagine not everyone can see that difference or is aware of it.

@Crell
Copy link
Copy Markdown
Collaborator

Crell commented May 10, 2026

Hm. How do we define "language construct cosplaying a function" in the spec in a way that is clear to people?

@mbeccati
Copy link
Copy Markdown
Author

Function-like language constructs, such as...

@Crell
Copy link
Copy Markdown
Collaborator

Crell commented May 10, 2026

Will the casual PHP reader understand that exit() is a function-like language construct but print() is not? Both permit parentheses.

@jrfnl
Copy link
Copy Markdown
Contributor

jrfnl commented May 10, 2026

print allows parentheses in the same way as return or include allow parentheses. The parentheses do not have a function for the construct, they only group whatever comes after.

Maybe it should be something to be clarified/defined in section 2 ? And then other sections can refer to that definition ?

@mbeccati
Copy link
Copy Markdown
Author

mbeccati commented May 10, 2026

With print, I'm a bit confused.

How do you suggest we treat the following?

$i = print("foo");

Likewise:

if (include("lib.php")) {
    ...
}

@KorvinSzanto
Copy link
Copy Markdown
Contributor

This feels a bit like scope creep to me. Can't we just add clone to the SHOULD list directly above that includes exit and die and work on a more general rule for language constructs in a separate issue? I'd argue we could get by with clone parenthesis being a SHOULD for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP 8.5: Always require parentheses around clone()

4 participants