Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Developed optional strict comparison for the IsInt validator. #71

Merged
merged 6 commits into from
Apr 25, 2018
Merged

Developed optional strict comparison for the IsInt validator. #71

merged 6 commits into from
Apr 25, 2018

Conversation

wenkepaul
Copy link
Contributor

Strict comparison is off by default.

asgrim
asgrim previously requested changes Apr 19, 2017
*
* @var int
*/
protected $strict = self::COMPARE_NOT_STRICT;
Copy link

Choose a reason for hiding this comment

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

Why would this value be an int, rather than bool? I can't see there being any other values (strict vs not)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I drop the constants if it'll always be a boolean?

Copy link

Choose a reason for hiding this comment

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

Yes

/**
* Sets the strict option mode
*
* @param $strict
Copy link

Choose a reason for hiding this comment

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

No type defined here

*/
public function setStrict($strict)
{
if ($strict !== self::COMPARE_NOT_STRICT && $strict !== self::COMPARE_STRICT) {
Copy link

Choose a reason for hiding this comment

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

If this is changed to be a bool then this check can be much simpler

@@ -103,6 +144,9 @@ public function isValid($value)

if (is_int($value)) {
return true;
} elseif (self::COMPARE_STRICT == $this->strict) {
Copy link

Choose a reason for hiding this comment

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

Does this need to be an elseif here? Doesn't seem connected to the previous statement. Also, should be using a strict comparison (===), except if changed to bool as above, then it'd just be if ($this->strict) {


/**
* @dataProvider strictIntDataProvider()
* @param $intVal
Copy link

Choose a reason for hiding this comment

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

@param mixed $intVal

/**
* @dataProvider strictIntDataProvider()
* @param $intVal
* @param $expected
Copy link

Choose a reason for hiding this comment

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

@param bool $expected

$this->validator->setStrict(-1);
}

public function strictIntDataProvider()
Copy link

Choose a reason for hiding this comment

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

Docblock missing, at least we should have @return array (helps for adding PHP 7 return type declarations later)

@@ -103,6 +144,9 @@ public function isValid($value)

if (is_int($value)) {
return true;
} elseif (self::COMPARE_STRICT == $this->strict) {
$this->error(self::INVALID);
Copy link

Choose a reason for hiding this comment

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

I wonder if we should introduce a new error type for this to ensure distinction (e.g. self::NOT_STRICTLY_INT for example)

* Sets the strict option mode
*
* @param $strict
* @return $this
Copy link

Choose a reason for hiding this comment

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

@return self (helps for adding return type declarations later)

@@ -39,6 +42,13 @@ class IsInt extends AbstractValidator
protected $locale;

/**
* Type of strict check to be used. "123" == 123
Copy link

Choose a reason for hiding this comment

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

I think comment could be expanded a little to explain what happens when "123" == 123 would be evaluated (i.e. say it would fail validation).

wenkepaul and others added 5 commits April 25, 2018 14:08
…pdated docblocks where necessary. Wrote docs for strict validation.
Fixes a CS issue as reported by phpcs. Additionally, updates the new
tests to use `expectException()` instead of `setExpectedException()`.
@weierophinney weierophinney dismissed asgrim’s stale review April 25, 2018 19:11

Changes were made as requested.

@weierophinney weierophinney merged commit 1c150c8 into zendframework:master Apr 25, 2018
weierophinney added a commit that referenced this pull request Apr 25, 2018
weierophinney added a commit that referenced this pull request Apr 25, 2018
@weierophinney
Copy link
Member

Thanks, @wenkepaul!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants