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

Fix PHP 7.4 support #203

Merged
merged 1 commit into from
Jul 19, 2019
Merged

Fix PHP 7.4 support #203

merged 1 commit into from
Jul 19, 2019

Conversation

nicolas-grekas
Copy link
Contributor

Symfony is going to need this to have green CI on PHP 7.4 :)

@nicolas-grekas
Copy link
Contributor Author

Ready.

Copy link
Owner

@egulias egulias left a comment

Choose a reason for hiding this comment

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

Hi @nicolas-grekas , thanks for the update.
I have left some comments for you.

</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
Copy link
Owner

Choose a reason for hiding this comment

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

Would you mind explaining why is this listener needed?

Copy link
Contributor Author

@nicolas-grekas nicolas-grekas Jul 18, 2019

Choose a reason for hiding this comment

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

because PHP 7.4 introduces new deprecations that affect phpunit, and the listener allows silencing them

@@ -77,10 +77,16 @@ class EmailLexer extends AbstractLexer

protected $previous;

public function __construct()
{
$this->reset();
Copy link
Owner

Choose a reason for hiding this comment

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

Calling reset on construction sounds a bit odd, reseting something 'new'. For what I've seen in the rest of the changes, this is needed mainly to do the equality evaluation (e.g https://github.com/egulias/EmailValidator/pull/203/files#diff-c759db088d5c729c1f537d85c2ea93d0R187).
I would suggest something like init() :

$this->hasInvalidTokens = false;
$this->previous = $this->token = ['type' => null, 'value' => '', 'position' => 0];

and call that from both __contruct & reset.

Copy link
Contributor Author

@nicolas-grekas nicolas-grekas Jul 18, 2019

Choose a reason for hiding this comment

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

the code is full of checks with $this->lexer->token['type'], when $this->lexer->token is null actually. PHP 7.4 is now refusing to access keys on null values, so that the affected properties must be set to an array representing the null-state. reset does it.

Copy link
Owner

@egulias egulias Jul 18, 2019

Choose a reason for hiding this comment

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

Thanks for the explanation, understood. My suggestion is moving both lines to a new method so they can call it having a more expresive meaning and avoiding coupling too much construction with re-use (reseting to analyse another string).

__construct()
{
    $this->init();
}

reset()
{
...
$this->init();
}

public function reset()
{
$this->hasInvalidTokens = false;
parent::reset();
$this->previous = $this->token = ['type' => null, 'value' => '', 'position' => 0];
Copy link
Owner

Choose a reason for hiding this comment

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

I'd suggest to use EmailLexer::C_NUL for type instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this wouldn't be the same - we don't want to this be confused with the NUL char.

Copy link
Owner

Choose a reason for hiding this comment

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

Agree. Should a new token be added?

Copy link
Contributor 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, null is really the best "const" for this to me.

@@ -59,7 +60,7 @@ public function testAccumulatesWarnings()
];
$expectedResult = array_merge($warnings1, $warnings2);

$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
$lexer = new EmailLexer();
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@@ -122,8 +128,10 @@ public function getPrevious()
public function moveNext()
{
$this->previous = $this->token;
$hasNext = parent::moveNext();
$this->token = $this->token ?: ['type' => null, 'value' => '', 'position' => 0];
Copy link
Owner

Choose a reason for hiding this comment

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

Is this line because of the lexer reaching the last token and being set to null?
I'd suggest to express that case with something like

public function moveNext()
{
    $this->previous = $this->token;
    $hasNext = parent::moveNext();
    /* new stuff */ $this->setEndToken($hasNext);
    return $hasNext;
}

private function setFinalToken($nextToken)
{
    $this->token = $this->token ?: ['type' => C_NUL, 'value' => '', 'position' => 0];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right about the reason: $this->token cannot be null.
not sure I get what the additional method would provide thought :)

Copy link
Owner

Choose a reason for hiding this comment

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

To understood this line I had to review carefully the full PR and take my time, even with my knowledge of the library.
The additional method adds clarity on the reason behind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed a variant, does it solve the concern?

Copy link
Owner

Choose a reason for hiding this comment

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

I think it can work.
Thanks!

@egulias egulias merged commit a6c8d71 into egulias:master Jul 19, 2019
@SamMousa
Copy link

Is this really the way to go about this @nicolas-grekas ??
It seems weird to me for 3rd party libraries to be required to include some dev package from symfony just so Symfony can have their tests pass...

@nicolas-grekas nicolas-grekas deleted the php74 branch July 22, 2019 15:24
@nicolas-grekas
Copy link
Contributor Author

nicolas-grekas commented Jul 22, 2019

@SamMousa you must be confused, this added dev dep is not to make Symfony's tests pass, but about making this very package tests pass.

@SamMousa
Copy link

SamMousa commented Jul 22, 2019

I have just removed it locally, the test listener and the dependency, and tests still pass.

Note: that's on PHUnit 6.

@nicolas-grekas
Copy link
Contributor Author

nicolas-grekas commented Jul 22, 2019

Try again with PHP7.4 + latest phpunit 6 (as required by composer.json).

@SamMousa
Copy link

root@ba1517249f5f:/project# php --version
PHP 7.4.0alpha3 (cli) (built: Jul 12 2019 21:26:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies

PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

...............................................................  63 / 253 ( 24%)
............................................................... 126 / 253 ( 49%)
............................................................... 189 / 253 ( 74%)
...........................................................SSSS 252 / 253 ( 99%)
.                                                               253 / 253 (100%)

Time: 537 ms, Memory: 6.00MB

OK, but incomplete, skipped, or risky tests!
Tests: 253, Assertions: 351, Skipped: 4.

@SamMousa
Copy link

SamMousa commented Jul 22, 2019 via email

@nicolas-grekas
Copy link
Contributor Author

nicolas-grekas commented Jul 22, 2019 via email

@mlocati
Copy link
Contributor

mlocati commented Jul 30, 2019

@egulias would you accept a similar pull request for version 1?

@egulias
Copy link
Owner

egulias commented Jul 30, 2019

Hi @mlocati
Yes, happy do it. It is still in use from what Packagist says.

@mlocati
Copy link
Contributor

mlocati commented Jul 31, 2019

Done: see #206

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.

5 participants