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

@@ -14,7 +14,7 @@ class AiGenerateFormRequest extends FormRequest
public function rules()
{
return [
'form_prompt' => 'required|string|max:1000'
'form_prompt' => 'required|string|max:1000',
];
}
}

View File

@@ -3,22 +3,22 @@
namespace App\Http\Requests;
use App\Models\Forms\Form;
use App\Rules\StorageFile;
use App\Service\Forms\FormLogicPropertyResolver;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use App\Rules\ValidHCaptcha;
use App\Rules\ValidPhoneInputRule;
use App\Rules\ValidUrl;
use App\Service\Forms\FormLogicPropertyResolver;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class AnswerFormRequest extends FormRequest
{
public Form $form;
protected array $requestRules = [];
protected int $maxFileSize;
public function __construct(Request $request)
@@ -34,7 +34,7 @@ class AnswerFormRequest extends FormRequest
*/
public function authorize()
{
return !$this->form->is_closed && !$this->form->max_number_of_submissions_reached && $this->form->visibility === 'public';
return ! $this->form->is_closed && ! $this->form->max_number_of_submissions_reached && $this->form->visibility === 'public';
}
/**
@@ -56,23 +56,24 @@ class AnswerFormRequest extends FormRequest
$selectionFields = collect($this->form->properties)->filter(function ($pro) {
return in_array($pro['type'], ['select', 'multi_select']);
});
foreach ($selectionFields as $field){
if(isset($data[$field['id']]) && is_array($data[$field['id']])){
foreach ($selectionFields as $field) {
if (isset($data[$field['id']]) && is_array($data[$field['id']])) {
$data[$field['id']] = array_map(function ($val) use ($field) {
$tmpop = collect($field[$field['type']]['options'])->first(function ($op) use ($val) {
return ($op['id'] ?? $op['value'] === $val);
return $op['id'] ?? $op['value'] === $val;
});
return isset($tmpop['name']) ? $tmpop['name'] : "";
return isset($tmpop['name']) ? $tmpop['name'] : '';
}, $data[$field['id']]);
}
};
}
if (FormLogicPropertyResolver::isRequired($property, $data)) {
$rules[] = 'required';
if ($property['type'] == 'checkbox') {
// Required for checkboxes means true
$rules[] = 'accepted';
} else if ($property['type'] == 'number' && isset($property['is_rating']) && $property['is_rating']) {
} elseif ($property['type'] == 'number' && isset($property['is_rating']) && $property['is_rating']) {
// For star rating, needs a minimum of 1 star
$rules[] = 'min:1';
}
@@ -107,6 +108,7 @@ class AnswerFormRequest extends FormRequest
/**
* Renames validated fields (because field names are ids)
*
* @return array
*/
public function attributes()
@@ -115,6 +117,7 @@ class AnswerFormRequest extends FormRequest
foreach ($this->form->properties as $property) {
$fields[$property['id']] = $property['name'];
}
return $fields;
}
@@ -127,21 +130,21 @@ class AnswerFormRequest extends FormRequest
{
$messages = [];
foreach ($this->form->properties as $property) {
if($property['type'] == 'date' && isset($property['date_range']) && $property['date_range']){
$messages[$property['id'].'.0.required_with'] = "From date is required";
$messages[$property['id'].'.1.required_with'] = "To date is required";
$messages[$property['id'].'.0.before_or_equal'] = "From date must be before or equal To date";
if ($property['type'] == 'date' && isset($property['date_range']) && $property['date_range']) {
$messages[$property['id'].'.0.required_with'] = 'From date is required';
$messages[$property['id'].'.1.required_with'] = 'To date is required';
$messages[$property['id'].'.0.before_or_equal'] = 'From date must be before or equal To date';
}
if ($property['type'] == 'number' && isset($property['is_rating']) && $property['is_rating']) {
$messages[$property['id'] . '.min'] = "A rating must be selected";
$messages[$property['id'].'.min'] = 'A rating must be selected';
}
}
return $messages;
}
/**
* Return validation rules for a given form property
* @param $property
*/
private function getPropertyRules($property): array
{
@@ -153,27 +156,32 @@ class AnswerFormRequest extends FormRequest
if ($property['is_rating'] ?? false) {
return ['numeric'];
}
return ['numeric'];
case 'select':
case 'multi_select':
if (($property['allow_creation'] ?? false)) {
return ['string'];
}
return [Rule::in($this->getSelectPropertyOptions($property))];
case 'checkbox':
return ['boolean'];
case 'url':
if (isset($property['file_upload']) && $property['file_upload']) {
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, [], $this->form)];
return ['array'];
}
return [new ValidUrl];
return [new ValidUrl()];
case 'files':
$allowedFileTypes = [];
if(!empty($property['allowed_file_types'])){
$allowedFileTypes = explode(",", $property['allowed_file_types']);
if (! empty($property['allowed_file_types'])) {
$allowedFileTypes = explode(',', $property['allowed_file_types']);
}
$this->requestRules[$property['id'].'.*'] = [new StorageFile($this->maxFileSize, $allowedFileTypes, $this->form)];
return ['array'];
case 'email':
return ['email:filter'];
@@ -182,14 +190,17 @@ class AnswerFormRequest extends FormRequest
$this->requestRules[$property['id'].'.*'] = $this->getRulesForDate($property);
$this->requestRules[$property['id'].'.0'] = ['required_with:'.$property['id'].'.1', 'before_or_equal:'.$property['id'].'.1'];
$this->requestRules[$property['id'].'.1'] = ['required_with:'.$property['id'].'.0'];
return ['array', 'min:2'];
}
return $this->getRulesForDate($property);
case 'phone_number':
if (isset($property['use_simple_text_input']) && $property['use_simple_text_input']) {
return ['string'];
}
return ['string', 'min:6', new ValidPhoneInputRule];
return ['string', 'min:6', new ValidPhoneInputRule()];
default:
return [];
}
@@ -199,18 +210,20 @@ class AnswerFormRequest extends FormRequest
{
if (isset($property['disable_past_dates']) && $property['disable_past_dates']) {
return ['date', 'after_or_equal:today'];
}else if (isset($property['disable_future_dates']) && $property['disable_future_dates']) {
} elseif (isset($property['disable_future_dates']) && $property['disable_future_dates']) {
return ['date', 'before_or_equal:today'];
}
return ['date'];
}
private function getSelectPropertyOptions($property): array
{
$type = $property['type'];
if (!isset($property[$type])) {
if (! isset($property[$type])) {
return [];
}
return array_column($property[$type]['options'], 'name');
}
@@ -223,25 +236,26 @@ class AnswerFormRequest extends FormRequest
$receivedValue = $receivedData[$property['id']] ?? null;
// Escape all '\' in select options
if(in_array($property['type'], ['select', 'multi_select']) && !is_null($receivedValue)){
if (in_array($property['type'], ['select', 'multi_select']) && ! is_null($receivedValue)) {
if (is_array($receivedValue)) {
$mergeData[$property['id']] = collect($receivedValue)->map(function ($value) {
$value = Str::of($value);
return $value->replace(
["\e", "\f", "\n", "\r", "\t", "\v", "\\"],
["\\e", "\\f", "\\n", "\\r", "\\t", "\\v", "\\\\"]
["\e", "\f", "\n", "\r", "\t", "\v", '\\'],
['\\e', '\\f', '\\n', '\\r', '\\t', '\\v', '\\\\']
)->toString();
})->toArray();
} else {
$receivedValue = Str::of($receivedValue);
$mergeData[$property['id']] = $receivedValue->replace(
["\e", "\f", "\n", "\r", "\t", "\v", "\\"],
["\\e", "\\f", "\\n", "\\r", "\\t", "\\v", "\\\\"]
["\e", "\f", "\n", "\r", "\t", "\v", '\\'],
['\\e', '\\f', '\\n', '\\r', '\\t', '\\v', '\\\\']
)->toString();
}
}
if($property['type'] === 'phone_number' && (!isset($property['use_simple_text_input']) || !$property['use_simple_text_input']) && $receivedValue && in_array($receivedValue, $countryCodeMapper)){
if ($property['type'] === 'phone_number' && (! isset($property['use_simple_text_input']) || ! $property['use_simple_text_input']) && $receivedValue && in_array($receivedValue, $countryCodeMapper)) {
$mergeData[$property['id']] = null;
}
});

View File

@@ -17,12 +17,14 @@ class StoreFormZapierWebhookRequest extends FormRequest
{
return [
'form_slug' => 'required|exists:forms,slug',
'hook_url' => 'required|string|url'
'hook_url' => 'required|string|url',
];
}
public function instanciateHook() {
public function instanciateHook()
{
$form = Form::whereSlug($this->form_slug)->firstOrFail();
return new FormZapierWebhook([
'form_id' => $form->id,
'hook_url' => $this->hook_url,

View File

@@ -2,9 +2,6 @@
namespace App\Http\Requests;
use App\Models\Forms\Form;
use Illuminate\Validation\Rule;
class StoreFormRequest extends UserFormRequest
{
/**

View File

@@ -7,7 +7,7 @@ use Illuminate\Foundation\Http\FormRequest;
class FormTemplateRequest extends FormRequest
{
const IGNORED_KEYS = [
public const IGNORED_KEYS = [
'id',
'creator',
'cleanings',
@@ -49,9 +49,10 @@ class FormTemplateRequest extends FormRequest
public function rules()
{
$slugRule = '';
if($this->id){
if ($this->id) {
$slugRule = ','.$this->id;
}
return [
'form' => 'required|array',
'publicly_listed' => 'boolean',
@@ -88,7 +89,7 @@ class FormTemplateRequest extends FormRequest
'types' => $this->types ?? [],
'industries' => $this->industries ?? [],
'related_templates' => $this->related_templates ?? [],
'questions' => $this->questions ?? []
'questions' => $this->questions ?? [],
]);
}
}

View File

@@ -4,5 +4,4 @@ namespace App\Http\Requests;
class UpdateFormRequest extends UserFormRequest
{
}

View File

@@ -7,7 +7,7 @@ use Illuminate\Foundation\Http\FormRequest;
class UploadAssetRequest extends FormRequest
{
const FORM_ASSET_MAX_SIZE = 5000000;
public const FORM_ASSET_MAX_SIZE = 5000000;
/**
* Get the validation rules that apply to the request.
@@ -22,7 +22,7 @@ class UploadAssetRequest extends FormRequest
'jpg',
'bmp',
'gif',
'svg'
'svg',
];
if ($this->offsetExists('type') && $this->get('type') === 'files') {
$fileTypes = [];

View File

@@ -1,20 +1,17 @@
<?php
namespace App\Http\Requests;
use App\Http\Requests\Workspace\CustomDomainRequest;
use App\Models\Forms\Form;
use App\Rules\FormPropertyLogicRule;
use App\Rules\OneEmailPerLine;
use Illuminate\Validation\Rule;
use App\Rules\FormPropertyLogicRule;
/**
* Abstract class to validate create/update forms
*
* Class UserFormRequest
* @package App\Http\Requests
*/
abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
{
@@ -30,11 +27,11 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'title' => 'required|string|max:60',
'description' => 'nullable|string|max:2000',
'tags' => 'nullable|array',
'visibility' => ['required',Rule::in(Form::VISIBILITY)],
'visibility' => ['required', Rule::in(Form::VISIBILITY)],
// Notifications
'notifies' => 'boolean',
'notification_emails' => ['required_if:notifies,1', new OneEmailPerLine ],
'notification_emails' => ['required_if:notifies,1', new OneEmailPerLine()],
'send_submission_confirmation' => 'boolean',
'notification_sender' => 'string|max:64',
'notification_subject' => 'string|max:200',
@@ -47,11 +44,11 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'notification_settings' => 'nullable',
// Customization
'theme' => ['required',Rule::in(Form::THEMES)],
'width' => ['required',Rule::in(Form::WIDTHS)],
'theme' => ['required', Rule::in(Form::THEMES)],
'width' => ['required', Rule::in(Form::WIDTHS)],
'cover_picture' => 'url|nullable',
'logo_picture' => 'url|nullable',
'dark_mode' => ['required',Rule::in(Form::DARK_MODE_VALUES)],
'dark_mode' => ['required', Rule::in(Form::DARK_MODE_VALUES)],
'color' => 'required|string',
'hide_title' => 'required|boolean',
'uppercase_labels' => 'required|boolean',
@@ -75,7 +72,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'editable_submissions' => 'boolean|nullable',
'editable_submissions_button_text' => 'string|min:1|max:50',
'confetti_on_submission' => 'boolean',
'auto_save'=> 'boolean',
'auto_save' => 'boolean',
// Properties
'properties' => 'required|array',
@@ -90,7 +87,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'properties.*.required' => 'boolean|nullable',
'properties.*.multiple' => 'boolean|nullable',
'properties.*.timezone' => 'sometimes|nullable',
'properties.*.width' => ['sometimes', Rule::in(['full','1/2','1/3','2/3','1/3','3/4','1/4'])],
'properties.*.width' => ['sometimes', Rule::in(['full', '1/2', '1/3', '2/3', '1/3', '3/4', '1/4'])],
'properties.*.align' => ['sometimes', Rule::in(['left', 'center', 'right', 'justify'])],
'properties.*.allowed_file_types' => 'sometimes|nullable',
'properties.*.use_toggle_switch' => 'boolean|nullable',
@@ -127,7 +124,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
// Custom SEO
'seo_meta' => 'nullable|array',
'custom_domain' => 'sometimes|nullable|regex:'. CustomDomainRequest::CUSTOM_DOMAINS_REGEX,
'custom_domain' => 'sometimes|nullable|regex:'.CustomDomainRequest::CUSTOM_DOMAINS_REGEX,
];
}

View File

@@ -8,10 +8,12 @@ use Illuminate\Http\Request;
class CustomDomainRequest extends FormRequest
{
const CUSTOM_DOMAINS_REGEX = '/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,20}$/';
public const CUSTOM_DOMAINS_REGEX = '/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,20}$/';
public Workspace $workspace;
public array $customDomains = [];
public function __construct(Request $request, Workspace $workspace)
{
$this->workspace = Workspace::findOrFail($request->workspaceId);
@@ -28,13 +30,13 @@ class CustomDomainRequest extends FormRequest
'custom_domains' => [
'present',
'array',
function($attribute, $value, $fail) {
function ($attribute, $value, $fail) {
$errors = [];
$domains = collect($value)->filter(function ($domain) {
return !empty( trim($domain) );
})->each(function($domain) use (&$errors) {
if (!preg_match(self::CUSTOM_DOMAINS_REGEX, $domain)) {
$errors[] = 'Invalid domain: ' . $domain;
return ! empty(trim($domain));
})->each(function ($domain) use (&$errors) {
if (! preg_match(self::CUSTOM_DOMAINS_REGEX, $domain)) {
$errors[] = 'Invalid domain: '.$domain;
}
});
@@ -44,16 +46,17 @@ class CustomDomainRequest extends FormRequest
$limit = $this->workspace->custom_domain_count_limit;
if ($limit && $domains->count() > $limit) {
$fail('You can only add ' . $limit . ' domain(s).');
$fail('You can only add '.$limit.' domain(s).');
}
$this->customDomains = $domains->toArray();
}
]
},
],
];
}
protected function passedValidation(){
protected function passedValidation()
{
$this->replace(['custom_domains' => $this->customDomains]);
}
}