for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Install GitHub App
<?php
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures;
trait CheckUnique
{
/**
* Checks if the given string is unique, and return existing entity.
* @return JSON containing success, message and data.
array
This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.
@return
*/
public function checkUniqueString()
$response = ['success' => false, 'message' => trans('backpack::crud.unique_error'), 'meta' => ['link' => null, 'snippet' => null, 'entity_key' => null]];
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.
$field_name = \Request::input('field_name', null);
$check_value = \Request::input('check_value', null);
$display_name = \Request::input('display_name', null);
if (empty($field_name)) {
$response['message'] = trans('backpack::crud.unique_field_name_missing');
} elseif (empty($check_value) && $check_value !== '0') {
$response['message'] = trans('backpack::crud.unique_check_value_missing');
} else {
$existing_entity = $this->crud->model->where([$field_name => $check_value])->first();
crud
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
if (! $existing_entity) {
$response['success'] = true;
$response['message'] = null;
$response['message'] = $this->crud->entity_name.' '.trans('backpack::crud.unique_exists');
$response['meta'] = [
'link' => url($this->crud->route.'/'.$existing_entity->getKey().'/edit'),
'snippet' => $display_name ? $existing_entity->{$display_name} : $this->crud->entity_name,
'entity_key' => $existing_entity->getKey(),
];
}
return $response;
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.