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

Check PHP version in stubs classes #145

Closed
wants to merge 2 commits into from
Closed

Conversation

jdeniau
Copy link

@jdeniau jdeniau commented Sep 5, 2018

I added a test in stubs classes to prevent php -l to fail on polyfills (errors like PHP Fatal error: Cannot declare class ArithmeticError, because the name is already in use)

  • add test to lint polyfill files
  • make lint test pass

@keradus
Copy link
Member

keradus commented Sep 5, 2018

👎 on that,
stubs are not loaded if they are not needed, eg, if we are on php7, we have TypeError defined, thus TypeError.php is not autoloaded at all. If we are on php5, TypeError is not defined by language, thus TypeError.php is loaded and in will always have to register the class.
that means, added conditions will always execute as true

@jdeniau jdeniau changed the title [WIP] Check PHP version in stubs classes Check PHP version in stubs classes Sep 5, 2018
@jdeniau
Copy link
Author

jdeniau commented Sep 5, 2018

@keradus You are right, but if you want to write a wordpress plugin, the wordpress svn pre-commit hook will simply reject your plugin because symfony/polyfill-php70 is included in the vendors 😞

As you said, stubs will be included only on older version of PHP, and only one time, so I think the "cost" of an if (true) is really insignificant versus the developper experience here.

@keradus
Copy link
Member

keradus commented Sep 5, 2018

if wordpress has issues how to properly deal with code, it shall be fixed on wordpress level, if you ask me

@jdeniau
Copy link
Author

jdeniau commented Sep 5, 2018

Of course, that's the proper way, and I already asked as you can imagine.

But I don't think it's only a wordpress issue, or I would not have opened a PR (I already forked polyfill-php70 and used it in my composer file), as linting all file of a directory to check errors is something often done.
I quoted Wordpress as an example here.

@nicolas-grekas
Copy link
Member

I agree with @keradus, this is working around someone else's issue.

@stof
Copy link
Member

stof commented Sep 5, 2018

When relying on autoloading, a class will not be autoloaded if it already exists in the runtime.

So the issue is in the infra running php -l on any file without taking things like that into account, not in code relying on the way PHP works.

@nicolas-grekas
Copy link
Member

Thanks for proposing.

@jdeniau jdeniau deleted the php70lint branch September 20, 2018 14:22
@jdeniau jdeniau restored the php70lint branch September 20, 2018 14:22
@karser
Copy link

karser commented Jun 10, 2020

Encountered this issue. Unfortunately, removing symfony/property-access from the plugin.
image

@nickyoung87
Copy link

I am also having this issue when pushing to SVN for the WP plugin repo. The same exact issue as @karser

What's the workaround here?

@TimothyBJacobs
Copy link

if wordpress has issues how to properly deal with code, it shall be fixed on wordpress level, if you ask me

I don't think this is WordPress doing anything improper. It doesn't seem unreasonable to me to expect that all files in a project are valid according to PHP's own built-in linter.

@bshaffer
Copy link

bshaffer commented Sep 1, 2020

@nickyoung87 @TimothyBJacobs
I added a workaround (and instructions) in this gist. Apply that patch to your svn branch to fix the issue! (via @jdeniau)

Note: This is only for polyfill-php70, a different patch file would need to be created for polyfill-php56

@nickyoung87
Copy link

Thanks @bshaffer! My only problem with this as a solution is now I have to remember to make that change if this package ever gets updated (and the fix is not included). Hopefully that doesn't happen :D

@Spreeuw
Copy link

Spreeuw commented Sep 8, 2020

as an alternative to applying a diff like @bshaffer does, you could also iterate over the files using bash. I'm using this in my github actions when deploying:

for filename in *.php; do
    [ -e "$filename" ] || continue
    sed -i '2s/^/if (PHP_VERSION_ID < 70000) :\n/' "$filename"
    sed -i '$s/$/\nendif;/' "$filename"
done

but of course you can run this locally too

figureone added a commit to uhm-coe/authorizer that referenced this pull request Sep 8, 2020
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.

None yet

9 participants