Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.0 #170

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1']
php: ['8.1', '8.2']

name: Linting - PHP ${{ matrix.php }}
steps:
Expand Down
29 changes: 14 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@
}
],
"require": {
"php": ">=7.4",
"symfony/asset": "^4.4 || ^5.4 || ^6.0",
"symfony/config": "^4.4 || ^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0",
"symfony/event-dispatcher": "^4.4 || ^5.4 || ^6.0",
"symfony/http-foundation": "^4.4 || ^5.4 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.4 || ^6.0",
"symfony/options-resolver": "^4.4 || ^5.4 || ^6.0",
"symfony/security-core": "^4.4 || ^5.4 || ^6.0",
"symfony/translation": "^4.4 || ^5.4 || ^6.0",
"symfony/twig-bridge": "^4.4 || ^5.4 || ^6.0",
"php": "8.1.*||8.2.*",
"symfony/asset": "^6.0",
"symfony/config": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/event-dispatcher": "^6.0",
"symfony/http-foundation": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/options-resolver": "^6.0",
"symfony/security-core": "^6.0",
"symfony/translation": "^6.0",
"symfony/twig-bridge": "^6.0",
"twig/twig": "^3.0"
},
"require-dev": {
"phpspec/prophecy": "^1.6",
"symfony/framework-bundle" : "^4.4 || ^5.4 || ^6.0",
"symfony/framework-bundle" : "^6.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^1.0",
Expand All @@ -49,8 +48,8 @@
"scripts": {
"tests": "vendor/bin/phpunit tests/",
"phpstan": [
"vendor/bin/phpstan analyse src/ --level=5",
"vendor/bin/phpstan analyse -c tests/phpstan.neon tests/ --level=5"
"vendor/bin/phpstan analyse -c phpstan.neon src/",
"vendor/bin/phpstan analyse -c tests/phpstan.neon tests/"
],
"codestyle": "vendor/bin/php-cs-fixer fix --dry-run --verbose --show-progress=none",
"codestyle-fix": "vendor/bin/php-cs-fixer fix"
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ includes:
- vendor/phpstan/phpstan-symfony/rules.neon

parameters:
level: 9
excludePaths:
- %rootDir%/../../
10 changes: 5 additions & 5 deletions src/DependencyInjection/TablerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yml');

if ($options['knp_menu']['enable'] === true) {
if (\array_key_exists('knp_menu', $options) && \array_key_exists('enable', $options['knp_menu']) && $options['knp_menu']['enable'] === true) {
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config/container'));
$loader->load('knp-menu.yml');
}
Expand All @@ -42,13 +42,13 @@ public function load(array $configs, ContainerBuilder $container): void
/**
* Merge available configuration options, so they are all available for the ContextHelper.
*
* @param array $config
* @return array
* @param array<string, mixed> $config
* @return array<string, array<string, mixed>>
*/
protected function getContextOptions(array $config = []): array
private function getContextOptions(array $config): array
{
$contextOptions = (array) ($config['options'] ?? []);
$contextOptions['knp_menu'] = (array) $config['knp_menu'];
$contextOptions['knp_menu'] = (array) ($config['knp_menu'] ?? []);

return $contextOptions;
}
Expand Down
9 changes: 2 additions & 7 deletions src/Event/MenuEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ class MenuEvent extends ThemeEvent
/**
* @var MenuItemInterface[]
*/
private $menuRootItems = [];
/**
* @var Request
*/
private $request;
private array $menuRootItems = [];

public function __construct(Request $request)
public function __construct(private Request $request)
{
$this->request = $request;
}

public function getRequest(): Request
Expand Down
2 changes: 1 addition & 1 deletion src/Event/NotificationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NotificationEvent extends ThemeEvent
/**
* @var array<NotificationInterface>
*/
private $notifications = [];
private array $notifications = [];

/**
* @param int|null $max
Expand Down
9 changes: 3 additions & 6 deletions src/Event/UserDetailsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
*/
class UserDetailsEvent extends ThemeEvent
{
/**
* @var UserInterface
*/
private $user;
private ?UserInterface $user = null;
/**
* @var MenuItemInterface[]
*/
private $links = [];
private $showLogoutLink = true;
private array $links = [];
private bool $showLogoutLink = true;

public function setUser(UserInterface $user): void
{
Expand Down
40 changes: 26 additions & 14 deletions src/Helper/ContextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@

namespace KevinPapst\TablerBundle\Helper;

/**
* @extends \ArrayObject<string, mixed>
*/
class ContextHelper extends \ArrayObject
{
public function getLogoUrl(): ?string
{
return $this->getOption('logo_url');
$logo = $this->getOption('logo_url');
if (\is_string($logo)) {
return $logo;
}

return null;
}

public function setLogoUrl(?string $logo): void
Expand Down Expand Up @@ -73,7 +81,12 @@ public function setIsDarkMode(bool $isDarkMode): void

public function getAssetVersion(): string
{
return (string) $this->getOption('asset_version');
$version = $this->getOption('asset_version');
if (\is_string($version)) {
return $version;
}

return '1.0';
}

public function setAssetVersion(string $assetVersion): void
Expand Down Expand Up @@ -113,24 +126,28 @@ public function setIsBoxedLayout(bool $boxed): void

public function getSecurityCoverUrl(): string
{
return (string) $this->getOption('security_cover_url');
$cover = $this->getOption('security_cover_url');
if (\is_string($cover)) {
return $cover;
}

return '';
}

public function setSecurityCoverUrl(string $url): void
{
$this->setOption('security_cover_url', $url);
}

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->getArrayCopy();
}

/**
* @param string $name
* @param mixed|null $value
*/
public function setOption(string $name, $value): void
public function setOption(string $name, mixed $value): void
{
$this->offsetSet($name, $value);
}
Expand All @@ -140,12 +157,7 @@ public function hasOption(string $name): bool
return $this->offsetExists($name);
}

/**
* @param string $name
* @param mixed $default
* @return mixed|null
*/
public function getOption(string $name, $default = null)
public function getOption(string $name, mixed $default = null): mixed
{
return $this->offsetExists($name) ? $this->offsetGet($name) : $default;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Model/MenuItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function getTranslationDomain(): string;

public function getRoute(): ?string;

/**
* @return array<string, mixed>
*/
public function getRouteArgs(): array;

public function getIcon(): ?string;
Expand Down
11 changes: 8 additions & 3 deletions src/Model/MenuItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MenuItemModel implements MenuItemInterface
private string $identifier;
private string $label;
private ?string $route;
/** @var array<string, mixed> */
private array $routeArgs;
private ?string $icon;
/** @var array<MenuItemInterface> */
Expand All @@ -26,6 +27,9 @@ class MenuItemModel implements MenuItemInterface
private bool $expanded = false;
private string $translationDomain = 'messages';

/**
* @param array<string, mixed> $routeArgs
*/
public function __construct(
string $identifier,
string $label,
Expand Down Expand Up @@ -78,9 +82,7 @@ public function getIsActive(): bool

public function setIsActive(bool $isActive): void
{
if ($this->hasParent()) {
$this->getParent()->setIsActive($isActive);
}
$this->parent?->setIsActive($isActive);

$this->isActive = $isActive;
}
Expand Down Expand Up @@ -125,6 +127,9 @@ public function getRouteArgs(): array
return $this->routeArgs;
}

/**
* @param array<string, mixed> $routeArgs
*/
public function setRouteArgs(array $routeArgs): void
{
$this->routeArgs = $routeArgs;
Expand Down
10 changes: 2 additions & 8 deletions src/Model/NotificationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

class NotificationModel implements NotificationInterface
{
private $id;
private $message;
private $type;
private $url;
private ?string $url = null;

public function __construct(string $id, string $message, string $type = Constants::TYPE_INFO)
public function __construct(private string $id, private string $message, private string $type = Constants::TYPE_INFO)
Copy link
Collaborator

@cavasinf cavasinf Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of putting class members INSIDE the constructor.

  1. When using PHP Attributes inside constructor parameters it can be messy.
  2. We have encountered a lot of confusing behavior logic when using it that way (in our company).
  3. Even more in a single line! It will be a pain when resolving conflicts on merge! (trust me 😭)

No complaint when using autowire though, just on models like this one.

I'll go:

public function __construct(
    private string $id, 
    private string $message, 
    private string $type = Constants::TYPE_INFO
) {
}

Or back to OLD way

private string $id;
private string $message; 
private string $type;
private ?string $url = null;

public function __construct(
    string $id, 
    string $message, 
    string $type = Constants::TYPE_INFO,
) {
    $this->id = $id;
    $this->message = $message;
    $this->type = $type;
}

Copy link
Collaborator

@cavasinf cavasinf Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using rector to do this PR, we have added the Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector to the skip config. And do this upgrade only where needed.

{
$this->id = $id;
$this->message = $message;
$this->type = $type;
}

public function getIdentifier(): string
Expand Down
11 changes: 1 addition & 10 deletions src/Model/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,16 @@ interface UserInterface
{
/**
* The user identifier for generating links.
*
* @return string
* @deprecated will be renamed to getUserIdentifier() once we raise the minimum SF version to 5
*/
public function getIdentifier(): string;
public function getUserIdentifier(): string;

/**
* Returns the display name (full name, email or whatever you want to use.
*
* @return string
*/
public function getName(): string;

/**
* Additional title that can be displayed next to the user.
*
* @return string|null
*/
public function getTitle(): ?string;

Expand All @@ -38,8 +31,6 @@ public function getTitle(): ?string;
*
* You can change the avatar behaviour and return any other string, you just need
* to overwrite the avatar component at templates/components/avatar_image.html.twig.
*
* @return string|null
*/
public function getAvatar(): ?string;
}
30 changes: 11 additions & 19 deletions src/Model/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,11 @@

final class UserModel implements UserInterface
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $title;
/**
* @var string|null
*/
private $avatar;
private ?string $title = null;
private ?string $avatar = null;

public function __construct(string $id, string $name)
public function __construct(private string $id, private string $name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

{
$this->id = $id;
$this->name = $name;
}

public function getId(): string
Expand Down Expand Up @@ -74,7 +58,15 @@ public function getTitle(): ?string
return $this->title;
}

/**
* @deprecated use getUserIdentifier instead
*/
public function getIdentifier(): string
{
return $this->getUserIdentifier();
}

public function getUserIdentifier(): string
{
if (!empty($this->id)) {
return $this->id;
Expand Down
2 changes: 1 addition & 1 deletion src/TablerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getPath(): string
return \dirname(__DIR__);
}

public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new TwigPass());
Expand Down
Loading
Loading