I've got a very simple FormRequest
class in a Laravel project. Two methods, both of which return a simple array that's partially populated by a method, but the IDE treats them differently.
<?php
namespace App\Http\Requests;
class VerifyPhoneNumber extends FormRequest {
use Support\ValidatesPhoneNumbers;
public function authorize(): bool {
return true;
}
public function rules(): array {
return [
"phone_number" => $this->getPhoneNumberRules(),
"verify_code" => ["required", "numeric", "max:999999"],
];
}
public function messages(): array {
return [
"phone_number.regex" => $this->getPhoneNumberMessage(),
];
}
}
And the very basic trait methods:
<?php
namespace App\Http\Requests\Support;
trait ValidatesPhoneNumbers {
protected function getPhoneNumberMessage(): string {
return __("Some localized error message");
}
protected function getPhoneNumberRules(): array {
return ["regex:/^\+?1?[2-9][0-9]{5,14}$/", "max:16"];
}
}
The thing I'm puzzled about is that the IDE inspection complains that I should add the JetBrains\PhpStorm\Pure
attribute to the rules()
method, but not the messages()
method.
The comments in the class definition say:
The attribute marks the function that has no impact on the program state or passed parameters used after the function execution. This means that a function call that resolves to such a function can be safely removed if the execution result is not used in code afterwards.
That doesn't really give me any clues why it treats these two methods differently. If I'm understanding the second sentence correctly, when the result of a "pure" method is unused the IDE will flag usage of the method as unused and recommend its removal.
What is the logic used to determine when this attribute is needed?