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

Adding scrutinizer for code coverage #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# .scrutinizer.yml
filter:
paths: [src/*]
tools:
php_code_coverage: true
external_code_coverage: true
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ before_script:
- phpenv rehash

script:
- php vendor/bin/phpunit
- php vendor/bin/phpunit --coverage-clover=coverage.clover
- php vendor/bin/phpcs -n --standard=PSR2 ./src

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion src/Particle/Validator/Exception/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InvalidValueException extends \Exception implements ExceptionInterface

/**
* @param string $message
* @param int $identifier
* @param string $identifier
* @param \Exception $previous
*/
public function __construct($message, $identifier, \Exception $previous = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Particle/Validator/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function setParameters($key, $name)
*/
public function isValid($key, Container $input)
{
return $this->validate($input->get($key), $input->getArrayCopy());
return $this->validate($input->get($key));
}

/**
Expand Down
28 changes: 13 additions & 15 deletions src/Particle/Validator/Rule/Alnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
*
* @package Particle\Validator\Rule
*/
class Alnum extends Rule
class Alnum extends Regex
{
/**
* A constant that will be used for the error message when the value is not alphanumeric.
*/
const NOT_ALNUM = 'Alnum::NOT_ALNUM';

/**
* A constant indicating spaces are allowed.
*/
const ALLOW_SPACES = true;

/**
* A constant indicated spaces are *not* allowed.
*/
const DISALLOW_SPACES = false;

/**
* The message templates which can be returned by this validator.
*
Expand All @@ -31,21 +41,14 @@ class Alnum extends Rule
self::NOT_ALNUM => 'The value of "{{ name }}" can only consist out of numeric and alphabetic characters'
];

/**
* Indicates whether or not this rule should accept spaces.
*
* @var bool
*/
protected $allowSpaces;

/**
* Construct the validation rule.
*
* @param bool $allowSpaces
*/
public function __construct($allowSpaces)
{
$this->allowSpaces = (bool) $allowSpaces;
$this->regex = $allowSpaces ? '~^[\p{L}0-9\s]*~iu' : '~^[\p{L}0-9]*$~iu';
}

/**
Expand All @@ -56,11 +59,6 @@ public function __construct($allowSpaces)
*/
public function validate($value)
{
$pattern = $this->allowSpaces ? '~^[\p{L}0-9\s]*~iu' : '~^[\p{L}0-9]*$~iu';

if (preg_match($pattern, $value) === 0) {
return $this->error(self::NOT_ALNUM);
}
return true;
return $this->match($this->regex, $value, self::NOT_ALNUM);
}
}
26 changes: 13 additions & 13 deletions src/Particle/Validator/Rule/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
*
* @package Particle\Validator\Rule
*/
class Alpha extends Rule
class Alpha extends Regex
{
/**
* A constant that will be used for the error message when the value contains non-alphabetic characters.
*/
const NOT_ALPHA = 'Alpha::NOT_ALPHA';

/**
* A constant indicated spaces are allowed.
*/
const ALLOW_SPACES = true;

/**
* A constant indicating spaces are *not* allowed.
*/
const DISALLOW_SPACES = false;

/**
* The message templates which can be returned by this validator.
*
Expand All @@ -31,19 +41,14 @@ class Alpha extends Rule
self::NOT_ALPHA => 'The value of "{{ name }}" can only consist out of alphabetic characters'
];

/**
* @var bool
*/
protected $allowWhitespace;

/**
* Construct the Alpha rule.
*
* @param bool $allowWhitespace
*/
public function __construct($allowWhitespace)
{
$this->allowWhitespace = (bool) $allowWhitespace;
$this->regex = $allowWhitespace ? '~^[\p{L}\s]*$~iu' : '~^[\p{L}]*$~ui';
}

/**
Expand All @@ -54,11 +59,6 @@ public function __construct($allowWhitespace)
*/
public function validate($value)
{
$regex = $this->allowWhitespace ? '~^[\p{L}\s]*$~iu' : '~^[\p{L}]*$~ui';

if (preg_match($regex, $value) === 0) {
return $this->error(self::NOT_ALPHA);
}
return true;
return $this->match($this->regex, $value, self::NOT_ALPHA);
}
}
7 changes: 7 additions & 0 deletions src/Particle/Validator/Rule/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Length extends Rule
self::TOO_LONG => 'The value of "{{ name }}" is too long, should be {{ length }} characters long'
];

/**
* The length the value should have.
*
* @var int
*/
protected $length;

/**
* Construct the Length validator.
*
Expand Down
18 changes: 15 additions & 3 deletions src/Particle/Validator/Rule/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,24 @@ public function __construct($regex)
*/
public function validate($value)
{
$result = preg_match($this->regex, $value);
return $this->match($this->regex, $value, self::NO_MATCH);
}

/**
* A method to match against a regex. If it doesn't match, it will log the message $reason.
*
* @param string $regex
* @param mixed $value
* @param string $reason
* @return bool
*/
protected function match($regex, $value, $reason)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice :-)

$result = preg_match($regex, $value);

if ($result === 0) {
return $this->error(self::NO_MATCH);
return $this->error($reason);
}

return true;
}
}
14 changes: 14 additions & 0 deletions src/Particle/Validator/Rule/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,27 @@ class Required extends Rule
*/
protected $shouldBreak;

/**
* Indicates if the value is required.
*
* @var bool
*/
protected $required;

/**
* Optionally contains a callable to overwrite the required requirement on time of validation.
*
* @var callable
*/
protected $requiredCallback;

/**
* Indicates if the value can be empty.
*
* @var bool
*/
protected $allowEmpty;

/**
* Optionally contains a callable to overwrite the allow empty requirement on time of validation.
*
Expand Down