Skip to content

Commit

Permalink
Feat: Adding max level on PHP Stan (#4)
Browse files Browse the repository at this point in the history
feat: patterns, phpstan, tests and git actions
  • Loading branch information
matmper authored Apr 9, 2024
1 parent 2ab13be commit 143e3ca
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ trim_trailing_whitespace = true
indent_size = unset
trim_trailing_whitespace = false

[*.{yml,yaml,json,xml}]
[*.{yml,yaml,xml}]
indent_size = 2
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Ignore all test and documentation with "export-ignore".
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/Makefile export-ignore
/mkdocs.yml export-ignore
/phpcs.xml export-ignore
/phpunit.xml export-ignore

/.github export-ignore
/docs export-ignore
/tests export-ignore
13 changes: 13 additions & 0 deletions .github/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

message="$(cat $1)"
requiredPattern="^(feat|fix|refactor|docs|test|chore|style|build|revert): .*$"

if ! [[ $message =~ $requiredPattern ]];
then
echo "Invalid commit message, follow the pattern below"
echo "<commit_type>: <commit_message>"
echo "Example - feat: adding new loading button"
echo "Allowed commit types: feat, fix, refactor, docs, test, chore, style, build, revert"
exit 1
fi
5 changes: 5 additions & 0 deletions .github/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

composer phpcs
composer phpstan
composer tests
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ composer.phar
/coverage/
/logs/
/site/
.DS_Store
.DS_Store
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

Composer package for request brazilian field validation for Laravel

<p align="center">
<a href="https://github.com/matmper/laravel-brazil-validation/pulls">
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs Welcome">
</a>
<a href="https://github.com/matmper/laravel-brazil-validation/actions/workflows/github_actions.yml?query=branch%3Amain+event%3Apush">
<img src="https://github.com/matmper/laravel-brazil-validation/actions/workflows/github_actions.yml/badge.svg?event=push" alt="License MIT">
</a>
<a href="https://opensource.org/license/mit/" target="_blank">
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License MIT">
</a>
</p>
<a href="https://github.com/matmper/laravel-brazil-validation/pulls" target="_blank">
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=for-the-badge" alt="PRs Welcome">
</a>
<a href="https://packagist.org/packages/matmper/laravel-brazil-validation" target="_blank">
<img src="https://img.shields.io/packagist/v/matmper/laravel-brazil-validation?style=for-the-badge&color=%23b6e673" alt="Packagist">
</a>
<a href="https://github.com/matmper/laravel-brazil-validation/actions/workflows/github_actions.yml?query=branch%3Amain" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/matmper/laravel-brazil-validation/github_actions.yml?branch=main&style=for-the-badge" alt="Github Actions">
</a>
<a href="https://opensource.org/license/mit/" target="_blank">
<img src="https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge" alt="License MIT">
</a>

# Dependences

Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@
"@phpstan",
"@tests"
],
"tests": "@php vendor/bin/phpunit -d memory_limit=256M --do-not-cache-result --configuration phpunit.xml",
"phpcbf": "@php vendor/bin/phpcbf",
"phpcs": "@php vendor/bin/phpcs",
"phpstan": "@php vendor/bin/phpstan analyse --memory-limit 256M",
"post-autoload-dump": "@php vendor/bin/testbench package:discover --ansi"
"phpstan": "@php vendor/bin/phpstan analyse --level 9 ./src ./tests",
"tests": "@php vendor/bin/phpunit -d memory_limit=256M --do-not-cache-result --configuration phpunit.xml",
"post-autoload-dump": [
"@php vendor/bin/testbench package:discover --ansi"
],
"post-install-cmd": [
"git config --local core.hooksPath .github/hooks",
"chmod +x .github/hooks/*"
]
},
"extra": {
"laravel": {
Expand Down
10 changes: 0 additions & 10 deletions phpstan.neon

This file was deleted.

33 changes: 33 additions & 0 deletions src/Contracts/RuleContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* Used for overwrite \Illuminate\Contracts\Validation\Rule;
*/
namespace Matmper\Contracts;

interface RuleContract
{
/**
* Set rule params
*
* @param array<string> $params
* @return self
*/
public function params(array $params): self;

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes(string $attribute, mixed $value): bool;

/**
* Get the validation error message.
*
* @return string|array<string>
*/
public function message(): string|array;
}
19 changes: 13 additions & 6 deletions src/Providers/ValidationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class ValidationProvider extends ServiceProvider
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;

/**
* Indicates if loading of the provider is deferred.
*
Expand All @@ -20,15 +25,17 @@ class ValidationProvider extends ServiceProvider
*/
public function boot(): void
{
/**
* @var array<\Matmper\Contracts\RuleContract> $services
*/
$services = [
'cep' => \Matmper\Rules\CepRule::class,
'document' => \Matmper\Rules\DocumentNumberRule::class,
'not_html' => \Matmper\Rules\NotHtmlRule::class,
'cep' => $this->app->make(\Matmper\Rules\CepRule::class),
'document' => $this->app->make(\Matmper\Rules\DocumentNumberRule::class),
'not_html' => $this->app->make(\Matmper\Rules\NotHtmlRule::class),
];

foreach ($services as $name => $service) {
$class = app($service);

foreach ($services as $name => $class) {
// @phpstan-ignore-next-line
$this->app['validator']->extend($name, function ($attribute, $value, $params) use ($class) {
return $class->params($params)->passes($attribute, $value);
}, $class->message());
Expand Down
24 changes: 10 additions & 14 deletions src/Rules/CepRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Matmper\Rules;

use Illuminate\Contracts\Validation\Rule;
use Matmper\Contracts\RuleContract;

class CepRule implements Rule
class CepRule implements RuleContract
{
/**
* Set true if check mask and value
Expand All @@ -14,9 +14,7 @@ class CepRule implements Rule
private $checkMask;

/**
* Set rule params
*
* @return self
* @inheritDoc
*/
public function params(array $params): self
{
Expand All @@ -28,25 +26,23 @@ public function params(array $params): self
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value): bool
public function passes(string $attribute, mixed $value): bool
{
if ($this->checkMask && !preg_match('/\d{5}\-\d{3}/', $value)) {
return false;
}

return strlen(preg_replace("/[^\d]/", "", $value)) === 8;
return strlen((string) preg_replace("/[^\d]/", "", $value)) === 8;
}

/**
* Get the validation error message.
*
* @return string
* @inheritDoc
*/
public function message()
public function message(): string
{
return ':attribute não é um CEP válido';
}
Expand Down
24 changes: 10 additions & 14 deletions src/Rules/DocumentNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Matmper\Rules;

use Illuminate\Contracts\Validation\Rule;
use Matmper\Contracts\RuleContract;
use Matmper\Enums\DocumentType;

class DocumentNumberRule implements Rule
class DocumentNumberRule implements RuleContract
{
/**
* Set true if check mask and value
Expand All @@ -17,14 +17,12 @@ class DocumentNumberRule implements Rule
/**
* List to documents to validate
*
* @var array
* @var array<string>
*/
private $documents;

/**
* Set rule params
*
* @return self
* @inheritDoc
*/
public function params(array $params): self
{
Expand All @@ -37,11 +35,11 @@ public function params(array $params): self
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value): bool
public function passes(string $attribute, mixed $value): bool
{
$cpfRule = new \Matmper\Services\DocumentCpfService;

Expand All @@ -59,11 +57,9 @@ public function passes($attribute, $value): bool
}

/**
* Get the validation error message.
*
* @return string
* @inheritDoc
*/
public function message()
public function message(): string
{
return ':attribute não é um documento válido';
}
Expand Down
22 changes: 9 additions & 13 deletions src/Rules/NotHtmlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Matmper\Rules;

use Illuminate\Contracts\Validation\Rule;
use Matmper\Contracts\RuleContract;

class NotHtmlRule implements Rule
class NotHtmlRule implements RuleContract
{
/**
* Set rule params
*
* @return self
* @inheritDoc
*/
public function params(array $params): self
{
Expand All @@ -19,11 +17,11 @@ public function params(array $params): self
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value): bool
public function passes(string $attribute, mixed $value): bool
{
if (!is_string($value)) {
throw new \Matmper\Exceptions\ValueIsNotStringException($attribute);
Expand All @@ -37,11 +35,9 @@ public function passes($attribute, $value): bool
}

/**
* Get the validation error message.
*
* @return string
* @inheritDoc
*/
public function message()
public function message(): string
{
return ':attribute não deve conter código HTML';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Services/DocumentCnpjService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class DocumentCnpjService
/**
* Determine if the validation rule passes.
*
* @param mixed $value
* @param string $value
* @return bool
*/
public function passes(mixed $value, bool $checkMask): bool
public function passes(string $value, bool $checkMask): bool
{
$cnpjValue = (string) preg_replace("/[^0-9]/", "", $value);

Expand Down
4 changes: 2 additions & 2 deletions src/Services/DocumentCpfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class DocumentCpfService
/**
* Determine if the validation rule passes.
*
* @param mixed $value
* @param string $value
* @return bool
*/
public function passes(mixed $value, bool $checkMask): bool
public function passes(string $value, bool $checkMask): bool
{
$cpfValue = (string) preg_replace("/[^0-9]/", "", $value);

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CepRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testCepRequest(string $cep, int $assertStatus): void
/**
* Data Provider: testCepRequest
*
* @return array
* @return array<string, array<mixed>>
*/
public function testCepDataProvider(): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Helpers/FakeCepHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FakeCepHelper
*/
public static function cep(bool $mask = false): string
{
$cep = random_int(10000000, 99999999);
$cep = (string) random_int(10000000, 99999999);
return $mask ? MaskHelper::create($cep, '#####-###') : $cep;
}
}
Loading

0 comments on commit 143e3ca

Please sign in to comment.