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

Add validation constraints to configuration object #264

Conversation

daniruizcamacho
Copy link
Contributor

This feature is to add validation constraints to configuration object. This feature is related to this opened issue. #204

Copy link
Owner

@lcobucci lcobucci left a comment

Choose a reason for hiding this comment

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

@daniruizcamacho thank you very much for your contribution (and I'm really sorry for not replying to you on the issue).

Could you please take a look on these comments? I think we can improve things a bit more 👍
They're more about delegating things to PHP so we don't need to worry to much on the library's side.

P.S.: your tests are great, I'm addressing the PHP 7.2 compatibility in #247.

/**
* @var Constraint[]|null
*/
private $constraints;
Copy link
Owner

Choose a reason for hiding this comment

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

We could configure the default value already here, so that the property would never be null.

Copy link
Owner

Choose a reason for hiding this comment

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

What do you think about renaming the property in order to align to the accessors' names (getter/setter) - rename to validationConstraints?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, totally agree. I will rename this var and the param in the setter function.

return [];
}

return $this->constraints;
Copy link
Owner

Choose a reason for hiding this comment

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

This would be simplified if we set the default value. Another way to simplify this method would be:

public function getValidationConstraints(): array
{
    return $this->constraints ?? [];
}

I think I'd have the former but am open to discuss possibilities here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's simpler. I use here the guard clauses pattern sometimes it's more clear. https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

return $this->constraints;
}

public function setValidationConstraints(array $constraints): void
Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather have a variadic function for this, which would leave this validation up to PHP. Something like:

public function setValidationConstraints(Constraint ...$constraints): void
{
    $this->constraints = $constraints;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very interesting. I never use variadic on this way. Thanks for the tip.

$validationConstraints = $config->getValidationConstraints();

self::assertSame([], $validationConstraints);
self::assertNotSame($this->validationConstraints, $validationConstraints);
Copy link
Owner

Choose a reason for hiding this comment

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

This assertion is not necessary since the one above already validates if the value is exactly [], so I'd remove it.

Copy link
Owner

Choose a reason for hiding this comment

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

Which would allow us to inline the variable $ValidationConstraints, since it has a single usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, this is an error from my part.

* @uses \Lcobucci\JWT\Signer\None
* @uses \Lcobucci\JWT\Signer\Key
*/
public function getValidationConstraintsShouldReturnTheDefaultWhenItWasNotConfigured(): void
Copy link
Owner

Choose a reason for hiding this comment

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

I'd suggest to rename this method to getValidationConstraintsShouldReturnAnEmptyArrayWhenItWasNotConfigured, since it makes our test intention a bit more explicit

*/
public function setValidationConstraintsShouldThrowExceptionWhenConstraintsArrayIsNotValid(): void
{
self::expectException(\InvalidArgumentException::class);
Copy link
Owner

Choose a reason for hiding this comment

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

This would not be an InvalidArgumentException but rather a type error if we use the variadic form.

@lcobucci lcobucci self-assigned this Sep 10, 2018
@lcobucci lcobucci added this to the 4.0.0 milestone Sep 10, 2018
@daniruizcamacho
Copy link
Contributor Author

@lcobucci Don't worry about the issue, it's very difficult to have some time to answer all things. All comments are addressed.

Copy link
Owner

@lcobucci lcobucci left a comment

Choose a reason for hiding this comment

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

Just some tiny things! Can you please run phpcbf to adjust the coding standards as well?

src/Configuration.php Outdated Show resolved Hide resolved
* @uses \Lcobucci\JWT\Signer\None
* @uses \Lcobucci\JWT\Signer\Key
*/
public function setValidationConstraintsShouldThrowExceptionWhenConstraintsArrayIsNotValid(): void
Copy link
Owner

Choose a reason for hiding this comment

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

Since we're relying on PHP's validation I think that this test can be removed, the language will always enforce that requirement.

@lcobucci lcobucci force-pushed the feature/addValidationConstraintsToConfigurationObject branch from 3311e2c to 627f436 Compare September 16, 2018 20:53
Copy link
Owner

@lcobucci lcobucci left a comment

Choose a reason for hiding this comment

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

@daniruizcamacho thanks! Everything is 100%.

@lcobucci lcobucci merged commit 8d8bc4b into lcobucci:master Sep 16, 2018
@lcobucci lcobucci modified the milestones: 4.0.0, 4.0.0-alpha2 Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants