for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace UniSharp\Uploadable;
use UniSharp\Uploadable\File;
use UniSharp\Uploadable\Image;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
class Uploader
{
const IMAGE_MIME_TYPES = [
'image/gif',
'image/jpeg',
'image/jpg',
'image/png'
];
public static function upload($file): File
$name = $file->getClientOriginalName();
$mime = $file->getMimeType();
$size = $file->getSize();
switch (true) {
TRUE
FALSE
NULL
true
case in_array($mime, static::IMAGE_MIME_TYPES):
$type = 'image';
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
$a = "a"; $ab = "ab"; $abc = "abc";
will produce issues in the first and second line, while this second example
will produce no issues.
$class = Image::class;
break;
default:
$type = 'file';
$class = File::class;
}
$path = $file->store(str_plural($type));
foreach (Config::get("uploadable.plugins.{$type}", []) as $plugin) {
sprintf
$type
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.
// Instead of $x = "foo $bar $baz"; // Better use either $x = "foo " . $bar . " " . $baz; $x = sprintf("foo %s %s", $bar, $baz);
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
$fullPath = $storagePath . DIRECTORY_SEPARATOR . $path;
(new $plugin)->handle($fullPath);
return $class::create(compact('name', 'mime', 'size', 'path'));
This check marks files that end in a newline character, i.e. an empy line.