Lint PHP code psr-12, add GH action

This commit is contained in:
Julien Nahum
2024-02-23 11:54:12 +01:00
parent e85e4df7fe
commit 62971a2ef4
226 changed files with 2338 additions and 2144 deletions

View File

@@ -2,23 +2,22 @@
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;
class FormPropertyLogicRule implements Rule, DataAwareRule
class FormPropertyLogicRule implements DataAwareRule, Rule
{
const ACTIONS_VALUES = [
public const ACTIONS_VALUES = [
'show-block',
'hide-block',
'make-it-optional',
'require-answer',
'enable-block',
'disable-block'
'disable-block',
];
const CONDITION_MAPPING = [
public const CONDITION_MAPPING = [
'text' => [
'comparators' => [
'equals' => [
@@ -43,35 +42,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number'
]
]
'expected_type' => 'number',
],
],
],
'url' => [
'comparators' => [
@@ -97,35 +96,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number'
]
]
'expected_type' => 'number',
],
],
],
'email' => [
'comparators' => [
@@ -151,35 +150,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number'
]
]
'expected_type' => 'number',
],
],
],
'phone_number' => [
'comparators' => [
@@ -205,35 +204,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number'
]
]
'expected_type' => 'number',
],
],
],
'number' => [
'comparators' => [
@@ -259,35 +258,35 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number'
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number'
]
]
'expected_type' => 'number',
],
],
],
'checkbox' => [
'comparators' => [
@@ -295,18 +294,18 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'does_not_equal' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
]
],
],
'select' => [
'comparators' => [
@@ -320,17 +319,17 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
]
]
'values' => [true],
],
],
],
],
'multi_select' => [
'comparators' => [
@@ -338,119 +337,119 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => ['object', 'string'],
'format' => [
'type' => 'uuid',
]
],
],
'does_not_contain' => [
'expected_type' => ['object', 'string'],
'format' => [
'type' => 'uuid',
]
],
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
]
]
'values' => [true],
],
],
],
],
'date' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
'format' => [
'type' => 'date'
]
'type' => 'date',
],
],
'before' => [
'expected_type' => 'string',
'format' => [
'type' => 'date'
]
'type' => 'date',
],
],
'after' => [
'expected_type' => 'string',
'format' => [
'type' => 'date'
]
'type' => 'date',
],
],
'on_or_before' => [
'expected_type' => 'string',
'format' => [
'type' => 'date'
]
'type' => 'date',
],
],
'on_or_after' => [
'expected_type' => 'string',
'format' => [
'type' => 'date'
]
'type' => 'date',
],
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'past_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
'values' => '{}',
],
],
'past_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
'values' => '{}',
],
],
'past_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
'values' => '{}',
],
],
'next_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
'values' => '{}',
],
],
'next_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
'values' => '{}',
],
],
'next_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}'
]
]
]
'values' => '{}',
],
],
],
],
'files' => [
'comparators' => [
@@ -458,56 +457,65 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true]
]
]
]
'values' => [true],
],
],
],
],
];
private $isConditionCorrect = true;
private $isActionCorrect = true;
private $conditionErrors = [];
private $field = [];
private $data = [];
private function checkBaseCondition($condition)
{
if (!isset($condition['value'])) {
if (! isset($condition['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
return;
}
if (!isset($condition['value']['property_meta'])) {
if (! isset($condition['value']['property_meta'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
return;
}
if (!isset($condition['value']['property_meta']['type'])) {
if (! isset($condition['value']['property_meta']['type'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
return;
}
if (!isset($condition['value']['operator'])) {
if (! isset($condition['value']['operator'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
return;
}
if (!isset($condition['value']['value'])) {
if (! isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
@@ -515,15 +523,17 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
$operator = $condition['value']['operator'];
$value = $condition['value']['value'];
if (!isset(self::CONDITION_MAPPING[$typeField])) {
if (! isset(self::CONDITION_MAPPING[$typeField])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
return;
}
if (!isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
if (! isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
return;
}
@@ -536,11 +546,11 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
$foundCorrectType = true;
}
}
if (!$foundCorrectType) {
if (! $foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
if (! $this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
@@ -551,12 +561,13 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
{
if (
($type === 'string' && gettype($value) !== 'string') ||
($type === 'boolean' && !is_bool($value)) ||
($type === 'number' && !is_numeric($value)) ||
($type === 'object' && !is_array($value))
($type === 'boolean' && ! is_bool($value)) ||
($type === 'number' && ! is_numeric($value)) ||
($type === 'object' && ! is_array($value))
) {
return false;
}
return true;
}
@@ -566,25 +577,28 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
$this->conditionErrors[] = 'missing operator';
$this->isConditionCorrect = false;
return;
}
if (isset($conditions['operatorIdentifier']['children'])) {
$this->conditionErrors[] = 'extra condition';
$this->isConditionCorrect = false;
return;
}
if (!is_array($conditions['children'])) {
if (! is_array($conditions['children'])) {
$this->conditionErrors[] = 'wrong sub-condition type';
$this->isConditionCorrect = false;
return;
}
foreach ($conditions['children'] as &$child) {
$this->checkConditions($child);
}
} else if (isset($conditions['identifier'])) {
} elseif (isset($conditions['identifier'])) {
$this->checkBaseCondition($conditions);
}
}
@@ -592,12 +606,12 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
private function checkActions($actions)
{
if (is_array($actions) && count($actions) > 0) {
foreach($actions as $val){
if (!in_array($val, static::ACTIONS_VALUES) ||
(in_array($this->field["type"], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']) && !in_array($val, ['hide-block', 'show-block'])) ||
(isset($this->field["hidden"]) && $this->field["hidden"] && !in_array($val, ['show-block', 'require-answer'])) ||
(isset($this->field["required"]) && $this->field["required"] && !in_array($val, ['make-it-optional', 'hide-block', 'disable-block'])) ||
(isset($this->field["disabled"]) && $this->field["disabled"] && !in_array($val, ['enable-block', 'require-answer', 'make-it-optional']))
foreach ($actions as $val) {
if (! in_array($val, static::ACTIONS_VALUES) ||
(in_array($this->field['type'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']) && ! in_array($val, ['hide-block', 'show-block'])) ||
(isset($this->field['hidden']) && $this->field['hidden'] && ! in_array($val, ['show-block', 'require-answer'])) ||
(isset($this->field['required']) && $this->field['required'] && ! in_array($val, ['make-it-optional', 'hide-block', 'disable-block'])) ||
(isset($this->field['disabled']) && $this->field['disabled'] && ! in_array($val, ['enable-block', 'require-answer', 'make-it-optional']))
) {
$this->isActionCorrect = false;
break;
@@ -618,28 +632,27 @@ class FormPropertyLogicRule implements Rule, DataAwareRule
public function passes($attribute, $value)
{
$this->setProperty($attribute);
if(isset($value["conditions"])){
$this->checkConditions($value["conditions"]);
if (isset($value['conditions'])) {
$this->checkConditions($value['conditions']);
$this->checkActions($value['actions'] ?? null);
}
return ($this->isConditionCorrect && $this->isActionCorrect);
return $this->isConditionCorrect && $this->isActionCorrect;
}
/**
* Get the validation error message.
*
*/
public function message()
{
$message = null;
if (! $this->isConditionCorrect) {
$message = 'The logic conditions for '.$this->field['name'].' are not complete.';
} else if (! $this->isActionCorrect) {
} elseif (! $this->isActionCorrect) {
$message = 'The logic actions for '.$this->field['name'].' are not valid.';
}
if (count($this->conditionErrors) > 0) {
return $message . ' Error detail(s): '.implode(', ', $this->conditionErrors);
return $message.' Error detail(s): '.implode(', ', $this->conditionErrors);
}
return $message;

View File

@@ -25,13 +25,16 @@ class OneEmailPerLine implements Rule
*/
public function passes($attribute, $value)
{
if ($value === null || empty(trim($value))) return true;
if ($value === null || empty(trim($value))) {
return true;
}
foreach (preg_split("/\r\n|\n|\r/", $value) as $email) {
$email = trim($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
}
return true;
}

View File

@@ -3,11 +3,11 @@
namespace App\Rules;
use App\Http\Controllers\Forms\PublicFormController;
use App\Models\Forms\Form;
use App\Service\Storage\StorageFileNameParser;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Models\Forms\Form;
class StorageFile implements Rule
{
@@ -19,7 +19,6 @@ class StorageFile implements Rule
public array $fileTypes;
/**
* @param int $maxSize
* @param string[] $fileTypes
*/
public function __construct(int $maxSize, array $fileTypes = [], public ?Form $form = null)
@@ -36,42 +35,44 @@ class StorageFile implements Rule
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
// If full path then no need to validate
if (filter_var($value, FILTER_VALIDATE_URL) !== FALSE) {
if (filter_var($value, FILTER_VALIDATE_URL) !== false) {
return true;
}
// This is use when updating a record, and file uploads aren't changed.
if($this->form){
if ($this->form) {
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
if(Storage::exists($newPath.'/'.$value)){
if (Storage::exists($newPath.'/'.$value)) {
return true;
}
}
$fileNameParser = StorageFileNameParser::parse($value);
if (!$uuid = $fileNameParser->uuid) {
if (! $uuid = $fileNameParser->uuid) {
return false;
}
$filePath = PublicFormController::TMP_FILE_UPLOAD_PATH.$uuid;
if (!Storage::exists($filePath)) {
if (! Storage::exists($filePath)) {
return false;
}
if (Storage::size($filePath) > $this->maxSize) {
$this->error = 'File is too large.';
return false;
}
if (count($this->fileTypes) > 0) {
$this->error = 'Incorrect file type. Allowed only: '.implode(",", $this->fileTypes);
$this->error = 'Incorrect file type. Allowed only: '.implode(',', $this->fileTypes);
return in_array($fileNameParser->extension, $this->fileTypes);
}
return true;
}

View File

@@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Http;
class ValidHCaptcha implements ImplicitRule
{
const H_CAPTCHA_VERIFY_URL = "https://hcaptcha.com/siteverify";
public const H_CAPTCHA_VERIFY_URL = 'https://hcaptcha.com/siteverify';
private $error = 'Invalid CAPTCHA. Please prove you\'re not a bot.';
@@ -21,13 +21,14 @@ class ValidHCaptcha implements ImplicitRule
public function passes($attribute, $value)
{
if (empty($value)) {
$this->error = "Please complete the captcha.";
$this->error = 'Please complete the captcha.';
return false;
}
return Http::asForm()->post(self::H_CAPTCHA_VERIFY_URL, [
'secret' => config('services.h_captcha.secret_key'),
'response' => $value
'response' => $value,
])->json('success');
}

View File

@@ -3,25 +3,24 @@
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;
class ValidPhoneInputRule implements Rule
{
public ?int $reason = 0;
public function passes($attribute, $value)
{
if (!is_string($value) || !$value) {
if (! is_string($value) || ! $value) {
return false;
}
try {
if(ctype_alpha(substr($value, 0, 2))){ // First 2 will be country code
if (ctype_alpha(substr($value, 0, 2))) { // First 2 will be country code
$value = substr($value, 2);
}
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phone = $phoneUtil->parse($value);
$this->reason = $phoneUtil->isPossibleNumberWithReason($phone);
return $this->reason === \libphonenumber\ValidationResult::IS_POSSIBLE;
} catch (\Exception $e) {
return false;