Skip to content
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

Only add PHP opening tag when the following rule of thumb are met #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ravage84
Copy link
Contributor

@ravage84 ravage84 commented Jun 5, 2023

While I like the consistency of having opening tags everywhere (as in #74), I can see it being "too much", especially for one liners.

What do you think of the following rule of thumb proposal?

Add PHP opening tags when all requirements are met:

  • Tne mandatory class namespace is defined or no class or interface respectively is defined
  • All necessary classes & functions are imported
  • All necessary variables & constants are defined
  • All necessary structures are visible or hinted at with a comment
  • All statements and/or structures are related to each other
  • Or the PHP opening tag is essential to the code example

Do not add PHP opening tags when:

  • Only one or many unrelated statements are shown
  • Only one or many unrelated functions are shown
  • Only one or many unrelated methods are shown
  • Only one or many unrelated classes or interfaces respectively are shown
  • Only one or many unrelated other structures, such as an if-clause or match expression, are shown

Add PHP opening tags when all requirements are met:

- Tne [mandatory class namespace](https://www.php-fig.org/psr/psr-1/#3-namespace-and-class-names) is defined or no class or interface respectively is defined
- All necessary classes & functions are imported
- All necessary variables & constants are defined
- All necessary structures are visible or hinted at with a comment
- All statements and/or structures are related to each other
- Or the PHP opening tag is essential to the code example

Do not add PHP opening tags when:

- Only one or many unrelated statements are shown
- Only one or many unrelated functions are shown
- Only one or many unrelated methods are shown
- Only one or many unrelated classes or interfaces respectively are shown
- Only one or many unrelated other structures, such as an if-clause or match expression, are shown
@@ -234,8 +234,6 @@ When using compound namespaces, there MUST NOT be more than two sub-namespaces w
That is, the following is allowed:

```php
<?php

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd leave this one. We're talking about the use statements, which happen at a very specific place in the file, so I think it's fine to leave here.

@vjik
Copy link
Contributor

vjik commented Sep 8, 2023

Suggest shorten the list when need add PHP opening tag to several items:

  • block contain code with several sections (for example, class declaration + use) that can be placed whole in file;
  • the PHP opening tag is essential to the code example.

In otherwise cases <?php looks overhead.

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.

3 participants