Fix errors psalm found scanning at level 7

This commit is contained in:
Jonny Barnes 2020-02-21 22:46:48 +00:00
parent e7612c8c31
commit 215b28b056
6 changed files with 97 additions and 100 deletions

View file

@ -6,13 +6,12 @@ namespace App\Http\Controllers;
use App\Exceptions\InvalidTokenException; use App\Exceptions\InvalidTokenException;
use App\Jobs\ProcessMedia; use App\Jobs\ProcessMedia;
use App\Models\{Like, Media, Note, Place}; use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use App\Models\{Media, Place};
use App\Services\Micropub\{HCardService, HEntryService, UpdateService}; use App\Services\Micropub\{HCardService, HEntryService, UpdateService};
use App\Services\TokenService; use App\Services\TokenService;
use Illuminate\Http\File; use Illuminate\Http\{File, JsonResponse, Response, UploadedFile};
use Illuminate\Http\JsonResponse;
use Illuminate\Http\UploadedFile;
use Illuminate\Http\{Request, Response};
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Intervention\Image\Exception\NotReadableException; use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
@ -42,9 +41,9 @@ class MicropubController extends Controller
/** /**
* This function receives an API request, verifies the authenticity * This function receives an API request, verifies the authenticity
* then passes over the info to the relavent Service class. * then passes over the info to the relevant Service class.
* *
* @return \Illuminate\Http\JsonResponse * @return JsonResponse
*/ */
public function post(): JsonResponse public function post(): JsonResponse
{ {
@ -102,11 +101,11 @@ class MicropubController extends Controller
* Respond to a GET request to the micropub endpoint. * Respond to a GET request to the micropub endpoint.
* *
* A GET request has been made to `api/post` with an accompanying * A GET request has been made to `api/post` with an accompanying
* token, here we check wether the token is valid and respond * token, here we check whether the token is valid and respond
* appropriately. Further if the request has the query parameter * appropriately. Further if the request has the query parameter
* synidicate-to we respond with the known syndication endpoints. * syndicate-to we respond with the known syndication endpoints.
* *
* @return \Illuminate\Http\JsonResponse * @return JsonResponse
*/ */
public function get(): JsonResponse public function get(): JsonResponse
{ {
@ -131,7 +130,7 @@ class MicropubController extends Controller
if (request()->has('q') && substr(request()->input('q'), 0, 4) === 'geo:') { if (request()->has('q') && substr(request()->input('q'), 0, 4) === 'geo:') {
preg_match_all( preg_match_all(
'/([0-9\.\-]+)/', '/([0-9.\-]+)/',
request()->input('q'), request()->input('q'),
$matches $matches
); );
@ -158,7 +157,9 @@ class MicropubController extends Controller
/** /**
* Process a media item posted to the media endpoint. * Process a media item posted to the media endpoint.
* *
* @return Illuminate\Http\JsonResponse * @return JsonResponse
* @throws BindingResolutionException
* @throws Exception
*/ */
public function media(): JsonResponse public function media(): JsonResponse
{ {
@ -220,9 +221,9 @@ class MicropubController extends Controller
} }
/** /**
* Return the relavent CORS headers to a pre-flight OPTIONS request. * Return the relevant CORS headers to a pre-flight OPTIONS request.
* *
* @return \Illuminate\Http\Response * @return Response
*/ */
public function mediaOptionsResponse(): Response public function mediaOptionsResponse(): Response
{ {
@ -230,12 +231,12 @@ class MicropubController extends Controller
} }
/** /**
* Get the file type from the mimetype of the uploaded file. * Get the file type from the mime-type of the uploaded file.
* *
* @param string $mimetype * @param string $mimeType
* @return string * @return string
*/ */
private function getFileTypeFromMimeType(string $mimetype): string private function getFileTypeFromMimeType(string $mimeType): string
{ {
//try known images //try known images
$imageMimeTypes = [ $imageMimeTypes = [
@ -246,7 +247,7 @@ class MicropubController extends Controller
'image/tiff', 'image/tiff',
'image/webp', 'image/webp',
]; ];
if (in_array($mimetype, $imageMimeTypes)) { if (in_array($mimeType, $imageMimeTypes)) {
return 'image'; return 'image';
} }
//try known video //try known video
@ -257,7 +258,7 @@ class MicropubController extends Controller
'video/quicktime', 'video/quicktime',
'video/webm', 'video/webm',
]; ];
if (in_array($mimetype, $videoMimeTypes)) { if (in_array($mimeType, $videoMimeTypes)) {
return 'video'; return 'video';
} }
//try known audio types //try known audio types
@ -267,7 +268,7 @@ class MicropubController extends Controller
'audio/ogg', 'audio/ogg',
'audio/x-m4a', 'audio/x-m4a',
]; ];
if (in_array($mimetype, $audioMimeTypes)) { if (in_array($mimeType, $audioMimeTypes)) {
return 'audio'; return 'audio';
} }
@ -289,7 +290,7 @@ class MicropubController extends Controller
/** /**
* Save the details of the micropub request to a log file. * Save the details of the micropub request to a log file.
* *
* @param array $request This is the info from request()->all() * @param array $request This is the info from request()->all()
*/ */
private function logMicropubRequest(array $request) private function logMicropubRequest(array $request)
{ {
@ -301,12 +302,13 @@ class MicropubController extends Controller
/** /**
* Save an uploaded file to the local disk. * Save an uploaded file to the local disk.
* *
* @param \Illuminate\Http\UploadedFele $file * @param UploadedFile $file
* @return string $filename * @return string
* @throws Exception
*/ */
private function saveFile(UploadedFile $file): string private function saveFile(UploadedFile $file): string
{ {
$filename = Uuid::uuid4() . '.' . $file->extension(); $filename = Uuid::uuid4()->toString() . '.' . $file->extension();
Storage::disk('local')->putFileAs('', $file, $filename); Storage::disk('local')->putFileAs('', $file, $filename);
return $filename; return $filename;
@ -315,9 +317,9 @@ class MicropubController extends Controller
/** /**
* Generate a response to be returned when the token has insufficient scope. * Generate a response to be returned when the token has insufficient scope.
* *
* @return \Illuminate\Http\JsonRepsonse * @return JsonResponse
*/ */
private function insufficientScopeResponse() private function insufficientScopeResponse(): JsonResponse
{ {
return response()->json([ return response()->json([
'response' => 'error', 'response' => 'error',
@ -329,9 +331,9 @@ class MicropubController extends Controller
/** /**
* Generate a response to be returned when the token is invalid. * Generate a response to be returned when the token is invalid.
* *
* @return \Illuminate\Http\JsonRepsonse * @return JsonResponse
*/ */
private function invalidTokenResponse() private function invalidTokenResponse(): JsonResponse
{ {
return response()->json([ return response()->json([
'response' => 'error', 'response' => 'error',
@ -343,9 +345,9 @@ class MicropubController extends Controller
/** /**
* Generate a response to be returned when the token has no scope. * Generate a response to be returned when the token has no scope.
* *
* @return \Illuminate\Http\JsonRepsonse * @return JsonResponse
*/ */
private function tokenHasNoScopeResponse() private function tokenHasNoScopeResponse(): JsonResponse
{ {
return response()->json([ return response()->json([
'response' => 'error', 'response' => 'error',

View file

@ -19,7 +19,7 @@ class WebMentionsController extends Controller
* This is probably someone looking for information about what * This is probably someone looking for information about what
* webmentions are, or about my particular implementation. * webmentions are, or about my particular implementation.
* *
* @return \Illuminate\View\View * @return View
*/ */
public function get(): View public function get(): View
{ {
@ -29,11 +29,11 @@ class WebMentionsController extends Controller
/** /**
* Receive and process a webmention. * Receive and process a webmention.
* *
* @return \Illuminate\Http\Respone * @return Response
*/ */
public function receive(): Response public function receive(): Response
{ {
//first we trivially reject requets that lack all required inputs //first we trivially reject requests that lack all required inputs
if ((request()->has('target') !== true) || (request()->has('source') !== true)) { if ((request()->has('target') !== true) || (request()->has('source') !== true)) {
return response( return response(
'You need both the target and source parameters', 'You need both the target and source parameters',

View file

@ -5,28 +5,19 @@ declare(strict_types=1);
namespace App\Models; namespace App\Models;
use App\Exceptions\TwitterContentException; use App\Exceptions\TwitterContentException;
use Cache;
use Codebird\Codebird; use Codebird\Codebird;
use Exception; use Exception;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\{Builder, Model, SoftDeletes};
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\{BelongsTo, BelongsToMany, HasMany, MorphMany};
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jonnybarnes\IndieWeb\Numbers; use Jonnybarnes\IndieWeb\Numbers;
use Laravel\Scout\Searchable; use Laravel\Scout\Searchable;
use League\CommonMark\Block\Element\FencedCode; use League\CommonMark\{CommonMarkConverter, Environment};
use League\CommonMark\Block\Element\IndentedCode; use League\CommonMark\Block\Element\{FencedCode, IndentedCode};
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Ext\Autolink\AutolinkExtension; use League\CommonMark\Ext\Autolink\AutolinkExtension;
use Normalizer; use Normalizer;
use Spatie\CommonMarkHighlighter\FencedCodeRenderer; use Spatie\CommonMarkHighlighter\{FencedCodeRenderer, IndentedCodeRenderer};
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;
use Twitter;
class Note extends Model class Note extends Model
{ {
@ -146,7 +137,7 @@ class Note extends Model
/** /**
* Normalize the note to Unicode FORM C. * Normalize the note to Unicode FORM C.
* *
* @param string|null $value * @param string|null $value
*/ */
public function setNoteAttribute(?string $value) public function setNoteAttribute(?string $value)
{ {
@ -162,7 +153,7 @@ class Note extends Model
/** /**
* Pre-process notes for web-view. * Pre-process notes for web-view.
* *
* @param string|null $value * @param string|null $value
* @return string|null * @return string|null
*/ */
public function getNoteAttribute(?string $value): ?string public function getNoteAttribute(?string $value): ?string
@ -178,9 +169,8 @@ class Note extends Model
$hcards = $this->makeHCards($value); $hcards = $this->makeHCards($value);
$hashtags = $this->autoLinkHashtag($hcards); $hashtags = $this->autoLinkHashtag($hcards);
$html = $this->convertMarkdown($hashtags);
return $html; return $this->convertMarkdown($hashtags);
} }
/** /**
@ -266,7 +256,7 @@ class Note extends Model
} }
/** /**
* Get the pubdate value for RSS feeds. * Get the publish date value for RSS feeds.
* *
* @return string * @return string
*/ */
@ -307,9 +297,9 @@ class Note extends Model
} }
if ($this->location !== null) { if ($this->location !== null) {
$pieces = explode(':', $this->location); $pieces = explode(':', $this->location);
$latlng = explode(',', $pieces[0]); $latLng = explode(',', $pieces[0]);
return (float) trim($latlng[1]); return (float) trim($latLng[1]);
} }
return null; return null;
@ -375,6 +365,7 @@ class Note extends Model
* That is we swap the contacts names for their known Twitter handles. * That is we swap the contacts names for their known Twitter handles.
* *
* @return string * @return string
* @throws TwitterContentException
*/ */
public function getTwitterContentAttribute(): string public function getTwitterContentAttribute(): string
{ {
@ -416,9 +407,9 @@ class Note extends Model
/** /**
* Scope a query to select a note via a NewBase60 id. * Scope a query to select a note via a NewBase60 id.
* *
* @param \Illuminate\Database\Eloquent\Builder $query * @param Builder $query
* @param string $nb60id * @param string $nb60id
* @return \Illuminate\Database\Eloquent\Builder * @return Builder
*/ */
public function scopeNb60(Builder $query, string $nb60id): Builder public function scopeNb60(Builder $query, string $nb60id): Builder
{ {
@ -433,7 +424,7 @@ class Note extends Model
* due to lack of contact info, we assume @username is a twitter handle and link it * due to lack of contact info, we assume @username is a twitter handle and link it
* as such. * as such.
* *
* @param string $text * @param string $text
* @return string * @return string
*/ */
private function makeHCards(string $text): string private function makeHCards(string $text): string
@ -444,7 +435,7 @@ class Note extends Model
return $text; return $text;
} }
$hcards = preg_replace_callback( return preg_replace_callback(
self::USERNAMES_REGEX, self::USERNAMES_REGEX,
function ($matches) { function ($matches) {
if (is_null($this->contacts[$matches[1]])) { if (is_null($this->contacts[$matches[1]])) {
@ -462,24 +453,28 @@ class Note extends Model
}, },
$text $text
); );
return $hcards;
} }
/** /**
* Get the value of the `contacts` property. * Get the value of the `contacts` property.
*
* @return array
*/ */
public function getContacts() public function getContacts(): array
{ {
if ($this->contacts === null) { if ($this->contacts === null) {
$this->setContacts(); $this->setContacts();
} }
return $this->contacts;
} }
/** /**
* Process the note and save the contacts to the `contacts` property. * Process the note and save the contacts to the `contacts` property.
*
* @return void
*/ */
public function setContacts() public function setContacts(): void
{ {
$contacts = []; $contacts = [];
if ($this->getOriginal('note')) { if ($this->getOriginal('note')) {
@ -500,7 +495,7 @@ class Note extends Model
* `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with * `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with
* `rel=tag` set and a `href` of 'section/tagged/' + tagname without the #. * `rel=tag` set and a `href` of 'section/tagged/' + tagname without the #.
* *
* @param string $note * @param string $note
* @return string * @return string
*/ */
public function autoLinkHashtag(string $note): string public function autoLinkHashtag(string $note): string
@ -519,7 +514,7 @@ class Note extends Model
/** /**
* Pass a note through the commonmark library. * Pass a note through the commonmark library.
* *
* @param string $note * @param string $note
* @return string * @return string
*/ */
private function convertMarkdown(string $note): string private function convertMarkdown(string $note): string
@ -536,15 +531,15 @@ class Note extends Model
/** /**
* Do a reverse geocode lookup of a `lat,lng` value. * Do a reverse geocode lookup of a `lat,lng` value.
* *
* @param float $latitude * @param float $latitude
* @param float $longitude * @param float $longitude
* @return string * @return string
*/ */
public function reverseGeoCode(float $latitude, float $longitude): string public function reverseGeoCode(float $latitude, float $longitude): string
{ {
$latlng = $latitude . ',' . $longitude; $latLng = $latitude . ',' . $longitude;
return Cache::get($latlng, function () use ($latlng, $latitude, $longitude) { return Cache::get($latLng, function () use ($latLng, $latitude, $longitude) {
$guzzle = resolve(Client::class); $guzzle = resolve(Client::class);
$response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [ $response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [
'query' => [ 'query' => [
@ -567,7 +562,7 @@ class Note extends Model
. '</span>, <span class="p-country-name">' . '</span>, <span class="p-country-name">'
. $json->address->country . $json->address->country
. '</span>'; . '</span>';
Cache::forever($latlng, $address); Cache::forever($latLng, $address);
return $address; return $address;
} }
@ -577,7 +572,7 @@ class Note extends Model
. '</span>, <span class="p-country-name">' . '</span>, <span class="p-country-name">'
. $json->address->country . $json->address->country
. '</span>'; . '</span>';
Cache::forever($latlng, $address); Cache::forever($latLng, $address);
return $address; return $address;
} }
@ -587,12 +582,12 @@ class Note extends Model
. '</span>, <span class="p-country-name">' . '</span>, <span class="p-country-name">'
. $json->address->country . $json->address->country
. '</span>'; . '</span>';
Cache::forever($latlng, $address); Cache::forever($latLng, $address);
return $address; return $address;
} }
$address = '<span class="p-country-name">' . $json->address->country . '</span>'; $address = '<span class="p-country-name">' . $json->address->country . '</span>';
Cache::forever($latlng, $address); Cache::forever($latLng, $address);
return $address; return $address;
}); });

View file

@ -5,8 +5,8 @@ declare(strict_types=1);
namespace App\Models; namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable; use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\{Builder, Model};
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Phaza\LaravelPostgis\Eloquent\PostgisTrait; use Phaza\LaravelPostgis\Eloquent\PostgisTrait;
@ -52,7 +52,7 @@ class Place extends Model
/** /**
* Define the relationship with Notes. * Define the relationship with Notes.
* *
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return HasMany
*/ */
public function notes() public function notes()
{ {
@ -62,12 +62,12 @@ class Place extends Model
/** /**
* Select places near a given location. * Select places near a given location.
* *
* @param \Illuminate\Database\Eloquent\Builder $query * @param Builder $query
* @param \Phaza\LaravelPostgis\Geometries\Point $point * @param Point $point
* @param int $distance * @param int $distance
* @return \Illuminate\Database\Eloquent\Builder * @return Builder
*/ */
public function scopeNear(Builder $query, Point $point, $distance = 1000): Builder public function scopeNear(Builder $query, Point $point, int $distance = 1000): Builder
{ {
$field = DB::raw( $field = DB::raw(
sprintf( sprintf(
@ -83,9 +83,9 @@ class Place extends Model
/** /**
* Select places based on a URL. * Select places based on a URL.
* *
* @param \Illuminate\Database\Eloquent\Builder $query * @param Builder $query
* @param string $url * @param string $url
* @return \Illuminate\Database\Eloquent\Builder * @return Builder
*/ */
public function scopeWhereExternalURL(Builder $query, string $url): Builder public function scopeWhereExternalURL(Builder $query, string $url): Builder
{ {

View file

@ -15,7 +15,7 @@ class NoteService
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @param string $client * @param string $client
* @return \App\Note * @return Note
*/ */
public function createNote(array $request, ?string $client = null): Note public function createNote(array $request, ?string $client = null): Note
{ {
@ -61,7 +61,7 @@ class NoteService
/** /**
* Get the content from the request to create a new note. * Get the content from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getContent(array $request): ?string private function getContent(array $request): ?string
@ -79,7 +79,7 @@ class NoteService
/** /**
* Get the in-reply-to from the request to create a new note. * Get the in-reply-to from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getInReplyTo(array $request): ?string private function getInReplyTo(array $request): ?string
@ -94,7 +94,7 @@ class NoteService
/** /**
* Get the published time from the request to create a new note. * Get the published time from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getPublished(array $request): ?string private function getPublished(array $request): ?string
@ -113,7 +113,7 @@ class NoteService
/** /**
* Get the location data from the request to create a new note. * Get the location data from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getLocation(array $request): ?string private function getLocation(array $request): ?string
@ -135,8 +135,8 @@ class NoteService
/** /**
* Get the checkin data from the request to create a new note. This will be a Place. * Get the checkin data from the request to create a new note. This will be a Place.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return \App\Models\Place|null * @return Place|null
*/ */
private function getCheckin(array $request): ?Place private function getCheckin(array $request): ?Place
{ {
@ -181,7 +181,7 @@ class NoteService
/** /**
* Get the Swarm URL from the syndication data in the request to create a new note. * Get the Swarm URL from the syndication data in the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getSwarmUrl(array $request): ?string private function getSwarmUrl(array $request): ?string
@ -196,7 +196,7 @@ class NoteService
/** /**
* Get the syndication targets from the request to create a new note. * Get the syndication targets from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return array * @return array
*/ */
private function getSyndicationTargets(array $request): array private function getSyndicationTargets(array $request): array
@ -225,7 +225,7 @@ class NoteService
/** /**
* Get the media URLs from the request to create a new note. * Get the media URLs from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return array * @return array
*/ */
private function getMedia(array $request): array private function getMedia(array $request): array
@ -255,7 +255,7 @@ class NoteService
/** /**
* Get the Instagram photo URL from the request to create a new note. * Get the Instagram photo URL from the request to create a new note.
* *
* @param array $request Data from request()->all() * @param array $request Data from request()->all()
* @return string|null * @return string|null
*/ */
private function getInstagramUrl(array $request): ?string private function getInstagramUrl(array $request): ?string

View file

@ -13,8 +13,8 @@ class PlaceService
/** /**
* Create a place. * Create a place.
* *
* @param array $data * @param array $data
* @return \App\Place * @return Place
*/ */
public function createPlace(array $data): Place public function createPlace(array $data): Place
{ {
@ -39,9 +39,9 @@ class PlaceService
} }
/** /**
* Create a place from a h-card checkin, for exameple from OwnYourSwarm. * Create a place from a h-card checkin, for example from OwnYourSwarm.
* *
* @param array * @param array
* @return Place * @return Place
*/ */
public function createPlaceFromCheckin(array $checkin): Place public function createPlaceFromCheckin(array $checkin): Place