for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\Traits;
use Illuminate\Support\Str;
use LaravelEnso\Companies\Models\Company;
trait UniqueStringTrait
{
public function unique_random($table, $col, $chars = 16)
$table
If this is a false-positive, you can also ignore this issue in your code via the ignore-unused annotation
ignore-unused
public function unique_random(/** @scrutinizer ignore-unused */ $table, $col, $chars = 16)
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
$unique = false;
// Store tested results in array to not test them again
$tested = [];
do {
// Generate random string of characters
$random = Str::random($chars);
// Check if it's already testing
// If so, don't query the database again
if (in_array($random, $tested)) {
continue;
}
// Check if it is unique in the database
$count = Company::where($col, '=', $random)->count();
// Store the random character in the tested array
// To keep track which ones are already tested
$tested[] = $random;
// String appears to be unique
if ($count === 0) {
// Set unique to true to break the loop
$unique = true;
// If unique is still false at this point
// it will just repeat all the steps until
// it has generated a random string of characters
} while (! $unique);
return $random;
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.