Add phpstan support

This commit is contained in:
Christopher C. Wells 2021-01-24 19:43:59 -08:00
parent d1c66924ea
commit 037d20fbb7
23 changed files with 216 additions and 102 deletions

View File

@ -7,20 +7,16 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
* @inheritdoc}
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
* @inheritdoc}
*/
protected function schedule(Schedule $schedule)
{
@ -28,9 +24,7 @@ class Kernel extends ConsoleKernel
}
/**
* Register the commands for the application.
*
* @return void
* @inheritdoc}
*/
protected function commands()
{

View File

@ -8,18 +8,14 @@ use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
* @inheritdoc}
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
* @inheritdoc}
*/
protected $dontFlash = [
'password',
@ -27,9 +23,7 @@ class Handler extends ExceptionHandler
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
* @inheritdoc}
*/
public function register()
{

View File

@ -4,17 +4,15 @@ namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class EmailVerificationNotificationController extends Controller
{
/**
* Send a new email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(Request $request): RedirectResponse
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);

View File

@ -124,43 +124,38 @@ class JournalEntryController extends Controller
// TODO: Improve efficiency? Potential for lots of queries here...
foreach ($ingredients as $ingredient) {
// Prepare entry values.
/**
* @var string $date
* @var string $meal
* @var float $amount
* @var string $unit
* @var int $id
* @var string $type
*/
extract($ingredient, EXTR_OVERWRITE);
$entry_key = "{$date}{$meal}";
$entry_key = "{$ingredient['date']}{$ingredient['meal']}";
$entries[$entry_key] = $entries[$entry_key] ?? JournalEntry::make([
'date' => $date,
'meal' => $meal,
'date' => $ingredient['date'],
'meal' => $ingredient['meal'],
])->user()->associate(Auth::user());
// Calculate amounts based on ingredient type.
if ($type == Food::class) {
$item = Food::whereId($id)->first();
$nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier($item, Number::floatFromString($amount), $unit,);
if ($ingredient['type'] == Food::class) {
$item = Food::whereId($ingredient['id'])->first();
$nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier(
$item,
Number::floatFromString($ingredient['amount']),
$ingredient['unit']
);
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient} =+ $item->{$nutrient} * $nutrient_multiplier;
}
$entries[$entry_key]->foods->add($item);
}
elseif ($type == Recipe::class) {
$item = Recipe::whereId($id)->first();
elseif ($ingredient['type'] == Recipe::class) {
$item = Recipe::whereId($ingredient['id'])->first();
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient} += $item->{"{$nutrient}PerServing"}() * Number::floatFromString($amount);
$entries[$entry_key]->{$nutrient} += $item->{"{$nutrient}PerServing"}() * Number::floatFromString($ingredient['amount']);
}
$entries[$entry_key]->recipes->add($item);
}
else {
return back()->withInput()->withErrors("Invalid ingredient type {$type}.");
return back()->withInput()->withErrors("Invalid ingredient type {$ingredient['type']}.");
}
// Update summary
$entries[$entry_key]->summary .= (!empty($entries[$entry_key]->summary) ? ', ' : null) . "{$amount} {$unit} {$item->name}";
$entries[$entry_key]->summary .= (!empty($entries[$entry_key]->summary) ? ', ' : null) . "{$ingredient['amount']} {$ingredient['unit']} {$item->name}";
}
foreach ($entries as $entry) {

View File

@ -251,14 +251,4 @@ class RecipeController extends Controller
return redirect()->route('recipes.show', $recipe);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Recipe $recipe
* @return \Illuminate\Http\Response
*/
public function destroy(Recipe $recipe)
{
//
}
}

View File

@ -7,11 +7,7 @@ use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
* {@inheritdoc}
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
@ -24,9 +20,7 @@ class Kernel extends HttpKernel
];
/**
* The application's route middleware groups.
*
* @var array
* {@inheritdoc}
*/
protected $middlewareGroups = [
'web' => [
@ -46,11 +40,7 @@ class Kernel extends HttpKernel
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
* {@inheritdoc}
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,

View File

@ -7,15 +7,13 @@ use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
* {@inheritdoc}
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (!$request->expectsJson()) {
return route('login');
}
return null;
}
}

View File

@ -7,9 +7,7 @@ use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
* {@inheritdoc}
*/
protected $except = [
//

View File

@ -7,9 +7,7 @@ use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Mi
class PreventRequestsDuringMaintenance extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
* {@inheritdoc}
*/
protected $except = [
//

View File

@ -7,9 +7,7 @@ use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
* {@inheritdoc}
*/
protected $except = [
'password',

View File

@ -7,9 +7,7 @@ use Illuminate\Http\Middleware\TrustHosts as Middleware;
class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array
* {@inheritdoc}
*/
public function hosts()
{

View File

@ -8,16 +8,12 @@ use Illuminate\Http\Request;
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array|string|null
* {@inheritdoc}
*/
protected $proxies;
/**
* The headers that should be used to detect proxies.
*
* @var int
* {@inheritdoc}
*/
protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

View File

@ -7,9 +7,7 @@ use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
* {@inheritdoc}
*/
protected $except = [
//

View File

@ -66,7 +66,7 @@ use Spatie\Tags\HasTags;
* @method static \Illuminate\Database\Eloquent\Builder|Food withAnyTags($tags, ?string $type = null)
* @method static \Illuminate\Database\Eloquent\Builder|Food withAnyTagsOfAnyType($tags)
*/
class Food extends Model
final class Food extends Model
{
use HasFactory, HasTags, Ingredient, Journalable, Sluggable;

View File

@ -23,8 +23,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read string $amount_formatted
* @property-read Model|\Eloquent $ingredient
* @property-read Model|\Eloquent $parent
* @property-read \App\Models\Food|\App\Models\Recipe $ingredient
* @property-read \App\Models\Recipe|\App\Models\JournalEntry $parent
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount query()
@ -41,7 +41,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value)
* @mixin \Eloquent
*/
class IngredientAmount extends Model
final class IngredientAmount extends Model
{
use HasFactory;

View File

@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
*
* @property int $id
* @property int $user_id
* @property \datetime $date
* @property \Illuminate\Support\Carbon $date
* @property string $summary
* @property float $calories
* @property float $fat
@ -49,7 +49,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients
* @property-read int|null $ingredients_count
*/
class JournalEntry extends Model
final class JournalEntry extends Model
{
use HasFactory;

View File

@ -53,7 +53,7 @@ use Spatie\Tags\HasTags;
* @method static \Illuminate\Database\Eloquent\Builder|Recipe withAnyTags($tags, ?string $type = null)
* @method static \Illuminate\Database\Eloquent\Builder|Recipe withAnyTagsOfAnyType($tags)
*/
class Recipe extends Model
final class Recipe extends Model
{
use HasFactory, HasIngredients, HasTags, Ingredient, Journalable, Sluggable;

View File

@ -27,7 +27,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @method static \Illuminate\Database\Eloquent\Builder|RecipeStep whereUpdatedAt($value)
* @mixin \Eloquent
*/
class RecipeStep extends Model
final class RecipeStep extends Model
{
use HasFactory;

View File

@ -33,7 +33,7 @@ use Illuminate\Notifications\Notifiable;
* @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
* @mixin \Eloquent
*/
class User extends Authenticatable
final class User extends Authenticatable
{
use HasFactory, Notifiable;

View File

@ -41,18 +41,21 @@ class Nutrients
$multiplier = match ($food->serving_unit) {
'tbsp' => 1/3,
'cup' => 1/48,
default => throw new \DomainException(),
};
}
elseif ($fromUnit === 'tbsp') {
$multiplier = match ($food->serving_unit) {
'tsp' => 3,
'cup' => 1/16,
default => throw new \DomainException(),
};
}
elseif ($fromUnit === 'cup') {
$multiplier = match ($food->serving_unit) {
'tsp' => 48,
'tbsp' => 16,
default => throw new \DomainException(),
};
}
else {

View File

@ -24,6 +24,7 @@
"laravel/sail": "^0.0.5",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"nunomaduro/larastan": "^0.6.13",
"phpunit/phpunit": "^9.3.3"
},
"config": {

159
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "346f8fdf213e965dba75a3adbcd0c247",
"content-hash": "7b4bf3adf3d57d7b218fd422cc1e50a9",
"packages": [
{
"name": "asm89/stack-cors",
@ -7480,6 +7480,103 @@
],
"time": "2020-10-29T14:50:40+00:00"
},
{
"name": "nunomaduro/larastan",
"version": "v0.6.13",
"source": {
"type": "git",
"url": "https://github.com/nunomaduro/larastan.git",
"reference": "7a047f7974e6e16d04ee038d86e2c5e6c59e9dfe"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nunomaduro/larastan/zipball/7a047f7974e6e16d04ee038d86e2c5e6c59e9dfe",
"reference": "7a047f7974e6e16d04ee038d86e2c5e6c59e9dfe",
"shasum": ""
},
"require": {
"composer/composer": "^1.0 || ^2.0",
"ext-json": "*",
"illuminate/console": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/container": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/contracts": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/database": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/http": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/pipeline": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"mockery/mockery": "^0.9 || ^1.0",
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^0.12.65",
"symfony/process": "^4.3 || ^5.0"
},
"require-dev": {
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0",
"phpunit/phpunit": "^7.3 || ^8.2 || ^9.3"
},
"suggest": {
"orchestra/testbench": "^4.0 || ^5.0"
},
"type": "phpstan-extension",
"extra": {
"branch-alias": {
"dev-master": "0.6-dev"
},
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"autoload": {
"psr-4": {
"NunoMaduro\\Larastan\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nuno Maduro",
"email": "enunomaduro@gmail.com"
}
],
"description": "Larastan - Discover bugs in your code without running it. A phpstan/phpstan wrapper for Laravel",
"keywords": [
"PHPStan",
"code analyse",
"code analysis",
"larastan",
"laravel",
"package",
"php",
"static analysis"
],
"support": {
"issues": "https://github.com/nunomaduro/larastan/issues",
"source": "https://github.com/nunomaduro/larastan/tree/v0.6.13"
},
"funding": [
{
"url": "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L",
"type": "custom"
},
{
"url": "https://github.com/canvural",
"type": "github"
},
{
"url": "https://github.com/nunomaduro",
"type": "github"
},
{
"url": "https://www.patreon.com/nunomaduro",
"type": "patreon"
}
],
"time": "2021-01-22T12:51:26+00:00"
},
{
"name": "phar-io/manifest",
"version": "2.0.1",
@ -7816,6 +7913,66 @@
},
"time": "2020-12-19T10:15:11+00:00"
},
{
"name": "phpstan/phpstan",
"version": "0.12.69",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "8f436ea35241da33487fd0d38b4bc3e6dfe30ea8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/8f436ea35241da33487fd0d38b4bc3e6dfe30ea8",
"reference": "8f436ea35241da33487fd0d38b4bc3e6dfe30ea8",
"shasum": ""
},
"require": {
"php": "^7.1|^8.0"
},
"conflict": {
"phpstan/phpstan-shim": "*"
},
"bin": [
"phpstan",
"phpstan.phar"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.12-dev"
}
},
"autoload": {
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PHPStan - PHP Static Analysis Tool",
"support": {
"issues": "https://github.com/phpstan/phpstan/issues",
"source": "https://github.com/phpstan/phpstan/tree/0.12.69"
},
"funding": [
{
"url": "https://github.com/ondrejmirtes",
"type": "github"
},
{
"url": "https://www.patreon.com/phpstan",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan",
"type": "tidelift"
}
],
"time": "2021-01-24T14:55:37+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "9.2.5",

8
phpstan.neon.dist Normal file
View File

@ -0,0 +1,8 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
paths:
- app
level: 6
checkMissingIterableValueType: false