semver icon indicating copy to clipboard operation
semver copied to clipboard

add bump method on version

Open cordoval opened this issue 10 years ago • 10 comments

does it have support for ->bump(SemVer::MAJOR) or such?

like here https://github.com/vierbergenlars/php-semver/tree/master#functions

https://github.com/vierbergenlars/php-semver/blob/2.x/src/vierbergenlars/SemVer/version.php#L127

<?php

  /**
     * Increment the version number
     * @param  string                         $what One of 'major', 'minor', 'patch' or 'build'
     * @return \vierbergenlars\SemVer\version
     * @throws SemVerException                When an invalid increment value is given
     */
    public function inc($what)
    {
        if ($what == 'major') {
            return new version(($this->major + 1) . '.0.0');
        }
        if ($what == 'minor') {
            return new version($this->major . '.' . ($this->minor + 1) . '.0');
        }
        if ($what == 'patch') {
            return new version($this->major . '.' . $this->minor . '.' . ($this->patch + 1));
        }
        if ($what == 'build') {
            if ($this->build == -1) {
                return new version($this->major . '.' . $this->minor . '.' . $this->patch . '-1');
            }

            return new version($this->major . '.' . $this->minor . '.' . $this->patch . '-' . ($this->build + 1));
        }
        throw new SemVerException('Invalid increment value given', $what);
    }

cordoval avatar Jan 25 '14 12:01 cordoval

That's not a bad idea.

Thinking about having the SemVer objects be immutable:

// Bump major by 1
$higherSemver = bump($semver, SemVer::MAJOR);

// bump minor by 4
$higherSemver = bump($semver, SemVer::MINOR, 4);

or mutable:

$semver->increase(SemVer::MAJOR);
$semver->increase(SemVer::MINOR, 5);

naneau avatar Jan 25 '14 18:01 naneau

I'd do the immutable way if there's a need for comparison or anything like that, but prefer the mutable way.

ivarb avatar Jan 28 '14 10:01 ivarb

Immutable. Otherwise you run into stuff like this:

<?php

$onePointOne = new SemVer("1.1");
$onePointOne->bump(SemVer::major);

echo $onePointOne;//WTF???

marijn avatar Jun 05 '14 06:06 marijn

Maybe it's an idea to make a mutable one that extends the immutable SemVer? Or, at a given point, be able to freeze the mutable SemVer thus transforming it into a immutable one (like Symfony does with ParameterBag).

ivarb avatar Jun 05 '14 07:06 ivarb

What benefit would that provide?

marijn avatar Jun 05 '14 08:06 marijn

To be able to support both

ivarb avatar Jun 05 '14 08:06 ivarb

What is the benefit of a maintaining a mutable implementation? To me it seems it will only result in confusion.

marijn avatar Jun 05 '14 08:06 marijn

Well true, most of the time you would definitely want an immutable SemVer. In the current implementation the Parser seems to need a mutable though.

ivarb avatar Jun 05 '14 08:06 ivarb

In that case adapting the Parser seems to be a more worthwhile adventure. At least in my opinion :smile:.

marijn avatar Jun 05 '14 09:06 marijn

It probably needs to be refactored and pass the Version metadata through the constructor for an immutable SemVer.

ivarb avatar Jun 05 '14 09:06 ivarb