What is ~ (useful) and ^ (circumflex) in the Composer versioning scheme?

9

In some cases, I see that some libraries that use Composer , place ~ or ^ at the beginning of the library version, so :

"orchestra/testbench" : "^3.6"

Or

"phpunit/phpunit"    : "~7.0"

I wanted to know what the purpose of each of them is.

    
asked by anonymous 18.07.2018 / 17:55

1 answer

7

Summary

Consider the semantic versioning template: MAJOR.MINOR.PATCH

The tilde operator (~) allows the last digit to be incremented

~MAJOR.MINOR.PATCH  >=MAJOR.MINOR.PATCH e < MAJOR.(MINOR+1).0

That is, all the PATCHs of the version MAJOR.MINOR

Example: ~ 1.2.3 will match all PATCHs released after version 1.2.3. > 1.2.3 < = 1.3.0

Other examples:

"vendor/package": "~1.3.2", // >=1.3.2 <1.4.0
"vendor/package": "~1.3", // >=1.3.0 <2.0.0

The circumflex operator (^) searches for the latest major (MAJOR) version.

There is an exception if the major version is zero. In this case the behavior is fetching up to the latest minor version. This exception exists to respect the rule 4 of the semantic versioning:

  

At the beginning of development, the Major version MUST be zero (0.y.z). Anything can change at any time. The public API should not be considered stable. "

Example: ^ 1.2.3 will match any version 1.x.x, including 1.3.0, but will suspend in 2.0.0.

Other examples

"vendor/package": "^1.3.2", // >=1.3.2 <2.0.0
"vendor/package": "^0.3.2", // >=0.3.2 <0.4.0 // exceção se a versão maior (MAJOR) é 0

Explaining the examples of the question:

"orchestra / testbench": "^ 3.6" - indicates which package has a dependency on this library and ensures compatibility with all versions of version 3.6 or greater, but smaller than version 4.minor

"phpunit / phpunit": "~ 7.0" - indicates that package has a dependency of at least 7.0, but allows all 7.minor.patch

Documentation

According to the Composer documentation:

  

Tilde Version Range (~)

     

The operator is best explained by example: ~ 1.2 is equivalent to> 1.2 = 1.2.3 1.2.3

18.07.2018 / 21:52