Hi, we are currently running a self-hosted SonarQube for our Laravel 10 / 11 project but receive some issues on our Laravel-styled code for string literal duplication - especially in request files (namespace App\Http\Requests\
).
- Language: PHP 8.3 + Laravel 10 / 11
- Rule: php:S1192 “String literals should not be duplicated”
- SonarQube Developer Edition v10.6
Example:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateNewUser extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'email' => ['required', 'email:rfc', 'max:100'],
'token' => ['required', 'max:100', new TokenRule()],
'password' => ['required', 'string', new PasswordRule()],
'firstname' => ['required', 'string', 'max:100'],
'surname' => ['required', 'string', 'max:100'],
'company' => ['nullable', 'string', 'max:100'],
];
}
}
After the scan, the max:100
strings are noted as the above issue. You can find this issue as well for various other Laravel parameterized rules like date_format:<format>
, min:<x>
, max:<x>
as in the Laravel Docs Available Validation Rules. In this particular case, I am not convinced that this should be an issue because this is pure Laravel style to define request validation rules. Defining a constant like UserRequest::MAX_100
solves the issue but does not make the code more readable or more maintainable since it’s a default Laravel approach where duplications are okay for good readability. Also, for fixing this issue application-wide would require defining a constant for all possible occurrences. I think it’s some kind of special case Laravel config string, but not a duplicated string.
Since I am a newbie in SonarQube: is there any way to add a string like above to a deny-list? I do not want to add a NOSONAR
to every file or line or disable the rule, to catch other errors in the file or catch real duplicated string in the project. If there is no way, I would request some kind of “ignore-option” or recognition for Laravel-Way styled request files.
Thanks!