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

Contains numeric characters rule #22

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

mboiev
Copy link
Collaborator

@mboiev mboiev commented Mar 6, 2020

No description provided.

* @param string|null $message
*/
public function __construct(
?int $numberCharacters = 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you ensure that the value of the parameter is always a positive integer or zero?

if($this->strict){
return $this->countDigits($v)<=$this->numberCharacters;
}
else{
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the control flows rely on return, the else is not necessary.

if($this->message){
return $this->message;
}
else if($this->strict){
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the control flow (if, elseif, else) relies on return statements, all the else workflows are unecessary, you can replace with if, if and a direct return.

mboiev pushed a commit to mboiev/Validator that referenced this pull request Mar 7, 2020
Comment on lines +31 to +33
int $numberCharacters,
bool $strict,
?string $message = null
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also set the default parameter values I had provided in the issue?

)
{
if($numberCharacters<0)
throw new InvalidArgumentException();
Copy link
Contributor

Choose a reason for hiding this comment

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

When throwing an exception, a message should always be provided in order to inform the user why the exception was thrown without needing to look at the source code.

public function validate($v): bool
{
if($this->strict){
return $this->countDigits($v)<=$this->numberCharacters;
Copy link
Contributor

Choose a reason for hiding this comment

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

There is an error here. Can you find it?

return $this->message;
}
if($this->strict){
return "Number of numeric characters exceeds ".${$this->numberCharacters};
Copy link
Contributor

Choose a reason for hiding this comment

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

The message here should be:

"The value '{$v}' should have exactly {$this->numberCharacters} numeric characters"

if($this->strict){
return "Number of numeric characters exceeds ".${$this->numberCharacters};
}
return "Number of numeric characters should exceed ".${$this->numberCharacters};
Copy link
Contributor

Choose a reason for hiding this comment

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

The message here should be:

"The value '{$v}' should have at least {$this->numberCharacters} numeric characters"

Comment on lines +10 to +13
public function testValidate(){
$ruleFirst = new ContainsNumericCharacters(1,true);
$ruleSecond = new ContainsNumericCharacters(1,false);
$ruleThird= new ContainsNumericCharacters(1,false,"Custom Message");
Copy link
Contributor

Choose a reason for hiding this comment

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

Testing with various values instead of only '1' would be better for test coverage.

@jwillp jwillp linked an issue Mar 8, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Password checking related validation rules
2 participants