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

[Yaml] don't override internal PHP constants #12372

Merged
merged 1 commit into from
Nov 16, 2014
Merged

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Nov 1, 2014

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #11783
License MIT
Doc PR

@xabbuh xabbuh changed the title [Yaml] don't override built-in PHP constants [Yaml] don't override internal PHP constants Nov 1, 2014
@fabpot
Copy link
Member

fabpot commented Nov 1, 2014

Would it make sense to make similar changes elsewhere in the code where we also override PHP constants? At least where speed is not critical.

@xabbuh
Copy link
Member Author

xabbuh commented Nov 1, 2014

Which parts do you have in mind?

@fabpot
Copy link
Member

fabpot commented Nov 2, 2014

I think almost all of them:

src/Symfony/Bridge/Twig/Extension/CodeExtension.php
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php
src/Symfony/Component/ClassLoader/ClassMapGenerator.php
src/Symfony/Component/Console/Input/InputDefinition.php
src/Symfony/Component/Debug/ExceptionHandler.php
src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php
src/Symfony/Component/Intl/Resources/bin/common.php
src/Symfony/Component/Process/Tests/PipeStdinInStdoutStdErrStreamSelect.php
src/Symfony/Component/Templating/PhpEngine.php
src/Symfony/Component/Yaml/Exception/ParseException.php

@xabbuh
Copy link
Member Author

xabbuh commented Nov 16, 2014

I updated removed other redefinitions of internal PHP constants. However, src/Symfony/Component/Intl/Resources/bin/common.php and src/Symfony/Component/Process/Tests/PipeStdinInStdoutStdErrStreamSelect.php define some constants, but those are not related to the PHP core itself. So I didn't modify them.

@@ -477,9 +473,14 @@ protected function initializeEscapers()
* @return string the escaped value
*/
function ($value) use ($that) {
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
Copy link
Member

Choose a reason for hiding this comment

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

This should be done outside of the function to avoid computing it each time a value is escaped.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fabpot
Copy link
Member

fabpot commented Nov 16, 2014

👍 (after the escape() method is optimized)

@fabpot
Copy link
Member

fabpot commented Nov 16, 2014

Thank you @xabbuh.

@fabpot fabpot merged commit 376cc03 into symfony:2.3 Nov 16, 2014
fabpot added a commit that referenced this pull request Nov 16, 2014
This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] don't override internal PHP constants

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

Commits
-------

376cc03 don't override internal PHP constants
@xabbuh xabbuh deleted the issue-11783 branch November 16, 2014 18:07
@@ -176,7 +172,13 @@ public function formatFile($file, $line, $text = null)
$text = "$text at line $line";

if (false !== $link = $this->getFileLink($file, $line)) {
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be worth using PHP_VERSION_ID > 50400 instead ? This way, OPCache could optimize it (it will inline the constant value for sure, and it could be smart enough to inline the comparison result as it would compare 2 constants which would then eliminate the useless branch entirely)

Copy link
Member

Choose a reason for hiding this comment

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

that does make sense. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

That's indeed a good idea (see #12497).

@arnaugm
Copy link

arnaugm commented Nov 29, 2014

I've just cloned the symfony repository in the hackday of the SymfonyCon and I'm running the tests. I'm getting this strange error:

1) Symfony\Component\Form\Tests\Extension\Csrf\CsrfProvider\DefaultCsrfProviderTest::testGenerateCsrfToken
PHPUnit_Framework_Exception: PHP Notice:  Constant SYMFONY_TRAIT already defined in /home/arnau/code/symfony/src/Symfony/Component/ClassLoader/ClassMapGenerator.php on line 15
PHP Stack trace:
PHP   1. {main}() -:0
PHP   2. require_once() -:2143
PHP   3. define() /home/arnau/code/symfony/src/Symfony/Component/ClassLoader/ClassMapGenerator.php:15

Notice: Constant SYMFONY_TRAIT already defined in /home/arnau/code/symfony/src/Symfony/Component/ClassLoader/ClassMapGenerator.php on line 15

Call Stack:
    0.0037     825536   1. {main}() -:0
    0.5145  171491904   2. require_once('/home/arnau/code/symfony/src/Symfony/Component/ClassLoader/ClassMapGenerator.php') -:2143
    0.5145  171492048   3. define() /home/arnau/code/symfony/src/Symfony/Component/ClassLoader/ClassMapGenerator.php:15

After having a look at it with @weaverryan we arrived to that issue which is apparently related to the error.

This seems to be very related to my specific configuration, I'm using:
OS: Ubuntu 14.04.1 LTS
PHP: 5.5.9-1ubuntu4.5
PHPUnit: 3.7.20

@xabbuh
Copy link
Member Author

xabbuh commented Nov 29, 2014

@arnaugm I'll have a look at it. How do you execute the tests?

@arnaugm
Copy link

arnaugm commented Nov 29, 2014

Just with

phpunit

Thanks!

@desarrolla2
Copy link
Contributor

I have the same error. Same version of OS and PHP.
PHPUnit 4.3.1

Y tried this

phpunit --strict

and failing too but this is working

phpunit --filter="DefaultCsrfProviderTest::testGenerateCsrfToken"

And this working too

phpunit --filter="DefaultCsrfProviderTest"

I have the error in a lot of test.

@xabbuh
Copy link
Member Author

xabbuh commented Nov 29, 2014

@arnaugm @desarrolla2 Can you both please check if the error disappears when you modify the top of the src/Symfony/Component/ClassLoader/ClassMapGenerator.php file like this:

if (!defined('SYMFONY_TRAIT')) {
    if (PHP_VERSION_ID >= 50400) {
        define('SYMFONY_TRAIT', T_TRAIT);
    } else {
        define('SYMFONY_TRAIT', 0);
    }
}

@arnaugm
Copy link

arnaugm commented Nov 29, 2014

Yes, that makes the test pass.

@xabbuh
Copy link
Member Author

xabbuh commented Nov 29, 2014

Thanks for the confirmation @arnaugm. I opened a pull request (see #12716).

fabpot added a commit that referenced this pull request May 1, 2015
…dosten)

This PR was squashed before being merged into the 2.6 branch (closes #14515).

Discussion
----------

Do not override PHP constants, only use when available

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

In #12372, the override of internal constants has been deleted, but the definition of `JSON_PRETTY_PRINT` if not available is missing in that PR.

Commits
-------

d5cc056 Do not override PHP constants, only use when available
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

5 participants