Commit graph

2127 commits

Author SHA1 Message Date
Henrique Moody b7c4189b4f
Remove "--CREDITS--" from PHPT files
Most of those files have undergone massive changes; in some cases, the
"--CREDITS--" might not make sense.

This is aligned with the latest coding standard changes [1], since we no
longer allow the annotation "@author".

In any case, we can always use `git blame` or `git log` to see the
contributors.

[1]: 9a13c9fb03

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:49:36 +01:00
Henrique Moody e4f2c8154a
Use PHP attributes to define templates
Creating a specific exception for each rule adds a painful overhead. If
you want to make a custom message for your rule, you will need to create
an exception and then register that exception namespace to be able to
use it—all that is just for customizing the message of your rule.

Having different namespaces also implies that you need to fetch the
exception of the rule from another directory to change it. As Uncle Bob
said, "Classes that change together belong together. Classes that are
not reused together should not be grouped."

This commit will drastically change this library, moving all the
templates from the exceptions to the rules. Consequently, the Factory
becomes much simpler, and the library gets a bit smaller, too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:43:57 +01:00
Henrique Moody 2b59e3df49
Only pass the necessary parameters to the exceptions
Currently, we convert the properties of a rule into parameters and pass
them to the exceptions. That complicates things for a few reasons:

1. The exception knows too much: there's a lot of information in an
   object, and the exception would only need a few parameters to work
   correctly.

2. Any variable change becomes a backward compatibility break: if we
   change the name of the variable type in a rule, even if it's a
   private one, we may need to change the template, which is a backward
   compatibility break.

3. The factory is bloated because of introspection tricks: it reads the
   properties from the class, even from the parent, and then passes it
   to the exception.

Of course, that means we introduce another method to `Validatable`, but
in most cases, extending `AbstractRule` is enough to create a new rule.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:30:38 +01:00
Henrique Moody dd896bb12d
Move template definitions to the rules
It's easier to identify the reason for choosing a specific message in
the rule than in the exception. The same goes for the key we use to
determine the templates.

This change will simplify the `ValidationException` because it will
already receive the template it needs to use. As a consequence, the
`Factory` also becomes more straightforward.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:17:27 +01:00
Henrique Moody 3334529591
Merge branch '2.3' 2024-01-29 23:14:41 +01:00
Henrique Moody debf6c556e
Make DateTime tests pass
I did some digging but couldn't figure out what was happening. It
probably is something related to today's date because PHP itself is not
parsing dates correctly.

```php
echo DateTime::createFromFormat('Ym', '202302')->format('Ym');
// Outputs 202303
```

For now, I'm just making the tests pass, but I created an issue with
getting back to it later.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 22:28:49 +01:00
Henrique Moody b1670c6d9a
Merge branch '2.3' 2024-01-28 21:52:24 +01:00
Henrique Moody 176d76c709
Create Read the Docs configuration file
The documentation is outdated because the file doesn't exist.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 21:50:01 +01:00
Henrique Moody 4a06ed30c0
Upgrade "phpstan/phpstan" to version 1.10
In practice it doesn't change the version, but at least it's up-do-date
in "composer.json".

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 15:15:09 +01:00
Henrique Moody 250fd93682
Rename "Attribute" rule to "Property"
Because now we have the concept of attributes in PHP, the rule with the
name "Attribute" makes no sense because it doesn't validate attributes
but properties.

In the future, it might be possible that Validation will have a rule
called "Attribute" to validate PHP attributes.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 15:07:42 +01:00
Henrique Moody 715bf4b16b
Merge branch '2.3' 2024-01-28 15:00:04 +01:00
César de la Cal 4c91e3397a
Fix wrong type in "KeySet" rule
The documentation states that we should use Validator::keySet() in
combination with Validator::key() but the return type of key() does not
match the expected parameter type of keyset().

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 14:55:45 +01:00
Henrique Moody 4c33f089fb
Upgrade "respect/stringifier" to version 2.0.0
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 14:47:13 +01:00
Henrique Moody 12c145756c
Upgrade "phpunit/phpunit"
This commit also replaces PHPUnit annotations with attributes.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 14:16:52 +01:00
Henrique Moody 9a19b235f8
Use a concrete class to test AbstractWrapper
Although testing abstract classes is good, using mocks makes the code
too complicated.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 04:31:26 +01:00
Henrique Moody 57b4c067bc
Use a concrete class to test AbstractSearcher
Although testing abstract classes is good, using mocks makes the code
too complicated.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 04:28:07 +01:00
Henrique Moody 0c7f26948b
Use a concrete class to test AbstractRule
Although testing abstract classes is good, using mocks makes the code
too complicated.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 04:04:01 +01:00
Henrique Moody ae1620b23d
Refactor "AbstractComposite" class
The class had too much complexity and some duplication on its children.

I started doing this because I'm refactoring the tests to upgrade
PHPUnit later, and then I made some improvements along the way.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 03:59:23 +01:00
Henrique Moody e983e52663
Make properties readonly
That will make it clear that we should not overwrite some properties.

Because of this change, I've made a few refactorings here and there.
It's nice to see that I've spotted some issues just because I was
setting some properties as `readonly`.

There are a few properties that I would like to make read-only, but to
do that I'd need to refactor a lot of code, so for now, I'm keeping it
as is.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 01:24:15 +01:00
Henrique Moody f9290f203e
Remove unnecessary property
There's no part of the library that uses that property, besides, it's
not ideal to overwrite a property every time we validate something.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 01:19:40 +01:00
Henrique Moody 917766d45c
Remove "setParams()" method from ValidationException
We already have a method called "updateParams()" which has a better name
for what it does. Besides, we don't really need this for this case.

Personally, I find it ugly to create a property in the rule just to have
it available in the exception, but that's how the design is.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 01:06:48 +01:00
Henrique Moody 1c91e7a2b4
Allow avoiding using "egulias/email-validator"
Currently, if you have "regulars/email-validator" installed, the `Email`
rule will always use it to validate emails. This commit introduces the
possibility of not using "egulias/email-validator" by passing `NULL` to
the constructor.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 01:06:40 +01:00
Henrique Moody 9a13c9fb03
Update coding standards
This change will bring many breaking changes. The good thing is that we
can finally use more modern resources available in PHP.

I can imagine that's not a popular change since it will bring many
breaking changes to users, but we shouldn't be stuck in time because of
that. Using some of those features will make it easier to contribute to
the project. At least, I hope so.

There are still some useless doc-blocks, and we're not using "readonly"
properties when we could. I aim to send those changes soon.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 00:22:41 +01:00
Henrique Moody d98d010b77
Upgrade "squizlabs/php_codesniffer"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:54:19 +01:00
Henrique Moody c6c911cf9f
Upgrade "egulias/email-validator"
The version 3.x hasn't been updated since June 2013.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:51:37 +01:00
Henrique Moody 16bef2d57b
Upgrade "symfony/polyfill-mbstring"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:49:59 +01:00
Henrique Moody bd325668a9
Merge branch '2.3' 2024-01-27 20:41:28 +01:00
Alan Taylor 8d7d783698
Check if property is initialized before getting its value
This commit also removed the use of "setAccessible", since it's not
neccessary after PHP 8.1.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:36:01 +01:00
Henrique Moody 6e3ed94076
Get CHANGELOG ready for release
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:14:16 +01:00
Henrique Moody cf3da2aa9c
Update changelog
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:09:57 +01:00
Henrique Moody 24edfda1aa
Drop support for PHP 8.0 and below
PHP 8.0 is no longer supported. Some of our dependencies now do not work
on PHP 8.0 anymore.

This commit will also run tests on PHP 8.3.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:04:17 +01:00
Henrique Moody bd953f99f0
Ignore PHPStan errors
I want to get the build green. Currently, PHPStan is complaining about a
couple of issues that are not so critical. It's especially concerning
that strict_types must be the very first statement on PHPT files, but
that's fine since PHPUnit parses its content.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 20:04:16 +01:00
Henrique Moody 177657d5f4
Upgrade Coding Standards
For now, I'm ignoring many rules because they'll make the changes that
would bring backward compatibility breaks, and I'm aiming to release a
minor version next.

However, upgrading "respect/coding-standard" is necessary because
version 4.0 no longer works.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-27 19:41:12 +01:00
The Respect Panda 703f610ee8 Update Regional Information 2023-06-13 21:46:15 -03:00
dependabot[bot] e3740860f9 Bump peter-evans/create-pull-request from 4 to 5
Bumps [peter-evans/create-pull-request](https://github.com/peter-evans/create-pull-request) from 4 to 5.
- [Release notes](https://github.com/peter-evans/create-pull-request/releases)
- [Commits](https://github.com/peter-evans/create-pull-request/compare/v4...v5)

---
updated-dependencies:
- dependency-name: peter-evans/create-pull-request
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-13 21:45:56 -03:00
Henrique Moody 2a743962e4
Use a concrete class to test AbstractEnvelop
It's essential to test our abstract classes because users might use
them. However, creating mocks when writing those tests make the code too
complicated.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 17:20:39 +02:00
Henrique Moody 2ee7509c2e
Make data providers static
From PHPUnit 10, all data providers need to be static. This commit will
make migrating from version 9 to 10 a bit easier.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 17:20:31 +02:00
Henrique Moody ebcf247187
Do not use mocks to simulate a rule
We can use the AlwaysValid and AlwaysInvalid rules in the tests instead
of mocking them. Those changes will help us later because we mainly use
the `createValidatableMock()` in the data providers and, as from
PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:38 +02:00
Henrique Moody 049b0b27ae
Do not use the test class to test the Callback rule
That will help us later because, on PHPUnit 10, all data providers need
to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:38 +02:00
Henrique Moody 445af454fd
Create a class to help test the Callable rule
With that, the tests will be more straightforward, and we won't need to
use the test class in the data providers. That will help us later
because, on PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:38 +02:00
Henrique Moody ee8dd98f54
Create class to help testing the Attribute rule
With that, the tests will be more straightforward, and we won't need to
use the test class in the data providers. That will help us later
because, on PHPUnit 10, all data providers need to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:38 +02:00
Henrique Moody 07c4095f11
Create stubs for PSR-7 interfaces
When we write tests requiring those interfaces, we create mocks. Those
new stubs will make those tests easier to read and allow us to reduce
the number of mocks we write with PHPUnit, making the code in the tests
a bit less complex.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:37 +02:00
Henrique Moody e8fcdb661f
Allow creating paths from fixture files
We had a method that returned the full path of the fixture directory,
and we frequently would concatenate that path with a file we needed. I
changed it to include the file's path inside the fixture directory. That
way, we avoid repeating the same patter over and over.

I made the method static because we use it in data providers, which need
to be static.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-03 16:28:37 +02:00
Henrique Moody 8a7bc1ab7a
Improve readability of integration tests
The integration tests use the same pattern to test exception messages.
With my changes, we won't validate which exception we throw in those
tests, but matching the message is enough. I created three functions to
replace most of those tests.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-01 07:40:42 +02:00
Henrique Moody 003830b8e9
Standardize exception messages
Most exception messages in Validation use "must" and "must not" in their
templates, but a few rules don't.

I fixed most of them, but AlwaysValid and AlwaysInvalid remain because I
wonder if they will be better if I update them.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-04-01 05:54:52 +02:00
Henrique Moody 830ad97126
Fix the typo on the "Exists" rule's exception
When I created that rule, I barely spoke English.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-03-22 20:39:02 +01:00
github-actions[bot] dd053f2eea
Update Regional Information (#1414)
Co-authored-by: The Respect Panda <therespectpanda@gmail.com>
2023-03-19 05:35:32 +00:00
Danilo Correa afa4cc41ea
Increase test coverage for some rules (#1412)
- Add test for null value in the Cnpj rule.
- Add UploadedFileInterface object test for Size rule.
- Add test for invalid values in Sorted rule.
2023-03-11 20:43:10 +00:00
Henrique Moody 508566eafc
Update list of rules in the chained validator (#1411)
The "bic" rule has been removed, and two more rules were added.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2023-03-10 14:42:27 +00:00
github-actions[bot] 2080e0d2fb
Update Regional Information (#1410)
Co-authored-by: The Respect Panda <therespectpanda@gmail.com>
2023-03-09 00:51:45 +00:00