Skip to content

Commit

Permalink
Code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed May 24, 2021
1 parent 0c0cf1e commit 2dc7b70
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
14 changes: 5 additions & 9 deletions phpstan.neon
Expand Up @@ -9,16 +9,12 @@ parameters:

universalObjectCratesClasses:
- Illuminate\Routing\Route
- ArchTech\SEO\SEOManager

ignoreErrors:
# -
# message: '#Offset (.*?) does not exist on array\|null#'
# paths:
# - tests/*
# -
# message: '#expects resource, resource\|false given#'
# paths:
# - tests/*
# - '#should return \$this#'
- '#SEOManager\|array\|string\|null#'
- '#string\|false given#'
- '#flipp\(\) should return#'
- '#\_\_set\(\) has no return typehint specified#'

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Sep 30, 2021

Contributor

@stancl We've got ourselves a PHPStan bug: phpstan/phpstan#5706
It was discovered while trying to resolve ignored errors using generics.


checkMissingIterableValueType: false
32 changes: 18 additions & 14 deletions src/SEOManager.php
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ArchTech\SEO;

use Illuminate\Support\Str;
Expand Down Expand Up @@ -73,9 +75,10 @@ protected function modify(string $key): string|null
}

/** Set one or more values. */
public function set(string|array $key, string|null $value = null): string|array
public function set(string|array $key, string|null $value = null): string|array|null
{
if (is_array($key)) {
/** @var array<string, string> $key */
foreach ($key as $k => $v) {
$this->set($k, $v);
}
Expand All @@ -84,15 +87,15 @@ public function set(string|array $key, string|null $value = null): string|array
->keys()
->mapWithKeys(fn (string $key) => [$key => $this->get($key)])
->toArray();
} else {
$this->values[$key] = $value;
}

if (Str::contains($key, '.')) {
$this->extension(Str::before($key, '.'), enabled: true);
}
$this->values[$key] = $value;

return $this->get($key);
if (Str::contains($key, '.')) {
$this->extension(Str::before($key, '.'), enabled: true);
}

return $this->get($key);
}

/** Resolve a value. */
Expand Down Expand Up @@ -124,7 +127,7 @@ public function extensions(): array
->filter(fn (bool $enabled) => $enabled)
->keys()
->mapWithKeys(fn (string $extension) => [
$extension => $this->meta("extensions.$extension.view") ?? ("seo::extensions." . $extension)
$extension => $this->meta("extensions.$extension.view") ?? ('seo::extensions.' . $extension),
])
->toArray();
}
Expand Down Expand Up @@ -155,12 +158,13 @@ public function flipp(string $template, string|array $data = null): string|stati
/**
* Get or set metadata.
* @param string|array $key The key or key-value pair being set.
* @param string|array|mixed $value The value (if a single key is provided).
* @return mixed
* @param string|array|null $value The value (if a single key is provided).
* @return $this|string
*/
public function meta(string|array $key, mixed $value = null): mixed
public function meta(string|array $key, string|array $value = null): mixed
{
if (is_array($key)) {
/** @var array<string, string> $key */
foreach ($key as $k => $v) {
$this->meta($k, $v);
}
Expand All @@ -178,7 +182,7 @@ public function meta(string|array $key, mixed $value = null): mixed
}

/** Handle magic method calls. */
public function __call($name, $arguments)
public function __call(string $name, array $arguments): string|array|null|static
{
if (isset($this->extensions[$name])) {
return $this->extension($name, $arguments[0] ?? true);
Expand Down Expand Up @@ -209,13 +213,13 @@ public function __call($name, $arguments)
}

/** Handle magic get. */
public function __get(string $key)
public function __get(string $key): string|null
{
return $this->get(Str::snake($key, '.'));
}

/** Handle magic set. */
public function __set(string $key, mixed $value)
public function __set(string $key, string $value)
{
return $this->set(Str::snake($key, '.'), $value);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SEOServiceProvider.php
Expand Up @@ -4,15 +4,16 @@

namespace ArchTech\SEO;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use ImLiam\BladeHelper\BladeHelperServiceProvider;
use ImLiam\BladeHelper\Facades\BladeHelper;

class SEOServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton('seo', SEOManager::class);
$this->app->register(BladeHelperServiceProvider::class);
}

public function boot(): void
Expand Down
4 changes: 3 additions & 1 deletion src/helpers.php
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

use ArchTech\SEO\SEOManager;

if (! function_exists('seo')) {
function seo(string|array $key = null): SEOManager|string|array|null
{
if (! $key) {
return app('seo');
} else if (is_array($key)) {
} elseif (is_array($key)) {
return app('seo')->set($key);
} else {
return app('seo')->get($key);
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest/FlippTest.php
@@ -1,5 +1,7 @@
<?php

beforeEach(fn () => config(['services.flipp.key' => 'abc']));

test('flipp templates can be set', function () {
seo()->flipp('blog', 'abcdefg');

Expand Down

0 comments on commit 2dc7b70

Please sign in to comment.