Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix PHPDoc #24

Merged
merged 1 commit into from
Aug 7, 2019
Merged

Fix PHPDoc #24

merged 1 commit into from
Aug 7, 2019

Conversation

dontub
Copy link

@dontub dontub commented Aug 6, 2019

No description provided.

@nikolaposa
Copy link
Owner

nikolaposa commented Aug 6, 2019

Actually, neither preRelease or build cannot be null, but either https://github.com/nikolaposa/version/blob/master/src/Extension/NoPreRelease.php or https://github.com/nikolaposa/version/blob/master/src/Extension/NoBuild.php. (Special Case design pattern)

So you've discovered a potential bug, because rest of the Version class assumes preRelease will never be null (for example: https://github.com/nikolaposa/version/blob/master/src/Version.php#L201), but these methods allow null to be set. Therefore we should focus on fixing that.

@dontub
Copy link
Author

dontub commented Aug 6, 2019

Actually the result of $version->withPreRelease(null) and $version->withPreRelease(new NoPreRelease()) is the same. Also Version::formPart(1, 0, 0, null) would set the pre release part to NoPreRelease. Using null to set the pre release part to NoPreRelease makes totally sense to me. (The same holds true for the build part.)

@nikolaposa
Copy link
Owner

Yeah, which is why we need to fix those two methods to convert passed nulls into appropriate Special Case objects. Editing docblock type hints isn't enough.

@dontub
Copy link
Author

dontub commented Aug 7, 2019

Yeah, which is why we need to fix those two methods to convert passed nulls into appropriate Special Case objects.

This already happens in the constructor.

@nikolaposa
Copy link
Owner

nikolaposa commented Aug 7, 2019

Correct, but it doesn't happen in these setter methods. Let me demonstrate what could happen:

$version->withPreRelease(null);
$version->isPreRelease(); //Fatal error: call to a member function isEmpty() on null

Therefore they need to be modified, like so for example:

public function withPreRelease($preRelease) : Version
{
    if (is_string($preRelease)) {
        //...
    } elseif (null === $preRelease) {
        $preRelease = new NoPreRelease();
    }
        
    //...
}

@dontub
Copy link
Author

dontub commented Aug 7, 2019

I have to correct myself: it's in fromParts() not in the constructor where null is replaced by an object of type NoPreRelease and NoBuild, respectively.

The code already prevents that any attribute may became null because the only place where attributes are set is in the constructor and it's signature ensures that they won't be null:

__construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build)

This test shows that everything is as it should be:

$version1 = Version::fromParts(1, 2, 3, PreRelease::fromIdentifiers('4'));
$this->assertEquals(PreRelease::fromIdentifiers('4'), $version1->getPreRelease());
$this->assertTrue($version1->isPreRelease());
$version2 = $version1->withPreRelease(null);
$this->assertEquals(new NoPreRelease(), $version2->getPreRelease());
$this->assertFalse($version2->isPreRelease());

@nikolaposa
Copy link
Owner

Yeah, we are at the same page now, got it. 👍

@nikolaposa nikolaposa self-requested a review August 7, 2019 09:44
@nikolaposa nikolaposa self-assigned this Aug 7, 2019
@nikolaposa nikolaposa added this to the 3.2.0 milestone Aug 7, 2019
@nikolaposa nikolaposa merged commit df5c148 into nikolaposa:master Aug 7, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants