Skip to content

Commit

Permalink
Switched to PSR2 with PHP_CodeSniffer and PHP-CS-Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayne committed Jul 22, 2016
1 parent 0e77d79 commit 0adfab4
Show file tree
Hide file tree
Showing 11 changed files with 604 additions and 583 deletions.
12 changes: 12 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in([
__DIR__ . DIRECTORY_SEPARATOR . 'src',
__DIR__ . DIRECTORY_SEPARATOR . 'tests',
]);

return Symfony\CS\Config\Config::create()
->level(\Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers(['short_array_syntax'])
->finder($finder);
36 changes: 4 additions & 32 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,9 @@ filter:
- 'tests/*'
checks:
php: true
coding_style:
php:
indentation:
general:
use_tabs: true
braces:
classes_functions:
class: end-of-line
function: end-of-line
closure: end-of-line
if:
opening: end-of-line
else_on_new_line: true
for:
opening: end-of-line
while:
opening: end-of-line
do_while:
opening: end-of-line
while_on_new_line: true
switch:
opening: end-of-line
try:
opening: end-of-line
catch_on_new_line: true
finally_on_new_line: true
upper_lower_casing:
keywords:
general: lower
constants:
true_false_null: lower
tools:
external_code_coverage:
runs: 4
runs: 2
php_code_sniffer:
config:
standard: "PSR2"
5 changes: 3 additions & 2 deletions src/NoSemanticVersionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
*
* @since 1.0.0-rc.1
*/
class NoSemanticVersionException extends RuntimeException {
}
class NoSemanticVersionException extends RuntimeException
{
}
163 changes: 84 additions & 79 deletions src/SemanticComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,87 @@
* @since 1.0.0-rc.1
* @see http://semver.org
*/
class SemanticComparator {
/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
* @see compare()
*/
public function __invoke(SemanticVersion $left, SemanticVersion $right) {
return $this->compare($left, $right);
}

/**
* Build metadata is ignored when determining version precedence.
*
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int `0` if both versions are equal, `< 0` if `$left` is smaller and `> 0` if `$left` is greater.
*/
public function compare(SemanticVersion $left, SemanticVersion $right) {
$result = $this->compareMajorMinorPatch($left, $right);

if ($result === 0) {
$result = $this->comparePre($left, $right);
}

return $result;
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
*/
private function compareMajorMinorPatch(SemanticVersion $left, SemanticVersion $right) {
$result = $left->getMajor() - $right->getMajor();

if ($result === 0) {
$result = $left->getMinor() - $right->getMinor();

if ($result === 0) {
$result = $left->getPatch() - $right->getPatch();
}
}

return $result;
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
*/
private function comparePre(SemanticVersion $left, SemanticVersion $right) {
if ($left->getPre() === '') {
return $right->getPre() === '' ? 0 : 1;
}

if ($right->getPre() === '') {
return -1;
}

$leftStack = $left->getPreStack();
$rightStack = $right->getPreStack();

$leftCount = count($leftStack);
$rightCount = count($rightStack);
$minCount = min($leftCount, $rightCount);

for ($i = 0; $i < $minCount; $i++) {
$result = strnatcasecmp($leftStack[$i], $rightStack[$i]);

if ($result !== 0) {
return $result;
}
}

return $leftCount - $rightCount;
}
}
class SemanticComparator
{
/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
* @see compare()
*/
public function __invoke(SemanticVersion $left, SemanticVersion $right)
{
return $this->compare($left, $right);
}

/**
* Build metadata is ignored when determining version precedence.
*
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int `0` if both versions are equal, `< 0` if `$left` is smaller and `> 0` if `$left` is greater.
*/
public function compare(SemanticVersion $left, SemanticVersion $right)
{
$result = $this->compareMajorMinorPatch($left, $right);

if ($result === 0) {
$result = $this->comparePre($left, $right);
}

return $result;
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
*/
private function compareMajorMinorPatch(SemanticVersion $left, SemanticVersion $right)
{
$result = $left->getMajor() - $right->getMajor();

if ($result === 0) {
$result = $left->getMinor() - $right->getMinor();

if ($result === 0) {
$result = $left->getPatch() - $right->getPatch();
}
}

return $result;
}

/**
* @param SemanticVersion $left
* @param SemanticVersion $right
* @return int
*/
private function comparePre(SemanticVersion $left, SemanticVersion $right)
{
if ($left->getPre() === '') {
return $right->getPre() === '' ? 0 : 1;
}

if ($right->getPre() === '') {
return -1;
}

$leftStack = $left->getPreStack();
$rightStack = $right->getPreStack();

$leftCount = count($leftStack);
$rightCount = count($rightStack);
$minCount = min($leftCount, $rightCount);

for ($i = 0; $i < $minCount; $i++) {
$result = strnatcasecmp($leftStack[$i], $rightStack[$i]);

if ($result !== 0) {
return $result;
}
}

return $leftCount - $rightCount;
}
}
Loading

0 comments on commit 0adfab4

Please sign in to comment.