Skip to content

Commit

Permalink
Install PHPStan, perform check
Browse files Browse the repository at this point in the history
  • Loading branch information
olssonm committed Dec 15, 2024
1 parent bfd56fa commit 2b41ed8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
},
"require-dev": {
"orchestra/testbench": ">=6.15",
"squizlabs/php_codesniffer": "^3.5",
"phpunit/phpunit": "<11"
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "<11",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
Expand All @@ -32,6 +33,7 @@
"test": "./vendor/bin/phpunit --no-coverage",
"phpsniff": "vendor/bin/phpcs --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"phpfix": "vendor/bin/phpcbf --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"phpstan": "./vendor/bin/phpstan",
"post-autoload-dump": [
"@php ./vendor/bin/testbench package:discover --ansi"
]
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 8
paths:
- src
ignoreErrors:
- identifier: missingType.generics
14 changes: 7 additions & 7 deletions src/AmpersandServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class AmpersandServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand All @@ -24,7 +24,7 @@ public function boot()
}
}

public function register()
public function register(): void
{
$this->setup();
$this->registerRepositories();
Expand All @@ -33,20 +33,20 @@ public function register()
$this->app->register(RouteServiceProvider::class);
}

private function setup()
private function setup(): void
{
$this->loadViewsFrom(__DIR__ . '/resources/views', 'ampersand');

$this->mergeConfigFrom(__DIR__ . '/config/ampersand.php', 'ampersand');

// Add a disk to the filesystem
$this->app['config']->set('filesystems.disks.ampersand::posts', [
config()->set('filesystems.disks.ampersand::posts', [
'driver' => 'local',
'root' => config('ampersand.posts_path')
]);

// Overload sheets config to avoid local conflicts
$this->app['config']->set('sheets.collections', [
config()->set('sheets.collections', [
'posts' => [
'disk' => 'ampersand::posts',
'sheet_class' => Post::class,
Expand All @@ -57,12 +57,12 @@ private function setup()
]);
}

private function registerRepositories()
private function registerRepositories(): void
{
$this->app->singleton(PostRepository::class);
}

private function registerCommands()
private function registerCommands(): void
{
$this->commands([
NewPost::class
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/NewPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function handle(): void
$file = sprintf('%s.%s.md', $date->format('Y-m-d'), Str::slug($title));

$content = file_get_contents(__DIR__ . '/../resources/stubs/post.md');
$content = Str::of($content)->replace('%title%', $title)
$content = Str::of((string) $content)->replace('%title%', $title)
->replace('%date%', $date);

$directory = config('ampersand.posts_path');
Expand Down
9 changes: 5 additions & 4 deletions src/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Olssonm\Ampersand\Http\Controllers;

use Illuminate\Http\Response;
use Olssonm\Ampersand\Models\Post;
use Olssonm\Ampersand\Repositories\PostRepository;

Expand All @@ -14,19 +15,19 @@ public function __construct(PostRepository $postRepository)
$this->postRepository = $postRepository;
}

public function index()
public function index(): Response
{
return view('ampersand::index', [
return response()->view('ampersand::index', [
'posts' => $this->postRepository->paginate(
config('ampersand.per_page'),
config('ampersand.page_indicator'),
)
]);
}

public function show(Post $post)
public function show(Post $post): Response
{
return view('ampersand::show', [
return response()->view('ampersand::show', [
'post' => $post
]);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace Olssonm\Ampersand\Models;

use AllowDynamicProperties;
use Carbon\Carbon;
use Olssonm\Ampersand\Repositories\PostRepository;
use Olssonm\Ampersand\Services\Model;

/**
* @property string $slug
*/
#[AllowDynamicProperties]
class Post extends Model
{
public function getRouteKey(): string
Expand All @@ -28,8 +33,10 @@ public function getDateAttribute(): Carbon
return Carbon::parse($this->attributes['date']);
}

public static function __callStatic($name, $arguments)
/** @phpstan-ignore-next-line */
public static function __callStatic(string $name, array $arguments): mixed
{
/** @phpstan-ignore-next-line */
return call_user_func_array([app(PostRepository::class), $name], $arguments);
}
}
3 changes: 2 additions & 1 deletion src/Repositories/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function find(string $slug): Post
{
$post = $this->all()->where('slug', $slug)->first();

/** @phpstan-ignore argument.type */
throw_if(!$post, (new ModelNotFoundException())->setModel(Post::class, $slug));

return $post;
}

public function paginate($perPage = 10, $pageName = 'page'): LengthAwarePaginator
public function paginate(int $perPage = 10, string $pageName = 'page'): LengthAwarePaginator
{
$page = LengthAwarePaginator::resolveCurrentPage($pageName, 1);

Expand Down
2 changes: 2 additions & 0 deletions src/Services/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

abstract class Model extends Sheet implements UrlRoutable
{
/** @phpstan-ignore return.missing */
public function resolveRouteBinding($value, $field = null)
{
}

/** @phpstan-ignore return.missing */
public function resolveChildRouteBinding($childType, $value, $field)
{
}
Expand Down

0 comments on commit 2b41ed8

Please sign in to comment.