From 03763cc6ff35e04de9461e84f624ebb2a566d1be Mon Sep 17 00:00:00 2001 From: butschster Date: Sun, 10 Sep 2023 11:08:51 +0400 Subject: [PATCH] Code refactoring - Updates PHP up to 8.0 - Adds github workflow to run unit tests - Removes redundant phpdocs fixes #56 --- .gitattributes | 5 + .github/workflows/phpunit.yml | 52 ++++ composer.json | 14 +- psalm.xml | 14 ++ src/Console/InstallCommand.php | 44 +--- .../Entities/HasVisibilityConditions.php | 7 +- .../MetaTags/Entities/TagInterface.php | 1 - .../MetaTags/Entities/TitleInterface.php | 36 +-- .../MetaTags/GeoMetaInformationInterface.php | 10 +- src/Contracts/MetaTags/MetaInterface.php | 226 +++--------------- .../MetaTags/PlacementsInterface.php | 8 +- .../MetaTags/RobotsTagsInterface.php | 4 +- .../MetaTags/SeoMetaTagsInterface.php | 8 +- src/Contracts/Packages/ManagerInterface.php | 21 +- src/Contracts/Packages/PackageInterface.php | 4 - src/Contracts/WithDependencies.php | 10 +- src/Facades/Meta.php | 7 +- src/Facades/PackageManager.php | 7 +- src/Hydrator/VueMetaHydrator.php | 50 ++-- src/Hydrator/VueMetaResource.php | 46 ++-- src/MetaTags/Concerns/InitializeDefaults.php | 7 +- src/MetaTags/Concerns/ManageAssets.php | 20 +- src/MetaTags/Concerns/ManageLinksTags.php | 53 +--- src/MetaTags/Concerns/ManageMetaTags.php | 82 +------ src/MetaTags/Concerns/ManagePackages.php | 46 +--- src/MetaTags/Concerns/ManagePlacements.php | 21 +- src/MetaTags/Concerns/ManageTitle.php | 18 +- .../Builders/YandexMetrikaCounterBuilder.php | 76 +----- src/MetaTags/Entities/Comment.php | 62 ++--- .../Entities/Concerns/ManageMaxLength.php | 19 +- .../Entities/Concerns/ManagePlacements.php | 18 +- .../Entities/Concerns/ManageVisibility.php | 11 +- src/MetaTags/Entities/ConditionalComment.php | 13 +- src/MetaTags/Entities/Description.php | 26 +- src/MetaTags/Entities/Favicon.php | 45 ++-- src/MetaTags/Entities/GoogleAnalytics.php | 45 ++-- src/MetaTags/Entities/GoogleTagManager.php | 35 +-- src/MetaTags/Entities/JavascriptVariables.php | 137 ++++------- src/MetaTags/Entities/Keywords.php | 29 +-- src/MetaTags/Entities/Script.php | 48 ++-- src/MetaTags/Entities/Style.php | 36 ++- src/MetaTags/Entities/Tag.php | 95 ++------ src/MetaTags/Entities/Title.php | 85 ++----- src/MetaTags/Entities/Webmaster.php | 30 +-- src/MetaTags/Entities/YandexMetrika.php | 49 ++-- src/MetaTags/Meta.php | 95 +++----- src/MetaTags/Placement.php | 10 +- src/MetaTags/PlacementsBag.php | 22 +- src/MetaTags/TagsCollection.php | 23 +- src/MetaTags/Viewport.php | 2 +- src/Packages/Concerns/Dependencies.php | 24 +- src/Packages/Entities/Concerns/ManageMeta.php | 30 +-- src/Packages/Entities/OpenGraphPackage.php | 114 +++------ src/Packages/Entities/TwitterCardPackage.php | 80 ++----- src/Packages/Manager.php | 25 +- src/Packages/Package.php | 24 +- .../MetaTagsApplicationServiceProvider.php | 34 ++- .../MetaTagsBootstrapServiceProvider.php | 25 +- src/Providers/MetaTagsServiceProvider.php | 10 +- tests/MetaTags/DescriptionMetaTagsTest.php | 8 + .../MetaTags/Entities/GoogleAnalyticsTest.php | 7 + .../Entities/GoogleTagManagerTest.php | 7 + .../Entities/JavascriptVariablesTest.php | 49 ++-- tests/MetaTags/Entities/KeywordsTest.php | 16 +- tests/MetaTags/Entities/ScriptTest.php | 10 + tests/MetaTags/Entities/StyleTest.php | 21 +- tests/MetaTags/FaviconMetaTagsTest.php | 9 + tests/MetaTags/TitleMetaTagsTest.php | 11 + 68 files changed, 758 insertions(+), 1578 deletions(-) create mode 100644 .github/workflows/phpunit.yml create mode 100644 psalm.xml diff --git a/.gitattributes b/.gitattributes index 176a458..4f4e21c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,6 @@ * text=auto +/tests export-ignore +/.github export-ignore +.gitignore export-ignore +.travis.yml export-ignore +phpunit.xml export-ignore diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..5572c94 --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,52 @@ +name: run-tests + +on: + pull_request: null + push: + branches: [ master ] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest ] + php: [ 8.0, 8.1, 8.2 ] + stability: [ prefer-stable ] + + name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Validate Composer + run: composer validate + + - name: Get Composer Cache Directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Restore Composer Cache + uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer + + - name: Install Dependencies + uses: nick-invision/retry@v2.8.3 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer update --prefer-dist --no-interaction --no-progress + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 3b41926..057842b 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,17 @@ } ], "require": { - "php": ">=7.1", - "illuminate/config": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/contracts": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/console": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0" + "ext-json": "*", + "php": ">=8.0", + "illuminate/config": "^9.0|^10.0", + "illuminate/support": "^9.0|^10.0", + "illuminate/contracts": "^9.0|^10.0", + "illuminate/console": "^9.0|^10.0" }, "require-dev": { "mockery/mockery": "^1.0", - "phpunit/phpunit": "^7.0|^9.3" + "phpunit/phpunit": "^9.3", + "vimeo/psalm": "^5.15" }, "autoload": { "psr-4": { diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..b8a42df --- /dev/null +++ b/psalm.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 4c2d898..44d78f0 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -7,26 +7,11 @@ class InstallCommand extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'meta-tags:install'; - - /** - * The console command description. - * - * @var string - */ + protected $description = 'Install all of the MetaTags package resources'; - /** - * Execute the console command. - * - * @return void - */ - public function handle() + public function handle(): void { $this->comment('Publishing MetaTags Service Provider...'); $this->callSilent('vendor:publish', ['--tag' => 'meta-tags-provider']); @@ -43,35 +28,34 @@ public function handle() /** * Register the Nova service provider in the application configuration file. - * - * @return void */ - protected function registerServiceProvider() + protected function registerServiceProvider(): void { $namespace = Str::replaceLast('\\', '', $this->getLaravel()->getNamespace()); + /** @psalm-suppress UndefinedFunction */ $config = file_get_contents(config_path('app.php')); - $line = "{$namespace}\Providers\MetaTagsServiceProvider::class"; + $line = $namespace . '\Providers\MetaTagsServiceProvider::class'; if (Str::contains($config, $line)) { $this->warn('Config config/app.php contains MetaTagsServiceProvider. Registration canceled!'); return; } + /** @psalm-suppress UndefinedFunction */ file_put_contents(config_path('app.php'), str_replace( - "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL, - "{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL." {$line},".PHP_EOL, + $namespace . '\Providers\EventServiceProvider::class,'.PHP_EOL, + $namespace . '\Providers\EventServiceProvider::class,'.PHP_EOL.sprintf(' %s,', $line).PHP_EOL, $config )); } /** * Set the proper application namespace on the installed files. - * - * @return void */ - protected function setAppNamespace() + protected function setAppNamespace(): void { + /** @psalm-suppress UndefinedFunction */ $this->setAppNamespaceOn( app_path('Providers/MetaTagsServiceProvider.php'), $this->getLaravel()->getNamespace() @@ -80,12 +64,8 @@ protected function setAppNamespace() /** * Set the namespace on the given file. - * - * @param string $file - * @param string $namespace - * @return void */ - protected function setAppNamespaceOn($file, $namespace) + protected function setAppNamespaceOn(string $file, string $namespace): void { file_put_contents($file, str_replace( 'App\\', @@ -93,4 +73,4 @@ protected function setAppNamespaceOn($file, $namespace) file_get_contents($file) )); } -} \ No newline at end of file +} diff --git a/src/Contracts/MetaTags/Entities/HasVisibilityConditions.php b/src/Contracts/MetaTags/Entities/HasVisibilityConditions.php index 8fe5a5a..31dab7b 100644 --- a/src/Contracts/MetaTags/Entities/HasVisibilityConditions.php +++ b/src/Contracts/MetaTags/Entities/HasVisibilityConditions.php @@ -9,16 +9,11 @@ interface HasVisibilityConditions /** * Check if entity should be rendered - * - * @return bool */ public function isVisible(): bool; /** * Add visibility condition - * @param Closure $condition - * - * @return $this */ - public function visibleWhen(Closure $condition); + public function visibleWhen(Closure $condition): self; } diff --git a/src/Contracts/MetaTags/Entities/TagInterface.php b/src/Contracts/MetaTags/Entities/TagInterface.php index 63dc222..49697a0 100644 --- a/src/Contracts/MetaTags/Entities/TagInterface.php +++ b/src/Contracts/MetaTags/Entities/TagInterface.php @@ -9,7 +9,6 @@ interface TagInterface extends Htmlable, Arrayable { /** * Get tag placement (footer, head, ...) - * @return string */ public function getPlacement(): string; } diff --git a/src/Contracts/MetaTags/Entities/TitleInterface.php b/src/Contracts/MetaTags/Entities/TitleInterface.php index 083057d..6976d25 100644 --- a/src/Contracts/MetaTags/Entities/TitleInterface.php +++ b/src/Contracts/MetaTags/Entities/TitleInterface.php @@ -7,46 +7,34 @@ interface TitleInterface extends TagInterface /** * Specify max length of the title * - * @param int $maxLength - * - * @return $this + * @param positive-int $maxLength */ - public function setMaxLength(int $maxLength); + public function setMaxLength(int $maxLength): self; /** * Set the main title * - * @param string|null $title - * @param int|null $maxLength - * - * @return $this + * @param positive-int|null $maxLength */ - public function setTitle(?string $title, ?int $maxLength = null); + public function setTitle(?string $title, ?int $maxLength = null): self; /** * Prepend next part of title - * - * @param string $text - * - * @return $this */ - public function prepend(string $text); + public function prepend(string $text): self; /** * Toggle RTL mode - * - * @param bool $status - * - * @return $this */ - public function rtl(bool $status = true); + public function rtl(bool $status = true): self; /** * Determine separator among title parts - * - * @param string $separator - * - * @return $this */ - public function setSeparator(string $separator); + public function setSeparator(string $separator): self; + + /** + * Get the title + */ + public function getTitle(): string; } diff --git a/src/Contracts/MetaTags/GeoMetaInformationInterface.php b/src/Contracts/MetaTags/GeoMetaInformationInterface.php index 0659a14..6844b77 100644 --- a/src/Contracts/MetaTags/GeoMetaInformationInterface.php +++ b/src/Contracts/MetaTags/GeoMetaInformationInterface.php @@ -6,29 +6,21 @@ interface GeoMetaInformationInterface { /** * Get the latitude - * - * @return string */ public function latitude(): string; /** * Get the longitude - * - * @return string */ public function longitude(): string; /** * Get the Place Name - * - * @return string|null */ public function placename(): ?string; /** * Get the region - * - * @return string|null */ public function region(): ?string; -} \ No newline at end of file +} diff --git a/src/Contracts/MetaTags/MetaInterface.php b/src/Contracts/MetaTags/MetaInterface.php index 83f210e..d24583c 100644 --- a/src/Contracts/MetaTags/MetaInterface.php +++ b/src/Contracts/MetaTags/MetaInterface.php @@ -4,7 +4,6 @@ use Butschster\Head\Contracts\MetaTags\Entities\TagInterface; use Butschster\Head\Contracts\Packages\PackageInterface; -use Butschster\Head\MetaTags\Concerns\ManageMetaTags; use Butschster\Head\MetaTags\Meta; use Butschster\Head\MetaTags\TagsCollection; use Illuminate\Contracts\Pagination\Paginator; @@ -15,11 +14,8 @@ interface MetaInterface extends Htmlable, PlacementsInterface, Arrayable { /** * Set meta information from object - * - * @param object $object - * @return $this */ - public function setMetaFrom($object); + public function setMetaFrom(object $object): self; /** * Set the meta title @@ -31,30 +27,19 @@ public function setMetaFrom($object); * titles under 60 characters, our research suggests that you can expect about 90% of your titles to display * properly. * - * @param string $title - * @param int|null $maxLength - * - * @return $this + * @param positive-int|null $maxLength */ - public function setTitle(string $title, int $maxLength = null); + public function setTitle(string $title, int $maxLength = null): self; /** * Prepend title part to default title - * - * @param string $text - * - * @return $this */ - public function prependTitle(string $text); + public function prependTitle(string $text): self; /** * Set the title separator - * - * @param string $separator - * - * @return $this */ - public function setTitleSeparator(string $separator); + public function setTitleSeparator(string $separator): self; /** * Set the meta description @@ -65,17 +50,12 @@ public function setTitleSeparator(string $separator); * the user's attention. Sell the page — get them to click on the result. Here's a great article on meta * descriptions that goes into more detail. * - * @param string $description - * @param int|null $maxLength - * - * @return $this + * @param positive-int|null $maxLength */ - public function setDescription(string $description, ?int $maxLength = null); + public function setDescription(string $description, ?int $maxLength = null): self; /** * Get the meta description - * - * @return TagInterface|null */ public function getDescription(): ?TagInterface; @@ -83,16 +63,12 @@ public function getDescription(): ?TagInterface; * Set the meta keywords * * @param string|array $keywords - * @param int|null $maxLength - * - * @return $this + * @param positive-int|null $maxLength */ - public function setKeywords($keywords, ?int $maxLength = null); + public function setKeywords($keywords, ?int $maxLength = null): self; /** * Get the meta keywords - * - * @return TagInterface|null */ public function getKeywords(): ?TagInterface; @@ -104,17 +80,11 @@ public function getKeywords(): ?TagInterface; * you want to change one of those two commands that you need to add meta robots. Therefore, if you want to noindex * but follow the links on the page, you would add the following tag with only the noindex, as the follow is * implied. Only change what you want to be different from the norm. - * - * @param string $behavior - * - * @return $this */ - public function setRobots(string $behavior); + public function setRobots(string $behavior): self; /** * Get the meta robots - * - * @return TagInterface|null */ public function getRobots(): ?TagInterface; @@ -124,17 +94,11 @@ public function getRobots(): ?TagInterface; * This tag is necessary to declare your character set for the page and should be present on every page. Leaving * this out could impact how your page renders in the browser. A few options are listed below, but your web * designer should know what's best for your site. - * - * @param string $type - * @param string $charset - * @return $this */ - public function setContentType(string $type, string $charset = 'utf-8'); + public function setContentType(string $type, string $charset = 'utf-8'): self; /** * Get Meta content type - * - * @return TagInterface|null */ public function getContentType(): ?TagInterface; @@ -143,17 +107,11 @@ public function getContentType(): ?TagInterface; * * In this mobile world, you should be specifying the viewport. If you don’t, you run the risk of having a * poor mobile experience - * - * @param string $viewport - * - * @return $this */ - public function setViewport(string $viewport); + public function setViewport(string $viewport): self; /** * Get Viewport - * - * @return TagInterface|null */ public function getViewport(): ?TagInterface; @@ -162,17 +120,11 @@ public function getViewport(): ?TagInterface; * * The rel="next" and rel="prev" link attributes are used to indicate the relations between a sequence of pages * to search engines. - * - * @param string $url - * - * @return $this */ - public function setPrevHref(string $url); + public function setPrevHref(string $url): self; /** * Get the prev link tag - * - * @return TagInterface|null */ public function getPrevHref(): ?TagInterface; @@ -181,44 +133,28 @@ public function getPrevHref(): ?TagInterface; * * The rel="next" and rel="prev" link attributes are used to indicate the relations between a sequence of pages * to search engines. - * - * @param string $url - * - * @return $this */ - public function setNextHref(string $url); + public function setNextHref(string $url): self; /** * Get the next link tag - * - * @return TagInterface|null */ public function getNextHref(): ?TagInterface; /** * Set the canonical link - * - * @param string $url - * - * @return $this */ - public function setCanonical(string $url); + public function setCanonical(string $url): self; /** * Get the canonical link tag - * - * @return TagInterface|null */ public function getCanonical(): ?TagInterface; /** * Set canonical link, prev and next from paginator object - * - * @param Paginator $paginator - * - * @return $this */ - public function setPaginationLinks(Paginator $paginator); + public function setPaginationLinks(Paginator $paginator): self; /** * Set a hreflang link @@ -226,214 +162,120 @@ public function setPaginationLinks(Paginator $paginator); * If you've got a website that's available in multiple languages, you want search engines to show your content * to the right audiences. In order to help search engines do so, you should use the hreflang attribute to indicate * the language that content is in, and optionally also what region it's meant for. - * - * @param string $lang - * @param string $url - * - * @return $this */ - public function setHrefLang(string $lang, string $url); + public function setHrefLang(string $lang, string $url): self; /** * Get the hreflang link tag - * - * @param string $lang - * - * @return TagInterface|null */ public function getHrefLang(string $lang): ?TagInterface; - /** - * @param GeoMetaInformationInterface $geo - * - * @return $this - */ - public function setGeo(GeoMetaInformationInterface $geo); + public function setGeo(GeoMetaInformationInterface $geo): self; /** * Specify the character encoding for the HTML document - * - * @param string $charset - * - * @return $this */ - public function setCharset(string $charset = 'utf-8'); + public function setCharset(string $charset = 'utf-8'): self; /** * Get the character encoding tag - * - * @return TagInterface|null */ public function getCharset(): ?TagInterface; /** * Add a favicon tag - * - * @param string $href - * @param array $attributes - * - * @return $this */ - public function setFavicon(string $href, array $attributes = []); + public function setFavicon(string $href, array $attributes = []): self; /** * Add webmaster tag. * * @param string $service Supported services [google, yandex, pinterest, alexa, bing] - * @param string $content - * - * @return $this */ - public function addWebmaster(string $service, string $content); + public function addWebmaster(string $service, string $content): self; /** * Create a custom link tag - * - * @param string $name - * @param array $attributes - * - * @return $this */ - public function addLink(string $name, array $attributes); + public function addLink(string $name, array $attributes): self; /** * Create a custom meta tag - * - * @param string $name - * @param array $attributes - * @param bool $checkNameAttribute - * - * @return $this */ - public function addMeta(string $name, array $attributes, bool $checkNameAttribute = true); + public function addMeta(string $name, array $attributes, bool $checkNameAttribute = true): self; /** * Add a custom tag - * - * @param string $name - * @param TagInterface $tag - * @param string|null $placement - * - * @return $this */ - public function addTag(string $name, TagInterface $tag, ?string $placement = null); + public function addTag(string $name, TagInterface $tag, ?string $placement = null): self; /** * Register tags from collection - * - * @param TagsCollection $tags - * @param string|null $placement - * - * @return $this */ - public function registerTags(TagsCollection $tags, ?string $placement = null); + public function registerTags(TagsCollection $tags, ?string $placement = null): self; /** * Get the tag by name - * - * @param string $name - * @return TagInterface|null */ public function getTag(string $name): ?TagInterface; /** * Remove tag by name - * - * @param string $name - * - * @return $this */ - public function removeTag(string $name); + public function removeTag(string $name): self; /** * Set a link to css file - * - * @param string $name - * @param string $src - * @param array $attributes - * - * @return $this */ - public function addStyle(string $name, string $src, array $attributes = []); + public function addStyle(string $name, string $src, array $attributes = []): self; /** * Set a link to script file - * - * @param string $name - * @param string $src - * @param array $attributes - * @param string $placement - * - * @return $this */ - public function addScript(string $name, string $src, array $attributes = [], string $placement = Meta::PLACEMENT_FOOTER); + public function addScript(string $name, string $src, array $attributes = [], string $placement = Meta::PLACEMENT_FOOTER): self; /** * Add the CSRF token tag. - * - * @return $this */ - public function addCsrfToken(); + public function addCsrfToken(): self; /** * Remove all tags - * - * @return $this */ - public function reset(); + public function reset(): self; /** * Get head meta tags placement bag - * - * @return PlacementInterface */ public function head(): PlacementInterface; /** * Get footer meta tags placement bag - * - * @return PlacementInterface */ public function footer(): PlacementInterface; /** * Include required packages - * - * @param array|string $packages - * - * @return $this */ - public function includePackages($packages); + public function includePackages($packages): self; /** * Register a new package and register all tags from this package - * - * @param PackageInterface $package - * - * @return $this */ - public function registerPackage(PackageInterface $package); + public function registerPackage(PackageInterface $package): self; /** * Replace package with a new one with the same name - * @param PackageInterface $package - * - * @return $this */ - public function replacePackage(PackageInterface $package); + public function replacePackage(PackageInterface $package): self; /** * Remove package by name - * @param string $name - * - * @return $this */ - public function removePackage(string $name); + public function removePackage(string $name): self; /** * Find package by name - * @param string $name Package name - * @return PackageInterface|null */ public function getPackage(string $name): ?PackageInterface; } diff --git a/src/Contracts/MetaTags/PlacementsInterface.php b/src/Contracts/MetaTags/PlacementsInterface.php index 1cb94a0..86c8f9a 100644 --- a/src/Contracts/MetaTags/PlacementsInterface.php +++ b/src/Contracts/MetaTags/PlacementsInterface.php @@ -6,18 +6,12 @@ interface PlacementsInterface { /** * Get specific placement by name - * - * @param string $name - * - * @return PlacementInterface */ public function placement(string $name): PlacementInterface; /** * Get all registered placements - * - * @return array */ public function getPlacements(): array; -} \ No newline at end of file +} diff --git a/src/Contracts/MetaTags/RobotsTagsInterface.php b/src/Contracts/MetaTags/RobotsTagsInterface.php index d346cca..fbdab77 100644 --- a/src/Contracts/MetaTags/RobotsTagsInterface.php +++ b/src/Contracts/MetaTags/RobotsTagsInterface.php @@ -6,8 +6,6 @@ interface RobotsTagsInterface { /** * Get the meta robots - * - * @return string|null */ public function getRobots(): ?string; -} \ No newline at end of file +} diff --git a/src/Contracts/MetaTags/SeoMetaTagsInterface.php b/src/Contracts/MetaTags/SeoMetaTagsInterface.php index 797df4c..bc99efb 100644 --- a/src/Contracts/MetaTags/SeoMetaTagsInterface.php +++ b/src/Contracts/MetaTags/SeoMetaTagsInterface.php @@ -6,22 +6,16 @@ interface SeoMetaTagsInterface { /** * Get the meta title - * - * @return string|null */ public function getTitle(): ?string; /** * Get the meta description - * - * @return string|null */ public function getDescription(): ?string; /** * Get the meta keywords - * - * @return string|array|null */ public function getKeywords(); -} \ No newline at end of file +} diff --git a/src/Contracts/Packages/ManagerInterface.php b/src/Contracts/Packages/ManagerInterface.php index dc2a89f..2a3610f 100644 --- a/src/Contracts/Packages/ManagerInterface.php +++ b/src/Contracts/Packages/ManagerInterface.php @@ -8,36 +8,21 @@ interface ManagerInterface { /** * Create a new package - * - * @param string $name - * @param Closure $callback - * - * @return $this */ - public function create(string $name, Closure $callback); + public function create(string $name, Closure $callback): self; /** * Register the package - * - * @param PackageInterface $package - * - * @return $this */ - public function register(PackageInterface $package); + public function register(PackageInterface $package): self; /** * Get all registered packages - * - * @return array */ public function getPackages(): array; /** * Get registered package by name - * - * @param string $name - * - * @return PackageInterface|null */ public function getPackage(string $name): ?PackageInterface; -} \ No newline at end of file +} diff --git a/src/Contracts/Packages/PackageInterface.php b/src/Contracts/Packages/PackageInterface.php index 996cc99..fcf3a51 100644 --- a/src/Contracts/Packages/PackageInterface.php +++ b/src/Contracts/Packages/PackageInterface.php @@ -10,15 +10,11 @@ interface PackageInterface extends Htmlable, Arrayable { /** * Get the package name - * - * @return string */ public function getName(): string; /** * Get the collection of tags - * - * @return TagsCollection */ public function getTags(): TagsCollection; } diff --git a/src/Contracts/WithDependencies.php b/src/Contracts/WithDependencies.php index 5da9b69..fe31414 100644 --- a/src/Contracts/WithDependencies.php +++ b/src/Contracts/WithDependencies.php @@ -4,13 +4,7 @@ interface WithDependencies { - /** - * @return array - */ public function getDependencies(): array; - /** - * @return bool - */ - public function hasDependencies(); -} \ No newline at end of file + public function hasDependencies(): bool; +} diff --git a/src/Facades/Meta.php b/src/Facades/Meta.php index d248f84..f9efb78 100644 --- a/src/Facades/Meta.php +++ b/src/Facades/Meta.php @@ -12,12 +12,7 @@ */ class Meta extends Facade { - /** - * Get the registered name of the component. - * - * @return string - */ - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { return MetaInterface::class; } diff --git a/src/Facades/PackageManager.php b/src/Facades/PackageManager.php index 5caec82..67fec60 100644 --- a/src/Facades/PackageManager.php +++ b/src/Facades/PackageManager.php @@ -12,12 +12,7 @@ */ class PackageManager extends Facade { - /** - * Get the registered name of the component. - * - * @return string - */ - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { return ManagerInterface::class; } diff --git a/src/Hydrator/VueMetaHydrator.php b/src/Hydrator/VueMetaHydrator.php index 95a6c64..f497b44 100644 --- a/src/Hydrator/VueMetaHydrator.php +++ b/src/Hydrator/VueMetaHydrator.php @@ -1,4 +1,5 @@ idKey = $idKey; + public function __construct( + private string $idKey = 'hid', + ) { $this->resource = new VueMetaResource(); } + /** + * @throws \JsonException + */ public function hydrate(MetaInterface $meta): array { $array = $meta->head()->toArray(); @@ -35,7 +31,10 @@ public function hydrate(MetaInterface $meta): array return $this->resource->toArray(); } - private function format(array $item) + /** + * @throws \JsonException + */ + private function format(array $item): void { if (!isset($item['tag'])) { return; @@ -46,33 +45,40 @@ private function format(array $item) switch ($tag) { case 'title': - return $this->formatTitle($item); + $this->formatTitle($item); + break; case 'meta': - return $this->formatMeta($item); + $this->formatMeta($item); + break; case 'link': - return $this->formatLink($item); + $this->formatLink($item); + break; case 'script': - return $this->formatScript($item); + $this->formatScript($item); + break; } } - private function formatTitle(array $item) + private function formatTitle(array $item): void { $this->resource->setTitle($item['content']); } - private function formatMeta(array $item) + /** + * @throws \JsonException + */ + private function formatMeta(array $item): void { - $item[$this->idKey] = $item['name'] ?? md5(json_encode($item)); + $item[$this->idKey] = $item['name'] ?? md5(json_encode($item, JSON_THROW_ON_ERROR)); $this->resource->appendMeta($item); } - private function formatLink(array $item) + private function formatLink(array $item): void { $this->resource->appendLink($item); } - private function formatScript(array $item) + private function formatScript(array $item): void { $this->resource->appendScript($item); } diff --git a/src/Hydrator/VueMetaResource.php b/src/Hydrator/VueMetaResource.php index 3af865b..78afc8e 100644 --- a/src/Hydrator/VueMetaResource.php +++ b/src/Hydrator/VueMetaResource.php @@ -1,48 +1,37 @@ title = $title; } - public function appendLink(array $link) + public function appendLink(array $link): void { $this->link[] = $link; } - public function appendScript(array $script) + public function appendScript(array $script): void { $this->script[] = $script; } - public function appendMeta(array $meta) + public function appendMeta(array $meta): void { $this->meta[] = $meta; } @@ -53,7 +42,12 @@ public function toArray() 'title' => $this->title, 'meta' => $this->meta, 'link' => $this->link, - 'script' => $this->script + 'script' => $this->script, ]; } + + public function jsonSerialize(): array + { + return $this->toArray(); + } } diff --git a/src/MetaTags/Concerns/InitializeDefaults.php b/src/MetaTags/Concerns/InitializeDefaults.php index 4c4623a..c127821 100644 --- a/src/MetaTags/Concerns/InitializeDefaults.php +++ b/src/MetaTags/Concerns/InitializeDefaults.php @@ -4,10 +4,7 @@ trait InitializeDefaults { - /** - * @return $this - */ - public function initialize() + public function initialize(): self { if (!empty($charset = $this->config('charset'))) { $this->setCharset($charset); @@ -16,7 +13,7 @@ public function initialize() if (!empty($viewport = $this->config('viewport'))) { $this->setViewport($viewport); } - + if (!empty($title = $this->config('title.default'))) { $this->setTitle($title); } diff --git a/src/MetaTags/Concerns/ManageAssets.php b/src/MetaTags/Concerns/ManageAssets.php index 7e133c3..910f30d 100644 --- a/src/MetaTags/Concerns/ManageAssets.php +++ b/src/MetaTags/Concerns/ManageAssets.php @@ -8,25 +8,23 @@ trait ManageAssets { - /** - * @inheritdoc - */ - public function addStyle(string $name, string $src, array $attributes = []) + public function addStyle(string $name, string $src, array $attributes = []): self { return $this->addTag( $name, - new Style($name, $src, $attributes) + new Style($name, $src, $attributes), ); } - /** - * @inheritdoc - */ - public function addScript(string $name, string $src, array $attributes = [], string $placement = Meta::PLACEMENT_FOOTER) - { + public function addScript( + string $name, + string $src, + array $attributes = [], + string $placement = Meta::PLACEMENT_FOOTER, + ): self { return $this->addTag( $name, - new Script($name, $src, $attributes, $placement) + new Script($name, $src, $attributes, $placement), ); } } diff --git a/src/MetaTags/Concerns/ManageLinksTags.php b/src/MetaTags/Concerns/ManageLinksTags.php index 9d16edf..3ed1bb2 100644 --- a/src/MetaTags/Concerns/ManageLinksTags.php +++ b/src/MetaTags/Concerns/ManageLinksTags.php @@ -9,10 +9,7 @@ trait ManageLinksTags { - /** - * @inheritdoc - */ - public function setPrevHref(?string $url) + public function setPrevHref(?string $url): self { if (!$url) { return $this; @@ -24,18 +21,12 @@ public function setPrevHref(?string $url) ]); } - /** - * @inheritdoc - */ public function getPrevHref(): ?TagInterface { return $this->getTag('prev_href'); } - /** - * @inheritdoc - */ - public function setNextHref(?string $url) + public function setNextHref(?string $url): self { if (!$url) { return $this; @@ -47,40 +38,24 @@ public function setNextHref(?string $url) ]); } - /** - * @inheritdoc - */ public function getNextHref(): ?TagInterface { return $this->getTag('next_href'); } - /** - * @inheritdoc - */ - public function setCanonical(string $url) + public function setCanonical(string $url): self { return $this->addLink('canonical', [ 'href' => strip_tags($url), ]); } - /** - * @inheritdoc - */ public function getCanonical(): ?TagInterface { return $this->getTag('canonical'); } - /** - * Set canonical link, prev and next from paginator object - * - * @param Paginator $paginator - * - * @return $this - */ - public function setPaginationLinks(Paginator $paginator) + public function setPaginationLinks(Paginator $paginator): self { $this->setCanonical($paginator->url($paginator->currentPage())); @@ -90,10 +65,7 @@ public function setPaginationLinks(Paginator $paginator) return $this; } - /** - * @inheritdoc - */ - public function setHrefLang(string $lang, string $url) + public function setHrefLang(string $lang, string $url): self { return $this->addLink('alternate_' . $lang, [ 'rel' => 'alternate', @@ -102,26 +74,17 @@ public function setHrefLang(string $lang, string $url) ]); } - /** - * @inheritdoc - */ public function getHrefLang(string $lang): ?TagInterface { return $this->getTag('alternate_' . $lang); } - /** - * @inheritdoc - */ - public function setFavicon(string $href, array $attributes = []) + public function setFavicon(string $href, array $attributes = []): self { return $this->addTag('favicon', new Favicon($href, $attributes)); } - /** - * @inheritdoc - */ - public function addLink(string $name, array $attributes) + public function addLink(string $name, array $attributes): self { if (!isset($attributes['rel'])) { $attributes = array_merge(['rel' => $name], $attributes); @@ -129,4 +92,4 @@ public function addLink(string $name, array $attributes) return $this->addTag($name, Tag::link($attributes)); } -} \ No newline at end of file +} diff --git a/src/MetaTags/Concerns/ManageMetaTags.php b/src/MetaTags/Concerns/ManageMetaTags.php index 8b0638f..f9e0dd1 100644 --- a/src/MetaTags/Concerns/ManageMetaTags.php +++ b/src/MetaTags/Concerns/ManageMetaTags.php @@ -14,11 +14,7 @@ trait ManageMetaTags { - - /** - * @inheritdoc - */ - public function setMetaFrom($object) + public function setMetaFrom($object): self { if ($object instanceof SeoMetaTagsInterface) { $this->setTitle($object->getTitle()) @@ -37,10 +33,7 @@ public function setMetaFrom($object) return $this; } - /** - * @inheritdoc - */ - public function setDescription(?string $description, ?int $maxLength = null) + public function setDescription(?string $description, ?int $maxLength = null): self { if (is_null($maxLength)) { $maxLength = $this->config('description.max_length'); @@ -52,18 +45,12 @@ public function setDescription(?string $description, ?int $maxLength = null) ); } - /** - * @inheritdoc - */ public function getDescription(): ?TagInterface { return $this->getTag('description'); } - /** - * @inheritdoc - */ - public function setKeywords($keywords, ?int $maxLength = null) + public function setKeywords($keywords, ?int $maxLength = null): self { if (!is_array($keywords)) { $keywords = [$keywords]; @@ -73,43 +60,29 @@ public function setKeywords($keywords, ?int $maxLength = null) $maxLength = $this->config('keywords.max_length'); } - $keywords = array_map(function ($keyword) { - return $this->cleanString($keyword); - }, $keywords); + $keywords = array_map(fn($keyword) => $this->cleanString($keyword), $keywords); return $this->addTag('keywords', new Keywords($keywords, $maxLength)); } - /** - * @inheritdoc - */ public function getKeywords(): ?TagInterface { return $this->getTag('keywords'); } - /** - * @inheritdoc - */ - public function setRobots(?string $behavior) + public function setRobots(?string $behavior): self { return $this->addMeta('robots', [ 'content' => $this->cleanString($behavior), ]); } - /** - * @inheritdoc - */ public function getRobots(): ?TagInterface { return $this->getTag('robots'); } - /** - * @inheritdoc - */ - public function setContentType(string $type, string $charset = 'utf-8') + public function setContentType(string $type, string $charset = 'utf-8'): self { return $this->addMeta('content_type', [ 'http-equiv' => 'Content-Type', @@ -117,76 +90,50 @@ public function setContentType(string $type, string $charset = 'utf-8') ], false); } - /** - * @inheritdoc - */ public function getContentType(): ?TagInterface { return $this->getTag('content_type'); } - /** - * @inheritdoc - */ - public function setCharset(string $charset = 'utf-8') + public function setCharset(string $charset = 'utf-8'): self { return $this->addMeta('charset', [ 'charset' => $charset, ], false); } - /** - * @inheritdoc - */ public function getCharset(): ?TagInterface { return $this->getTag('charset'); } - /** - * @inheritdoc - */ - public function setViewport(string $viewport) + public function setViewport(string $viewport): self { return $this->addMeta('viewport', [ 'content' => $this->cleanString($viewport), ]); } - /** - * @inheritdoc - */ public function getViewport(): ?TagInterface { return $this->getTag('viewport'); } - /** - * @inheritdoc - */ - public function addCsrfToken() + public function addCsrfToken(): self { return $this->addMeta('csrf-token', [ - 'content' => function() { - return Session::token(); - }, + 'content' => static fn() => Session::token(), ]); } - /** - * @inheritdoc - */ - public function addWebmaster(string $service, string $content) + public function addWebmaster(string $service, string $content): self { return $this->addTag('webmaster.'.$service, new Webmaster( $service, $content )); } - /** - * @inheritdoc - */ - public function setGeo(GeoMetaInformationInterface $geo) + public function setGeo(GeoMetaInformationInterface $geo): self { $this->addMeta('geo.position', [ 'content' => $this->cleanString($geo->latitude() . '; ' . $geo->longitude()), @@ -207,10 +154,7 @@ public function setGeo(GeoMetaInformationInterface $geo) return $this; } - /** - * @inheritdoc - */ - public function addMeta(string $name, array $attributes, bool $checkNameAttribute = true) + public function addMeta(string $name, array $attributes, bool $checkNameAttribute = true): self { if ($checkNameAttribute && !isset($attributes['name'])) { $attributes = array_merge(['name' => $name], $attributes); diff --git a/src/MetaTags/Concerns/ManagePackages.php b/src/MetaTags/Concerns/ManagePackages.php index 6c33d62..59c0055 100644 --- a/src/MetaTags/Concerns/ManagePackages.php +++ b/src/MetaTags/Concerns/ManagePackages.php @@ -8,20 +8,11 @@ trait ManagePackages { - /** - * @var array - */ - private $packages = []; - - /** - * @var array - */ - private $registeredPackages = []; - - /** - * @inheritdoc - */ - public function registerPackage(PackageInterface $package) + private array $packages = []; + + private array $registeredPackages = []; + + public function registerPackage(PackageInterface $package): self { $name = $package->getName(); @@ -51,24 +42,18 @@ public function registerPackage(PackageInterface $package) return $this; } - /** - * @inheritdoc - */ - public function replacePackage(PackageInterface $package) + public function replacePackage(PackageInterface $package): self { $this->removePackage($package->getName()); return $this->registerPackage($package); } - /** - * @inheritdoc - */ - public function removePackage(string $name) + public function removePackage(string $name): self { $package = $this->getPackage($name); - if (($index = array_search($name, $this->registeredPackages)) !== false) { + if (($index = array_search($name, $this->registeredPackages, true)) !== false) { unset($this->registeredPackages[$index]); } @@ -77,7 +62,7 @@ public function removePackage(string $name) } if ($package instanceof PlacementsInterface) { - foreach ($package->getPlacements() as $placement => $tags) { + foreach ($package->getPlacements() as $tags) { foreach ($tags as $name => $tag) { $this->removeTag($name); } @@ -91,18 +76,12 @@ public function removePackage(string $name) return $this; } - /** - * @inheritdoc - */ public function getPackage(string $name): ?PackageInterface { return $this->packageManager->getPackage($name); } - /** - * @inheritdoc - */ - public function includePackages($packages) + public function includePackages($packages): self { $packages = is_array($packages) ? $packages : func_get_args(); @@ -115,11 +94,6 @@ public function includePackages($packages) return $this; } - /** - * @param string $name - * - * @return bool - */ protected function isRegisteredPackage(string $name): bool { return in_array($name, $this->registeredPackages); diff --git a/src/MetaTags/Concerns/ManagePlacements.php b/src/MetaTags/Concerns/ManagePlacements.php index d48e173..976d5c4 100644 --- a/src/MetaTags/Concerns/ManagePlacements.php +++ b/src/MetaTags/Concerns/ManagePlacements.php @@ -7,38 +7,23 @@ trait ManagePlacements { - /** - * @var PlacementsBag - */ - protected $placements; - - /** - * @inheritdoc - */ + protected PlacementsBag $placements; + public function head(): PlacementInterface { return $this->placement(static::PLACEMENT_HEAD); } - /** - * @inheritdoc - */ public function footer(): PlacementInterface { return $this->placement(static::PLACEMENT_FOOTER); } - /** - * @inheritdoc - */ public function placement(string $name): PlacementInterface { return $this->placements->getBag($name); } - /** - * @inheritdoc - */ public function getPlacements(): array { return $this->placements->all(); @@ -48,4 +33,4 @@ protected function initPlacements(): void { $this->placements = new PlacementsBag(); } -} \ No newline at end of file +} diff --git a/src/MetaTags/Concerns/ManageTitle.php b/src/MetaTags/Concerns/ManageTitle.php index c79aa0d..08091e7 100644 --- a/src/MetaTags/Concerns/ManageTitle.php +++ b/src/MetaTags/Concerns/ManageTitle.php @@ -7,30 +7,21 @@ trait ManageTitle { - /** - * @inheritdoc - */ - public function setTitle(?string $title, int $maxLength = null) + public function setTitle(?string $title, int $maxLength = null): self { $this->getTitle()->setTitle($this->cleanString($title), $maxLength); return $this; } - /** - * @inheritdoc - */ - public function setTitleSeparator(string $separator) + public function setTitleSeparator(string $separator): self { $this->getTitle()->setSeparator($separator); return $this; } - /** - * @inheritdoc - */ - public function prependTitle(?string $text) + public function prependTitle(?string $text): self { $title = $this->getTitle(); @@ -43,9 +34,6 @@ public function prependTitle(?string $text) return $this; } - /** - * @inheritdoc - */ public function getTitle(): ?TitleInterface { $title = $this->getTag('title'); diff --git a/src/MetaTags/Entities/Builders/YandexMetrikaCounterBuilder.php b/src/MetaTags/Entities/Builders/YandexMetrikaCounterBuilder.php index f3181e9..d6d2625 100644 --- a/src/MetaTags/Entities/Builders/YandexMetrikaCounterBuilder.php +++ b/src/MetaTags/Entities/Builders/YandexMetrikaCounterBuilder.php @@ -5,49 +5,29 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Htmlable; -class YandexMetrikaCounterBuilder implements Htmlable, Arrayable +class YandexMetrikaCounterBuilder implements Htmlable, Arrayable, \Stringable { - /** - * @var string - */ - private $counterId; - /** * Counter settings - * - * @var array */ - private $settings = [ + private array $settings = [ 'clickmap' => true, 'trackLinks' => true, 'accurateTrackBounce' => true, 'webvisor' => true, ]; - /** - * @var string - */ - private $scriptUrl = 'https://mc.yandex.ru/metrika/tag.js'; + private string $scriptUrl = 'https://mc.yandex.ru/metrika/tag.js'; - /** - * @var bool - */ - private $forXML = true; + private bool $forXML = true; - /** - * @param string $counterId - */ - public function __construct(string $counterId) - { - $this->counterId = $counterId; + public function __construct( + private string $counterId + ) { } /** * @see https://yandex.ru/support/metrica/behavior/click-map.html#click-map - * - * @param bool $state - * - * @return $this */ public function clickmap(bool $state = true): self { @@ -62,10 +42,6 @@ public function clickmap(bool $state = true): self * Подробные записи действий посетителей на сайте: движения мышью, прокручивание страницы и клики. * * @see https://yandex.ru/support/metrica/webvisor/settings.html - * - * @param bool $state - * - * @return $this */ public function webvisor(bool $state = true): self { @@ -78,10 +54,6 @@ public function webvisor(bool $state = true): self * Собирается ли статистика на внешние ресурсы, данные о загрузке файлов и данные о нажатии на кнопку "Поделиться". * * @see https://yandex.ru/support/metrica/behavior/link-map.html - * - * @param bool $state - * - * @return $this */ public function trackLinks(bool $state = true): self { @@ -92,10 +64,6 @@ public function trackLinks(bool $state = true): self /** * Точный показатель отказов - * - * @param bool $state - * - * @return $this */ public function accurateTrackBounce(bool $state = true): self { @@ -106,10 +74,6 @@ public function accurateTrackBounce(bool $state = true): self /** * Опция применима для AJAX-сайтов. - * - * @param bool $state - * - * @return $this */ public function trackHash(bool $state = true): self { @@ -123,10 +87,6 @@ public function trackHash(bool $state = true): self * Чтобы статистика начала собираться, настройте на сайте передачу данных. * * @see https://yandex.ru/support/metrica/data/e-commerce.html - * - * @param string $containerName - * - * @return $this */ public function eCommerce(string $containerName): self { @@ -138,8 +98,6 @@ public function eCommerce(string $containerName): self /** * Позволяет корректно учитывать посещения из регионов, в которых ограничен доступ к ресурсам Яндекса. * Использование этой опции может снизить скорость загрузки кода счётчика. - * - * @return $this */ public function useCDN(): self { @@ -150,8 +108,6 @@ public function useCDN(): self /** * Элемент noscript не должен использоваться в XML-документах (Content‑Type:application/xhtml+xml). - * - * @return $this */ public function disableNoScript(): self { @@ -162,10 +118,8 @@ public function disableNoScript(): self /** * Generate HTML script for Yandex counter - * - * @inheritDoc */ - public function toHtml() + public function toHtml(): string { return sprintf(<< @@ -179,14 +133,11 @@ public function toHtml() TAG , $this->scriptUrl, $this->counterId, - json_encode($this->filteredSettings()), + json_encode($this->filteredSettings(), JSON_THROW_ON_ERROR), $this->getNoscriptHtml() ); } - /** - * @return string - */ private function getNoscriptHtml(): string { if (!$this->forXML) { @@ -199,25 +150,20 @@ private function getNoscriptHtml(): string ); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } /** * Get settings without false values - * - * @return array */ private function filteredSettings(): array { return array_filter($this->settings); } - public function toArray() + public function toArray(): array { return [ 'settings' => $this->settings, diff --git a/src/MetaTags/Entities/Comment.php b/src/MetaTags/Entities/Comment.php index a41dde0..6d6a309 100644 --- a/src/MetaTags/Entities/Comment.php +++ b/src/MetaTags/Entities/Comment.php @@ -6,74 +6,56 @@ use Butschster\Head\Contracts\MetaTags\Entities\TagInterface; use Butschster\Head\MetaTags\Entities\Concerns\ManageVisibility; -class Comment implements TagInterface, HasVisibilityConditions +class Comment implements TagInterface, HasVisibilityConditions, \Stringable { use ManageVisibility; - /** - * @var string - */ - protected $openComment; + protected string $closeComment; - /** - * @var string - */ - protected $closeComment; - - /** - * @var TagInterface - */ - protected $tag; + public function __construct( + protected TagInterface $tag, + protected string $openComment, + ?string $closeComment = null, + ) { + $this->closeComment = $closeComment ?: $openComment; + } - /** - * @param TagInterface $tag - * @param string $openComment - * @param string|null $closeComment - */ - public function __construct(TagInterface $tag, string $openComment, ?string $closeComment = null) + public function getTag(): TagInterface { - $this->tag = $tag; - $this->openComment = $openComment; - $this->closeComment = $closeComment ?: $openComment; + return $this->tag; } - /** - * Get content as a string of HTML. - * - * @return string - */ - public function toHtml() + public function toHtml(): string { - return sprintf(<< %s COM -, $this->openComment, $this->tag->toHtml(), $this->closeComment); + , + $this->openComment, + $this->tag->toHtml(), + $this->closeComment, + ); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - /** - * @return string - */ public function getPlacement(): string { return $this->tag->getPlacement(); } - public function toArray() + public function toArray(): array { return [ 'type' => 'comment', 'tag' => $this->tag->toArray(), - 'content' => $this->toHtml() + 'content' => $this->toHtml(), ]; } } diff --git a/src/MetaTags/Entities/Concerns/ManageMaxLength.php b/src/MetaTags/Entities/Concerns/ManageMaxLength.php index 1d68262..df5cde3 100644 --- a/src/MetaTags/Entities/Concerns/ManageMaxLength.php +++ b/src/MetaTags/Entities/Concerns/ManageMaxLength.php @@ -7,17 +7,9 @@ trait ManageMaxLength { - /** - * @var int - */ - protected $maxLength = self::DEFAULT_LENGTH; - - /** - * @param int|null $maxLength - * - * @return $this - */ - public function setMaxLength(?int $maxLength) + protected ?int $maxLength = self::DEFAULT_LENGTH; + + public function setMaxLength(?int $maxLength): self { if (!is_null($maxLength) && $maxLength < 1) { throw new InvalidArgumentException('The maximum length must be greater 0.'); @@ -30,11 +22,6 @@ public function setMaxLength(?int $maxLength) return $this; } - /** - * @param string $string - * - * @return string - */ protected function limitString(string $string): string { if ($this->maxLength) { diff --git a/src/MetaTags/Entities/Concerns/ManagePlacements.php b/src/MetaTags/Entities/Concerns/ManagePlacements.php index d1bd28f..3e0d9c3 100644 --- a/src/MetaTags/Entities/Concerns/ManagePlacements.php +++ b/src/MetaTags/Entities/Concerns/ManagePlacements.php @@ -6,30 +6,18 @@ trait ManagePlacements { - /** - * @var string - */ - protected $placement = Meta::PLACEMENT_HEAD; + protected string $placement = Meta::PLACEMENT_HEAD; - - /** - * @return string - */ public function getPlacement(): string { return $this->placement; } - /** - * @param string $placement - * - * @return $this - */ - public function setPlacement(string $placement) + public function setPlacement(string $placement): self { $this->placement = $placement; return $this; } -} \ No newline at end of file +} diff --git a/src/MetaTags/Entities/Concerns/ManageVisibility.php b/src/MetaTags/Entities/Concerns/ManageVisibility.php index 47fa2f1..d8e7800 100644 --- a/src/MetaTags/Entities/Concerns/ManageVisibility.php +++ b/src/MetaTags/Entities/Concerns/ManageVisibility.php @@ -8,23 +8,16 @@ trait ManageVisibility { /** * Visibility condition - * @var Closure */ - protected $visibilityCondition = null; + protected ?Closure $visibilityCondition = null; - /** - * @inheritDoc - */ - public function visibleWhen(Closure $condition) + public function visibleWhen(Closure $condition): self { $this->visibilityCondition = $condition; return $this; } - /** - * @inheritDoc - */ public function isVisible(): bool { if ($this->visibilityCondition instanceof Closure) { diff --git a/src/MetaTags/Entities/ConditionalComment.php b/src/MetaTags/Entities/ConditionalComment.php index 2e2637d..7ba0389 100644 --- a/src/MetaTags/Entities/ConditionalComment.php +++ b/src/MetaTags/Entities/ConditionalComment.php @@ -6,21 +6,12 @@ class ConditionalComment extends Comment { - /** - * @param TagInterface $tag - * @param string $condition - */ public function __construct(TagInterface $tag, string $condition) { parent::__construct($tag, $condition); } - /** - * Get content as a string of HTML. - * - * @return string - */ - public function toHtml() + public function toHtml(): string { return sprintf(<< @@ -29,4 +20,4 @@ public function toHtml() COM , $this->openComment, $this->tag->toHtml()); } -} \ No newline at end of file +} diff --git a/src/MetaTags/Entities/Description.php b/src/MetaTags/Entities/Description.php index 7fd3601..4ab2c0d 100644 --- a/src/MetaTags/Entities/Description.php +++ b/src/MetaTags/Entities/Description.php @@ -6,28 +6,22 @@ class Description extends Tag { use Concerns\ManageMaxLength; - const DEFAULT_LENGTH = null; + public const DEFAULT_LENGTH = null; - /** - * @var string - */ - protected $description; - - /** - * @param string $description - * @param int|null $maxLength - */ - public function __construct(string $description, ?int $maxLength = self::DEFAULT_LENGTH) - { + public function __construct( + protected string $description, + ?int $maxLength = self::DEFAULT_LENGTH + ) { parent::__construct('meta', []); - $this->description = $description; $this->setMaxLength($maxLength); } - /** - * @return array - */ + public function getDescription(): string + { + return $this->description; + } + protected function getAttributes(): array { return array_merge([ diff --git a/src/MetaTags/Entities/Favicon.php b/src/MetaTags/Entities/Favicon.php index 8363289..cbbaccf 100644 --- a/src/MetaTags/Entities/Favicon.php +++ b/src/MetaTags/Entities/Favicon.php @@ -4,25 +4,18 @@ class Favicon extends Tag { - /** - * @var string - */ - protected $href; + public function __construct( + protected string $href, + array $attributes = [], + ) { + parent::__construct(tagName: 'link', attributes: $attributes, closeTag: false); + } - /** - * @param string $href - * @param array $attributes - */ - public function __construct(string $href, array $attributes = []) + public function getHref(): string { - $this->href = $href; - - parent::__construct('link', $attributes, false); + return $this->href; } - /** - * @return array - */ protected function getAttributes(): array { return array_merge([ @@ -32,22 +25,14 @@ protected function getAttributes(): array ], parent::getAttributes()); } - /** - * @return string - */ public function getType(): string { $ext = pathinfo($this->href, PATHINFO_EXTENSION); - - switch ($ext) { - case 'png': - return 'image/png'; - case 'gif': - return 'image/gif'; - case 'svg': - return 'image/svg+xml'; - } - - return 'image/x-icon'; + return match ($ext) { + 'png' => 'image/png', + 'gif' => 'image/gif', + 'svg' => 'image/svg+xml', + default => 'image/x-icon', + }; } -} \ No newline at end of file +} diff --git a/src/MetaTags/Entities/GoogleAnalytics.php b/src/MetaTags/Entities/GoogleAnalytics.php index d1934be..31e2856 100644 --- a/src/MetaTags/Entities/GoogleAnalytics.php +++ b/src/MetaTags/Entities/GoogleAnalytics.php @@ -5,29 +5,23 @@ use Butschster\Head\Contracts\MetaTags\Entities\TagInterface; use Butschster\Head\MetaTags\Meta; -class GoogleAnalytics implements TagInterface +class GoogleAnalytics implements TagInterface, \Stringable { - /** - * Google analytics identifier - * - * @var string - */ - private $counterId; + public function __construct( + /** Google analytics identifier */ + private string $counterId, + ) { + } - /** - * @param string $counterId Google analytics identifier - */ - public function __construct(string $counterId) + public function getCounterId(): string { - $this->counterId = $counterId; + return $this->counterId; } - /** - * @inheritDoc - */ - public function toHtml() + public function toHtml(): string { - return sprintf(<< (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), @@ -37,31 +31,26 @@ public function toHtml() ga('send', 'pageview'); TAG - , $this->counterId -); + , + $this->counterId, + ); } - /** - * @inheritDoc - */ public function getPlacement(): string { return Meta::PLACEMENT_FOOTER; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { return [ 'type' => 'google_analytics', - 'counter_id' => $this->counterId + 'counter_id' => $this->counterId, ]; } } diff --git a/src/MetaTags/Entities/GoogleTagManager.php b/src/MetaTags/Entities/GoogleTagManager.php index 149f078..52be92e 100644 --- a/src/MetaTags/Entities/GoogleTagManager.php +++ b/src/MetaTags/Entities/GoogleTagManager.php @@ -5,27 +5,20 @@ use Butschster\Head\Contracts\MetaTags\Entities\TagInterface; use Butschster\Head\MetaTags\Meta; -class GoogleTagManager implements TagInterface +class GoogleTagManager implements TagInterface, \Stringable { - /** - * Google analytics identifier - * - * @var string - */ - private $counterId; + public function __construct( + /** Google analytics identifier */ + private string $counterId + ) { + } - /** - * @param string $counterId Google analytics identifier - */ - public function __construct(string $counterId) + public function getCounterId(): string { - $this->counterId = $counterId; + return $this->counterId; } - /** - * @inheritDoc - */ - public function toHtml() + public function toHtml(): string { return sprintf(<< @@ -41,23 +34,17 @@ function gtag(){dataLayer.push(arguments);} ); } - /** - * @inheritDoc - */ public function getPlacement(): string { return Meta::PLACEMENT_HEAD; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { return [ 'type' => 'google_tag_manager', diff --git a/src/MetaTags/Entities/JavascriptVariables.php b/src/MetaTags/Entities/JavascriptVariables.php index 8fbc9fe..a24e17f 100644 --- a/src/MetaTags/Entities/JavascriptVariables.php +++ b/src/MetaTags/Entities/JavascriptVariables.php @@ -8,40 +8,21 @@ use Illuminate\Contracts\Support\Jsonable; use InvalidArgumentException; -class JavascriptVariables implements TagInterface, HasVisibilityConditions +class JavascriptVariables implements TagInterface, HasVisibilityConditions, \Stringable { - use Concerns\ManagePlacements, - Concerns\ManageVisibility; + use Concerns\ManagePlacements; + use Concerns\ManageVisibility; + protected array $variables = []; - /** - * @var array - */ - protected $variables = []; - - /** + public function __construct(array $variables = [], /** * The namespace to nest JS vars under. - * - * @var string */ - protected $namespace; - - /** - * @param array $variables - * @param string $namespace - */ - public function __construct(array $variables = [], string $namespace = 'window') + protected string $namespace = 'window') { - $this->namespace = $namespace; $this->buildJavaScriptSyntax($variables); } - /** - * @param string $key - * @param mixed $value - * - * @return $this - */ - public function put(string $key, $value) + public function put(string $key, mixed $value): self { // First, we have to translate the variables // to something JS-friendly. @@ -53,10 +34,8 @@ public function put(string $key, $value) /** * Translate the array of PHP vars to * the expected JavaScript syntax. - * - * @param array $variables */ - protected function buildJavaScriptSyntax(array $variables) + protected function buildJavaScriptSyntax(array $variables): void { foreach ($variables as $key => $value) { $this->variables[] = $this->buildVariableInitialization($key, $value); @@ -66,21 +45,15 @@ protected function buildJavaScriptSyntax(array $variables) /** * Translate a single PHP var to JS. * - * @param string $key - * @param mixed $value - * - * @return string * @throws InvalidArgumentException */ - protected function buildVariableInitialization(string $key, $value): string + protected function buildVariableInitialization(string $key, mixed $value): string { - return "{$this->namespace}.{$key} = {$this->optimizeValueForJavaScript($value)};"; + return sprintf('%s.%s = %s;', $this->namespace, $key, $this->optimizeValueForJavaScript($value)); } /** * Create the namespace to which all vars are nested. - * - * @return string */ protected function buildNamespaceDeclaration(): string { @@ -88,26 +61,23 @@ protected function buildNamespaceDeclaration(): string return ''; } - return "window.{$this->namespace} = window.{$this->namespace} || {};" . PHP_EOL; + return sprintf('window.%s = window.%s || {};', $this->namespace, $this->namespace) . PHP_EOL; } /** * Format a value for JavaScript. * - * @param string $value - * - * @return string * @throws \Exception * */ - protected function optimizeValueForJavaScript($value) + protected function optimizeValueForJavaScript(mixed $value): mixed { // For every transformable type, let's see if // it needs to be transformed for JS-use. $types = ['String', 'Array', 'Object', 'Numeric', 'Boolean', 'Null']; foreach ($types as $transformer) { - $js = $this->{"transform{$transformer}"}($value); + $js = $this->{'transform' . $transformer}($value); if (!is_null($js)) { return $js; @@ -117,69 +87,54 @@ protected function optimizeValueForJavaScript($value) /** * Transform a string. - * - * @param string $value - * - * @return string */ - protected function transformString($value) + protected function transformString(mixed $value): ?string { if (is_string($value)) { $value = str_replace(['\\', "'"], ['\\\\', "\'"], $value); - return "'{$value}'"; + return sprintf('\'%s\'', $value); } + + return null; } /** * Transform an array. - * - * @param array $value - * - * @return string */ - protected function transformArray($value) + protected function transformArray(mixed $value): ?string { if (is_array($value)) { - return json_encode($value); + return json_encode($value, JSON_THROW_ON_ERROR); } + + return null; } /** * Transform a numeric value. - * - * @param mixed $value - * - * @return mixed */ - protected function transformNumeric($value) + protected function transformNumeric(mixed $value) { if (is_numeric($value)) { return $value; } + + return null; } /** * Transform a boolean. - * - * @param bool $value - * - * @return string */ - protected function transformBoolean($value) + protected function transformBoolean($value): ?string { if (is_bool($value)) { return $value ? 'true' : 'false'; } + + return null; } - /** - * @param object $value - * - * @return string - * @throws InvalidArgumentException - * - */ - protected function transformObject($value): ?string + protected function transformObject(mixed $value): ?string { if (!is_object($value)) { return null; @@ -190,7 +145,7 @@ protected function transformObject($value): ?string } if ($value instanceof Arrayable) { - return json_encode($value->toArray()); + return json_encode($value->toArray(), JSON_THROW_ON_ERROR); } // Otherwise, if the object doesn't even have a @@ -199,55 +154,47 @@ protected function transformObject($value): ?string throw new InvalidArgumentException('Cannot transform this object to JavaScript.'); } - return "'{$value}'"; + return sprintf('\'%s\'', $value); } /** * Transform "null.". - * - * @param mixed $value - * - * @return string */ - protected function transformNull($value) + protected function transformNull(mixed $value): ?string { if (is_null($value)) { return 'null'; } - } + return null; + } - /** - * Get content as a string of HTML. - * - * @return string - */ - public function toHtml() + public function toHtml(): string { $string = implode(PHP_EOL, $this->variables); $namespaceDeclaration = $this->buildNamespaceDeclaration(); - return sprintf(<< %s VAR - , $namespaceDeclaration . $string); + , + $namespaceDeclaration . $string, + ); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { return [ 'type' => 'javascript_variables', - 'variables' => $this->variables + 'variables' => $this->variables, ]; } } diff --git a/src/MetaTags/Entities/Keywords.php b/src/MetaTags/Entities/Keywords.php index 41de8a6..a97f02f 100644 --- a/src/MetaTags/Entities/Keywords.php +++ b/src/MetaTags/Entities/Keywords.php @@ -6,18 +6,11 @@ class Keywords extends Tag { use Concerns\ManageMaxLength; - const DEFAULT_LENGTH = null; - - /** - * @var string - */ - protected $keywords = []; - - /** - * @param string|array $keywords - * @param int|null $maxLength - */ - public function __construct($keywords, ?int $maxLength = self::DEFAULT_LENGTH) + public const DEFAULT_LENGTH = null; + + protected array $keywords = []; + + public function __construct(string|array $keywords, ?int $maxLength = self::DEFAULT_LENGTH) { parent::__construct('meta', []); @@ -25,9 +18,11 @@ public function __construct($keywords, ?int $maxLength = self::DEFAULT_LENGTH) $this->setMaxLength($maxLength); } - /** - * @return array - */ + public function getKeywords(): array + { + return $this->keywords; + } + protected function getAttributes(): array { return array_merge([ @@ -36,10 +31,6 @@ protected function getAttributes(): array ], parent::getAttributes()); } - /** - * @param array $keywords - * @return string - */ protected function makeString(array $keywords): string { return $this->limitString(implode(', ', $keywords)); diff --git a/src/MetaTags/Entities/Script.php b/src/MetaTags/Entities/Script.php index 728c4a8..6bd166a 100644 --- a/src/MetaTags/Entities/Script.php +++ b/src/MetaTags/Entities/Script.php @@ -6,34 +6,27 @@ class Script extends Tag { - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $src; - - /** - * @param string $name - * @param string $src - * @param array $attributes - * @param string|null $placement - */ - public function __construct(string $name, string $src, array $attributes = [], string $placement = null) - { - $this->name = $name; - $this->src = $src; + public function __construct( + protected string $name, + protected string $src, + array $attributes = [], + string $placement = null + ) { $this->placement = $placement ?: Meta::PLACEMENT_FOOTER; parent::__construct('script', $attributes, true); } - /** - * @return array - */ + public function getName(): string + { + return $this->name; + } + + public function getSrc(): string + { + return $this->src; + } + protected function getAttributes(): array { return array_merge([ @@ -41,12 +34,7 @@ protected function getAttributes(): array ], parent::getAttributes()); } - /** - * Get content as a string of HTML. - * - * @return string - */ - public function toHtml() + public function toHtml(): string { return sprintf( '<%s %s>', @@ -55,4 +43,4 @@ public function toHtml() $this->tagName ); } -} \ No newline at end of file +} diff --git a/src/MetaTags/Entities/Style.php b/src/MetaTags/Entities/Style.php index e7e682b..d800e71 100644 --- a/src/MetaTags/Entities/Style.php +++ b/src/MetaTags/Entities/Style.php @@ -4,32 +4,24 @@ class Style extends Tag { - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $src; + public function __construct( + protected string $name, + protected string $src, + array $attributes = [], + ) { + parent::__construct('link', $attributes, false); + } - /** - * @param string $name - * @param string $src - * @param array $attributes - */ - public function __construct(string $name, string $src, array $attributes = []) + public function getName(): string { - $this->name = $name; - $this->src = $src; + return $this->name; + } - parent::__construct('link', $attributes, false); + public function getSrc(): string + { + return $this->src; } - /** - * @return array - */ protected function getAttributes(): array { return array_merge([ @@ -39,4 +31,4 @@ protected function getAttributes(): array 'href' => $this->src, ], parent::getAttributes()); } -} \ No newline at end of file +} diff --git a/src/MetaTags/Entities/Tag.php b/src/MetaTags/Entities/Tag.php index ee1b8f1..24d60aa 100644 --- a/src/MetaTags/Entities/Tag.php +++ b/src/MetaTags/Entities/Tag.php @@ -6,80 +6,40 @@ use Butschster\Head\Contracts\MetaTags\Entities\TagInterface; use Closure; -class Tag implements TagInterface, HasVisibilityConditions +class Tag implements TagInterface, HasVisibilityConditions, \Stringable { - use Concerns\ManagePlacements, - Concerns\ManageVisibility; - - /** - * Make a new instance - * - * @param string $tagName - * @param array $attributes - * @param bool $closeTag - * - * @return static - */ - public static function make(string $tagName, array $attributes, bool $closeTag = false) + use Concerns\ManagePlacements; + use Concerns\ManageVisibility; + public static function make(string $tagName, array $attributes, bool $closeTag = false): self { return new static($tagName, $attributes, $closeTag); } /** * Make a new meta tag - * - * @param array $attributes - * - * @return static */ - public static function meta(array $attributes) + public static function meta(array $attributes): self { return new static('meta', $attributes); } /** * Make a new link tag - * - * @param array $attributes - * - * @return static */ - public static function link(array $attributes) + public static function link(array $attributes): self { return new static('link', $attributes); } - /** - * @var string - */ - protected $tagName; - - /** - * @var array - */ - protected $attributes; - - /** - * @var bool - */ - protected $closeTag; - - /** - * @param string $tagName - * @param array $attributes - * @param bool $closeTag - */ - public function __construct(string $tagName, array $attributes, bool $closeTag = false) - { - $this->tagName = $tagName; - $this->attributes = $attributes; - $this->closeTag = $closeTag; + public function __construct( + protected string $tagName, + protected array $attributes, + protected bool $closeTag = false, + ) { } /** * Build an HTML attribute string from an array. - * - * @return string */ public function compiledAttributes(): string { @@ -93,14 +53,11 @@ public function compiledAttributes(): string } } - return count($html) > 0 + return $html !== [] ? implode(' ', $html) : ''; } - /** - * @return array - */ protected function getAttributes(): array { return $this->attributes; @@ -108,13 +65,8 @@ protected function getAttributes(): array /** * Build a single attribute element. - * - * @param string $key - * @param string $value - * - * @return string */ - protected function attributeElement($key, $value) + protected function attributeElement(mixed $key, mixed $value): mixed { // For numeric keys we will assume that the key and the value are the same // as this will convert HTML attributes such as "required" to a correct @@ -134,28 +86,23 @@ protected function attributeElement($key, $value) /** * Get content as a string of HTML. - * - * @return string */ - public function toHtml() + public function toHtml(): string { return sprintf( '<%s %s%s>', $this->tagName, $this->compiledAttributes(), - $this->closeTag ? ' /' : '' + $this->closeTag ? ' /' : '', ); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { $attributes = []; @@ -172,13 +119,13 @@ public function toArray() $attributes[$key] = $value; } else { - $attributes[$key] = (string) $value; + $attributes[$key] = (string)$value; } } return $attributes + [ - 'type' => 'tag', - 'tag' => $this->tagName - ]; + 'type' => 'tag', + 'tag' => $this->tagName, + ]; } } diff --git a/src/MetaTags/Entities/Title.php b/src/MetaTags/Entities/Title.php index 28e60a8..b47e2c1 100644 --- a/src/MetaTags/Entities/Title.php +++ b/src/MetaTags/Entities/Title.php @@ -5,55 +5,32 @@ use Butschster\Head\Contracts\MetaTags\Entities\HasVisibilityConditions; use Butschster\Head\Contracts\MetaTags\Entities\TitleInterface; -class Title implements TitleInterface, HasVisibilityConditions +class Title implements TitleInterface, HasVisibilityConditions, \Stringable { - use Concerns\ManageMaxLength, - Concerns\ManageVisibility; + use Concerns\ManageMaxLength; + use Concerns\ManageVisibility; - const DEFAULT_LENGTH = null; + public const DEFAULT_LENGTH = null; - /** - * @var string - */ - protected $title; + protected string $separator = '|'; - /** - * @var string - */ - protected $separator = '|'; + protected array $prepend = []; - /** - * @var array - */ - protected $prepend = []; + protected bool $rtl = false; - /** - * @var bool - */ - protected $rtl = false; - - /** - * @param string|null $title - * @param int|null $maxLength - */ - public function __construct(?string $title = null, ?int $maxLength = self::DEFAULT_LENGTH) - { - $this->title = $title; + public function __construct( + protected ?string $title = null, + ?int $maxLength = self::DEFAULT_LENGTH + ) { $this->setMaxLength($maxLength); } - /** - * @inheritdoc - */ public function getPlacement(): string { return 'head'; } - /** - * @inheritdoc - */ - public function setTitle(?string $title, ?int $maxLength = null) + public function setTitle(?string $title, ?int $maxLength = null): self { $this->title = $title; $this->setMaxLength($maxLength); @@ -61,10 +38,7 @@ public function setTitle(?string $title, ?int $maxLength = null) return $this; } - /** - * @inheritdoc - */ - public function prepend(string $text) + public function prepend(string $text): self { $this->prepend[] = $text; @@ -73,34 +47,24 @@ public function prepend(string $text) /** * Toggle RTL mode - * - * @param bool $status - * - * @return $this */ - public function rtl(bool $status = true) + public function rtl(bool $status = true): self { $this->rtl = $status; return $this; } - /** - * @inheritdoc - */ - public function setSeparator(string $separator) + public function setSeparator(string $separator): self { $this->separator = trim($separator); return $this; } - /** - * @return string - */ protected function makeTitle(): string { - $separator = " {$this->separator} "; + $separator = sprintf(' %s ', $this->separator); $title = ''; if (!empty($this->prepend)) { @@ -122,27 +86,26 @@ protected function makeTitle(): string return $this->limitString($title); } - /** - * @inheritdoc - */ - public function toHtml() + public function toHtml(): string { return sprintf('%s', $this->makeTitle()); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { return [ 'tag' => 'title', 'content' => $this->makeTitle() ]; } + + public function getTitle(): string + { + return $this->makeTitle(); + } } diff --git a/src/MetaTags/Entities/Webmaster.php b/src/MetaTags/Entities/Webmaster.php index 8713dc3..71c225a 100644 --- a/src/MetaTags/Entities/Webmaster.php +++ b/src/MetaTags/Entities/Webmaster.php @@ -4,19 +4,22 @@ class Webmaster extends Tag { - const GOOGLE = 'google'; - const YANDEX = 'yandex'; - const PINTEREST = 'pinterest'; - const ALEXA = 'alexa'; - const BING = 'bing'; - const FACEBOOK = 'facebook'; + public const GOOGLE = 'google'; + + public const YANDEX = 'yandex'; + + public const PINTEREST = 'pinterest'; + + public const ALEXA = 'alexa'; + + public const BING = 'bing'; + + public const FACEBOOK = 'facebook'; /** * The supported webmasters. - * - * @var array */ - protected $services = [ + protected array $services = [ Webmaster::YANDEX => 'yandex-verification', Webmaster::GOOGLE => 'google-site-verification', Webmaster::PINTEREST => 'p:domain_verify', @@ -25,10 +28,6 @@ class Webmaster extends Tag Webmaster::FACEBOOK => 'facebook-domain-verification', ]; - /** - * @param string $service - * @param string $content - */ public function __construct(string $service, string $content) { parent::__construct('meta', [ @@ -37,11 +36,6 @@ public function __construct(string $service, string $content) ]); } - /** - * @param string $service - * - * @return string - */ protected function getServiceName(string $service): string { if (!isset($this->services[$service])) { diff --git a/src/MetaTags/Entities/YandexMetrika.php b/src/MetaTags/Entities/YandexMetrika.php index 1c9100f..d885373 100644 --- a/src/MetaTags/Entities/YandexMetrika.php +++ b/src/MetaTags/Entities/YandexMetrika.php @@ -10,41 +10,30 @@ /** * @mixin YandexMetrikaCounterBuilder */ -class YandexMetrika implements TagInterface +class YandexMetrika implements TagInterface, \Stringable { - /** - * @var YandexMetrikaCounterBuilder - */ - protected $builder; + protected YandexMetrikaCounterBuilder $builder; - /** - * @param string $counterId - */ public function __construct(string $counterId) { $this->builder = new YandexMetrikaCounterBuilder($counterId); } - /** - * @return string - */ public function getPlacement(): string { return Meta::PLACEMENT_FOOTER; } - /** - * @param string $method - * @param array $arguments - * - * @return YandexMetrika - */ - public function __call($method, $arguments) + public function __call(string $method, array $arguments): YandexMetrika { if (!method_exists($this->builder, $method)) { - throw new BadMethodCallException(sprintf( - 'Method %s::%s does not exist.', get_class($this->builder), $method - )); + throw new BadMethodCallException( + sprintf( + 'Method %s::%s does not exist.', + $this->builder::class, + $method, + ), + ); } call_user_func_array([$this->builder, $method], $arguments); @@ -52,28 +41,20 @@ public function __call($method, $arguments) return $this; } - /** - * Get content as a string of HTML. - * - * @return string - */ - public function toHtml() + public function toHtml(): string { return (string)$this->builder; } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } - public function toArray() + public function toArray(): array { return [ - 'type' => 'yandex_metrika', - ] + $this->builder->toArray(); + 'type' => 'yandex_metrika', + ] + $this->builder->toArray(); } } diff --git a/src/MetaTags/Meta.php b/src/MetaTags/Meta.php index 5fd6248..6014e80 100644 --- a/src/MetaTags/Meta.php +++ b/src/MetaTags/Meta.php @@ -8,45 +8,28 @@ use Illuminate\Contracts\Config\Repository; use Illuminate\Support\Traits\Macroable; -class Meta implements MetaInterface +class Meta implements MetaInterface, \Stringable { - use Macroable, - Concerns\ManageTitle, - Concerns\ManageMetaTags, - Concerns\ManageLinksTags, - Concerns\ManagePlacements, - Concerns\ManagePackages, - Concerns\ManageAssets, - Concerns\InitializeDefaults; - - const PLACEMENT_HEAD = 'head'; - const PLACEMENT_FOOTER = 'footer'; - - /** - * @var Manager - */ - protected $packageManager; - - /** - * @var Repository|null - */ - private $config; - - /** - * @param Manager $packageManager - * @param Repository|null $config - */ - public function __construct(Manager $packageManager, Repository $config = null) - { - $this->config = $config; - $this->packageManager = $packageManager; - + use Macroable; + use Concerns\ManageTitle; + use Concerns\ManageMetaTags; + use Concerns\ManageLinksTags; + use Concerns\ManagePlacements; + use Concerns\ManagePackages; + use Concerns\ManageAssets; + use Concerns\InitializeDefaults; + + public const PLACEMENT_HEAD = 'head'; + + public const PLACEMENT_FOOTER = 'footer'; + + public function __construct( + protected Manager $packageManager, + private ?Repository $config = null, + ) { $this->initPlacements(); } - /** - * @inheritdoc - */ public function getTag(string $name): ?TagInterface { foreach ($this->getPlacements() as $placement) { @@ -58,20 +41,14 @@ public function getTag(string $name): ?TagInterface return null; } - /** - * @inheritdoc - */ - public function addTag(string $name, TagInterface $tag, ?string $placement = null) + public function addTag(string $name, TagInterface $tag, ?string $placement = null): self { $this->placement($placement ?: $tag->getPlacement())->put($name, $tag); return $this; } - /** - * @inheritdoc - */ - public function registerTags(TagsCollection $tags, ?string $placement = null) + public function registerTags(TagsCollection $tags, ?string $placement = null): self { foreach ($tags as $name => $tag) { $this->addTag($name, $tag, $placement); @@ -80,10 +57,7 @@ public function registerTags(TagsCollection $tags, ?string $placement = null) return $this; } - /** - * @inheritdoc - */ - public function removeTag(string $name) + public function removeTag(string $name): self { foreach ($this->getPlacements() as $placement) { $placement->forget($name); @@ -92,10 +66,7 @@ public function removeTag(string $name) return $this; } - /** - * @inheritdoc - */ - public function reset() + public function reset(): self { foreach ($this->getPlacements() as $placement) { $placement->reset(); @@ -106,24 +77,19 @@ public function reset() /** * Get content as a string of HTML. - * @return string */ - public function toHtml() + public function toHtml(): string { return $this->head()->toHtml(); } - public function __toString() + public function __toString(): string { return $this->toHtml(); } /** * Remove HTML tags - * - * @param string $string - * - * @return string */ protected function cleanString(?string $string): string { @@ -133,22 +99,17 @@ protected function cleanString(?string $string): string /** * Get value from config repository * If config repository is not set, it returns default value - * - * @param string $key - * @param mixed|null $default - * - * @return mixed|null */ - protected function config(string $key, $default = null) + protected function config(string $key, mixed $default = null): mixed { - if (!$this->config) { + if ($this->config === null) { return $default; } - return $this->config->get('meta_tags.'.$key, $default); + return $this->config->get('meta_tags.' . $key, $default); } - public function toArray() + public function toArray(): array { return $this->placements->toArray(); } diff --git a/src/MetaTags/Placement.php b/src/MetaTags/Placement.php index 642b704..3857f9c 100644 --- a/src/MetaTags/Placement.php +++ b/src/MetaTags/Placement.php @@ -5,7 +5,7 @@ use Butschster\Head\Contracts\MetaTags\PlacementInterface; use Illuminate\Contracts\Support\Htmlable; -class Placement extends TagsCollection implements PlacementInterface +class Placement extends TagsCollection implements PlacementInterface, \Stringable { /** * Clear bag @@ -17,9 +17,8 @@ public function reset(): void /** * Get content as a string of HTML. - * @return string */ - public function toHtml() + public function toHtml(): string { return $this ->onlyVisible() @@ -27,10 +26,7 @@ public function toHtml() ->implode(PHP_EOL); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } diff --git a/src/MetaTags/PlacementsBag.php b/src/MetaTags/PlacementsBag.php index 1114899..8a2b284 100644 --- a/src/MetaTags/PlacementsBag.php +++ b/src/MetaTags/PlacementsBag.php @@ -11,16 +11,11 @@ class PlacementsBag implements Arrayable { /** * The array of the view error bags. - * - * @var array */ - protected $bags = []; + protected array $bags = []; /** * Get a Placement instance from the bags. - * - * @param string $key - * @return PlacementInterface */ public function getBag(string $key): PlacementInterface { @@ -29,28 +24,19 @@ public function getBag(string $key): PlacementInterface /** * Create a new Placement - * - * @param string $key - * - * @return Placement */ - public function makeBug(string $key) + public function makeBug(string $key): PlacementInterface { return $this->bags[$key] = new Placement(); } - /** - * @return array - */ public function all(): array { return $this->bags; } - public function toArray() + public function toArray(): array { - return array_map(function (Placement $placement) { - return $placement->toArray(); - }, $this->bags); + return array_map(static fn(Placement $placement) => $placement->toArray(), $this->bags); } } diff --git a/src/MetaTags/TagsCollection.php b/src/MetaTags/TagsCollection.php index bcc3a86..c62cbf6 100644 --- a/src/MetaTags/TagsCollection.php +++ b/src/MetaTags/TagsCollection.php @@ -7,34 +7,33 @@ use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Collection; +/** + * @template T of TagInterface + */ class TagsCollection extends Collection { /** * Filter only visible tags - * @return TagsCollection */ - public function onlyVisible() + public function onlyVisible(): self { - return $this->filter(function (TagInterface $tag) { + return $this->filter(static function (TagInterface $tag) { if ($tag instanceof HasVisibilityConditions) { return $tag->isVisible(); } - return true; }); } /** * Convert to string entities - * @return TagsCollection */ - public function stringifyEntities() + public function stringifyEntities(): self { - return $this->map(function ($tag) { + return $this->map(static function ($tag) { if ($tag instanceof Htmlable) { return $tag->toHtml(); } - return (string)$tag; }); } @@ -42,22 +41,20 @@ public function stringifyEntities() /** * Set the item at a given offset. * - * @param mixed $key * @param TagInterface $value - * @return void */ - public function offsetSet($key, $value): void + public function offsetSet(mixed $key, mixed $value): void { if (!$value instanceof TagInterface) { throw new \InvalidArgumentException( - 'Tge tag nust implement of Butschster\Head\Contracts\MetaTags\Entities\TagInterface interface.' + 'The tag must implement of Butschster\Head\Contracts\MetaTags\Entities\TagInterface interface.', ); } parent::offsetSet($key, $value); } - public function toArray() + public function toArray(): array { return array_values(parent::toArray()); } diff --git a/src/MetaTags/Viewport.php b/src/MetaTags/Viewport.php index 80b34b5..6fb4020 100644 --- a/src/MetaTags/Viewport.php +++ b/src/MetaTags/Viewport.php @@ -9,5 +9,5 @@ class Viewport * that screen is 320px wide, the browser window will be 320px wide, rather than way zoomed out and showing 960px * (or whatever that device does by default, in lieu of a responsive meta tag). */ - const RESPONSIVE = 'width=device-width, initial-scale=1'; + public const RESPONSIVE = 'width=device-width, initial-scale=1'; } \ No newline at end of file diff --git a/src/Packages/Concerns/Dependencies.php b/src/Packages/Concerns/Dependencies.php index 430cade..593f95e 100644 --- a/src/Packages/Concerns/Dependencies.php +++ b/src/Packages/Concerns/Dependencies.php @@ -4,38 +4,24 @@ trait Dependencies { - /** - * @var array - */ - protected $dependencies = []; + protected array $dependencies = []; - /** - * @param array|string $packages - * - * @return $this - */ - public function requires($packages) + public function requires(array|string $packages): self { $packages = is_array($packages) ? $packages : func_get_args(); - $this->dependencies = (array) $packages; + $this->dependencies = $packages; return $this; } - /** - * @return array - */ public function getDependencies(): array { return array_unique($this->dependencies); } - /** - * @return bool - */ - public function hasDependencies() + public function hasDependencies(): bool { return count($this->dependencies) > 0; } -} \ No newline at end of file +} diff --git a/src/Packages/Entities/Concerns/ManageMeta.php b/src/Packages/Entities/Concerns/ManageMeta.php index 2c18e71..1dc2ac4 100644 --- a/src/Packages/Entities/Concerns/ManageMeta.php +++ b/src/Packages/Entities/Concerns/ManageMeta.php @@ -7,10 +7,7 @@ trait ManageMeta { - /** - * @var TagsCollection - */ - protected $tags; + protected TagsCollection $tags; protected function initTags(): void { @@ -19,8 +16,6 @@ protected function initTags(): void /** * Get the collection of tags - * - * @return TagsCollection */ public function getTags(): TagsCollection { @@ -29,14 +24,8 @@ public function getTags(): TagsCollection /** * Add custom meta tag - * - * @param string $key - * - * @param string $content - * - * @return $this */ - public function addMeta(string $key, string $content) + public function addMeta(string $key, string $content): self { $key = $this->prefix.$key; @@ -50,23 +39,16 @@ public function addMeta(string $key, string $content) /** * Get content as a string of HTML. - * - * @return string */ - public function toHtml() + public function toHtml(): string { return $this->tags - ->map(function ($tag) { - return $tag->toHtml(); - }) + ->map(static fn($tag) => $tag->toHtml()) ->implode(PHP_EOL); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->toHtml(); } -} \ No newline at end of file +} diff --git a/src/Packages/Entities/OpenGraphPackage.php b/src/Packages/Entities/OpenGraphPackage.php index ccb539d..b46fd8d 100644 --- a/src/Packages/Entities/OpenGraphPackage.php +++ b/src/Packages/Entities/OpenGraphPackage.php @@ -4,7 +4,6 @@ use Butschster\Head\Contracts\Packages\PackageInterface; use Butschster\Head\MetaTags\Entities\Tag; -use Butschster\Head\Packages\Entities\Concerns\ManageMeta; /** * @see http://ogp.me/ @@ -12,30 +11,15 @@ class OpenGraphPackage implements PackageInterface { use Concerns\ManageMeta; + protected string $prefix = 'og:'; - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $prefix = 'og:'; - - /** - * @param string $name - */ - public function __construct(string $name) + public function __construct(protected string $name) { - $this->name = $name; $this->initTags(); } /** * Get the package name - * - * @return string */ public function getName(): string { @@ -45,24 +29,16 @@ public function getName(): string /** * Set the type of your object, e.g., "video.movie". Depending on the type you specify, other properties may * also be required. - * - * @param string $type - * - * @return $this */ - public function setType(string $type) + public function setType(string $type): self { return $this->addOgMeta('type', $type); } /** * Set the title of your object as it should appear within the graph, e.g., "The Rock". - * - * @param string $title - * - * @return $this */ - public function setTitle(string $title) + public function setTitle(string $title): self { return $this->addOgMeta('title', $title); } @@ -71,12 +47,8 @@ public function setTitle(string $title) * Set the description * * A one to two sentence description of your object. - * - * @param string $description - * - * @return $this */ - public function setDescription(string $description) + public function setDescription(string $description): self { return $this->addOgMeta('description', $description); } @@ -84,25 +56,16 @@ public function setDescription(string $description) /** * Set the site name * If your object is part of a larger web site, the name which should be displayed for the overall site. e.g., "IMDb". - * - * @param string $name - * - * @return $this */ - public function setSiteName(string $name) + public function setSiteName(string $name): self { return $this->addOgMeta('site_name', $name); } /** * Add an image URL which should represent your object within the graph. - * - * @param string $url - * @param array $properties - * - * @return $this */ - public function addImage(string $url, array $properties = []) + public function addImage(string $url, array $properties = []): self { $this->addOgMeta('image', $url); @@ -114,14 +77,9 @@ public function addImage(string $url, array $properties = []) } /** - * Add an video URL - * - * @param string $url - * @param array $properties - * - * @return $this + * Add a video URL */ - public function addVideo(string $url, array $properties = []) + public function addVideo(string $url, array $properties = []): self { $this->addOgMeta('video', $url); @@ -134,44 +92,35 @@ public function addVideo(string $url, array $properties = []) /** * Set the canonical URL of your object that will be used as its permanent ID in the graph. - * - * @param string $url - * - * @return $this */ - public function setUrl(string $url) + public function setUrl(string $url): self { return $this->addOgMeta('url', $url); } /** * Set the locale these tags are marked up in. Of the format language_TERRITORY - * - * @param string $locale - * - * @return $this */ - public function setLocale(string $locale) + public function setLocale(string $locale): self { return $this->addOgMeta('locale', $locale); } /** * Set other locales this page are available in. - * - * @param string[] $locales - * - * @return $this */ - public function addAlternateLocale(string ...$locales) + public function addAlternateLocale(string ...$locales): self { foreach ($locales as $locale) { $key = $this->prefix . 'locale:alternate'; - $this->tags->put($key . $locale, Tag::meta([ - 'property' => $key, - 'content' => $locale, - ])); + $this->tags->put( + $key . $locale, + Tag::meta([ + 'property' => $key, + 'content' => $locale, + ]), + ); } return $this; @@ -179,27 +128,24 @@ public function addAlternateLocale(string ...$locales) /** * Add custom meta tag - * - * @param string $key - * - * @param string $content - * - * @return $this */ - public function addOgMeta(string $key, string $content) + public function addOgMeta(string $key, string $content): self { - $key = $this->prefix.$key; + $key = $this->prefix . $key; - $this->tags->put($key, Tag::meta([ - 'property' => $key, - 'content' => $content, - ])); + $this->tags->put( + $key, + Tag::meta([ + 'property' => $key, + 'content' => $content, + ]), + ); return $this; } - public function toArray() + public function toArray(): array { - return $this->tags->toArray(); + return $this->tags->toArray(); } } diff --git a/src/Packages/Entities/TwitterCardPackage.php b/src/Packages/Entities/TwitterCardPackage.php index 693dd7f..0454f18 100644 --- a/src/Packages/Entities/TwitterCardPackage.php +++ b/src/Packages/Entities/TwitterCardPackage.php @@ -11,34 +11,23 @@ class TwitterCardPackage implements TwitterCardPackageInterface { use Concerns\ManageMeta; - const CARD_SUMMARY = 'summary'; - const CARD_SUMMARY_LARGE_IMAGE = 'summary_large_image'; - const CARD_APP = 'app'; - const CARD_PLAYER = 'player'; - - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $prefix = 'twitter:'; - - /** - * @param string $name - */ - public function __construct(string $name) + public const CARD_SUMMARY = 'summary'; + + public const CARD_SUMMARY_LARGE_IMAGE = 'summary_large_image'; + + public const CARD_APP = 'app'; + + public const CARD_PLAYER = 'player'; + + protected string $prefix = 'twitter:'; + + public function __construct(protected string $name) { - $this->name = $name; $this->initTags(); } /** * Get the package name - * - * @return string */ public function getName(): string { @@ -48,36 +37,24 @@ public function getName(): string /** * Set the type of the card * The card type, which will be one of “summary”, “summary_large_image”, “app”, or “player”. - * - * @param string $type - * - * @return $this */ - public function setType(string $type) + public function setType(string $type): self { return $this->addMeta('card', $type); } /** * Set the @username for the content creator / author. - * - * @param string $username - * - * @return $this */ - public function setCreator(string $username) + public function setCreator(string $username): self { return $this->addMeta('creator', $username); } /** * Set the @username for the website used in the card footer. - * - * @param string $site - * - * @return $this */ - public function setSite(string $site) + public function setSite(string $site): self { return $this->addMeta('site', $site); } @@ -93,12 +70,8 @@ public function setSite(string $site) * * @link https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary * @link https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image - * - * @param string $url - * - * @return $this */ - public function setImage(string $url) + public function setImage(string $url): self { return $this->addMeta('image', $url); } @@ -109,7 +82,7 @@ public function setImage(string $url) * * @deprecated */ - public function addImage(string $url) + public function addImage(string $url): self { return $this->setImage($url); } @@ -119,13 +92,8 @@ public function addImage(string $url) * Add a video URL * * @link https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/player-card - * - * @param string $url - * @param array $properties - * - * @return $this */ - public function setVideo(string $url, array $properties = []) + public function setVideo(string $url, array $properties = []): self { $this->addMeta('player', $url); @@ -139,12 +107,8 @@ public function setVideo(string $url, array $properties = []) /** * Set the title - * - * @param string $title - * - * @return $this */ - public function setTitle(string $title) + public function setTitle(string $title): self { return $this->addMeta('title', $title); } @@ -154,17 +118,13 @@ public function setTitle(string $title) * * A description that concisely summarizes the content as appropriate for presentation within a Tweet. You should * not re-use the title as the description or use this field to describe the general services provided by the website. - * - * @param string $description - * - * @return $this */ - public function setDescription(string $description) + public function setDescription(string $description): self { return $this->addMeta('description', $description); } - public function toArray() + public function toArray(): array { return $this->tags->toArray(); } diff --git a/src/Packages/Manager.php b/src/Packages/Manager.php index e27ba8e..14b0a9d 100644 --- a/src/Packages/Manager.php +++ b/src/Packages/Manager.php @@ -9,53 +9,38 @@ class Manager implements ManagerInterface { - /** - * @var Collection - */ - protected $packages; + protected Collection $packages; public function __construct() { $this->packages = new Collection(); } - /** - * @inheritdoc - */ - public function register(PackageInterface $package) + public function register(PackageInterface $package): self { $this->packages->put($package->getName(), $package); return $this; } - /** - * @inheritdoc - */ - public function create(string $name, Closure $callback = null) + public function create(string $name, Closure $callback = null): self { $this->register($package = new Package($name)); - if ($callback) { + if ($callback !== null) { $callback->__invoke($package); } return $this; } - /** - * @inheritdoc - */ public function getPackages(): array { return $this->packages->all(); } - /** - * @inheritdoc - */ public function getPackage(string $name): ?PackageInterface { return $this->packages->get($name); } -} \ No newline at end of file +} diff --git a/src/Packages/Package.php b/src/Packages/Package.php index adea78d..17f24ec 100644 --- a/src/Packages/Package.php +++ b/src/Packages/Package.php @@ -13,35 +13,19 @@ class Package extends Meta implements PackageInterface, WithDependencies { use Dependencies; - /** - * @var string - */ - protected $name; - - /** - * @param string $name - */ - public function __construct(string $name) - { - $this->name = $name; + public function __construct( + protected string $name, + ) { $this->placements = new PlacementsBag(); } - /** - * @inheritdoc - */ public function getName(): string { return $this->name; } - /** - * Get the collection of tags - * - * @return TagsCollection - */ public function getTags(): TagsCollection { return new TagsCollection(); } -} \ No newline at end of file +} diff --git a/src/Providers/MetaTagsApplicationServiceProvider.php b/src/Providers/MetaTagsApplicationServiceProvider.php index c1cb3fa..cebc92e 100644 --- a/src/Providers/MetaTagsApplicationServiceProvider.php +++ b/src/Providers/MetaTagsApplicationServiceProvider.php @@ -11,18 +11,14 @@ class MetaTagsApplicationServiceProvider extends ServiceProvider { - /** - * Bootstrap any application services. - * @return void - */ - public function boot() + public function boot(): void { if ($this->app->bound('blade.compiler')) { $this->registerBladeDirectives(); } } - public function register() + public function register(): void { $this->registerPackageManager(); $this->registerMeta(); @@ -38,30 +34,32 @@ protected function packages() protected function registerMeta(): void { $this->app->singleton(MetaInterface::class, function () { + $manager = $this->app->get(ManagerInterface::class); + \assert($manager instanceof ManagerInterface); + + $config = $this->app->get('config'); + $meta = new Meta( - $this->app[ManagerInterface::class], - $this->app['config'] + $manager, + $config ); return $meta->initialize(); }); } - protected function registerPackageManager() + protected function registerPackageManager(): void { - $this->app->singleton(ManagerInterface::class, function () { - return new Manager(); - }); + $this->app->singleton(ManagerInterface::class, static fn() => new Manager()); } - protected function registerBladeDirectives() + protected function registerBladeDirectives(): void { - Blade::directive('meta_tags', function($expression) { + Blade::directive('meta_tags', static function ($expression) { if (empty($expression)) { - return ""; + return ''; } - - return "toHtml(); ?>"; + return sprintf('toHtml(); ?>', $expression); }); } -} \ No newline at end of file +} diff --git a/src/Providers/MetaTagsBootstrapServiceProvider.php b/src/Providers/MetaTagsBootstrapServiceProvider.php index 8eb41ba..df10b26 100644 --- a/src/Providers/MetaTagsBootstrapServiceProvider.php +++ b/src/Providers/MetaTagsBootstrapServiceProvider.php @@ -7,43 +7,30 @@ class MetaTagsBootstrapServiceProvider extends ServiceProvider { - /** - * Bootstrap any package services. - * - * @return void - */ - public function boot() + public function boot(): void { if ($this->app->runningInConsole()) { $this->registerPublishing(); } } - /** - * Register any application services. - * - * @return void - */ - public function register() + public function register(): void { $this->commands([ InstallCommand::class, ]); } - /** - * Register the package's publishable resources. - * - * @return void - */ - protected function registerPublishing() + protected function registerPublishing(): void { + /** @psalm-suppress UndefinedFunction */ $this->publishes([ __DIR__.'/../Console/stubs/MetaTagsServiceProvider.stub' => app_path('Providers/MetaTagsServiceProvider.php'), ], 'meta-tags-provider'); + /** @psalm-suppress UndefinedFunction */ $this->publishes([ __DIR__.'/../../config/meta_tags.php' => config_path('meta_tags.php'), ], 'meta-tags-config'); } -} \ No newline at end of file +} diff --git a/src/Providers/MetaTagsServiceProvider.php b/src/Providers/MetaTagsServiceProvider.php index 01c68c0..80c337f 100644 --- a/src/Providers/MetaTagsServiceProvider.php +++ b/src/Providers/MetaTagsServiceProvider.php @@ -6,17 +6,17 @@ class MetaTagsServiceProvider extends ServiceProvider { - /** - * @return void - */ - public function boot() + public function boot(): void { if ($this->app->runningInConsole()) { $this->app->register(MetaTagsBootstrapServiceProvider::class); } + /** + * @psalm-suppress UndefinedInterfaceMethod + */ if (!$this->app->configurationIsCached()) { $this->mergeConfigFrom(__DIR__ . '/../../config/meta_tags.php', 'meta_tags'); } } -} \ No newline at end of file +} diff --git a/tests/MetaTags/DescriptionMetaTagsTest.php b/tests/MetaTags/DescriptionMetaTagsTest.php index 7efcb70..31bab42 100644 --- a/tests/MetaTags/DescriptionMetaTagsTest.php +++ b/tests/MetaTags/DescriptionMetaTagsTest.php @@ -14,6 +14,14 @@ function test_description_can_be_set() ); } + function test_get_description() + { + $this->assertSame( + 'test description', + $this->makeMetaTags()->setDescription('test description')->getDescription()->getDescription() + ); + } + function test_description_can_be_null() { $this->assertHtmlableEquals( diff --git a/tests/MetaTags/Entities/GoogleAnalyticsTest.php b/tests/MetaTags/Entities/GoogleAnalyticsTest.php index 6e13d21..84c63d6 100644 --- a/tests/MetaTags/Entities/GoogleAnalyticsTest.php +++ b/tests/MetaTags/Entities/GoogleAnalyticsTest.php @@ -24,6 +24,13 @@ function test_it_can_be_created() , $tag->toHtml()); } + public function test_get_id(): void + { + $tag = new GoogleAnalytics('UA-12345678-1'); + + $this->assertSame('UA-12345678-1', $tag->getCounterId()); + } + function test_it_can_be_add_to_meta_class() { $meta = $this->makeMetaTags(); diff --git a/tests/MetaTags/Entities/GoogleTagManagerTest.php b/tests/MetaTags/Entities/GoogleTagManagerTest.php index ee0e2f1..7d5838c 100644 --- a/tests/MetaTags/Entities/GoogleTagManagerTest.php +++ b/tests/MetaTags/Entities/GoogleTagManagerTest.php @@ -18,6 +18,13 @@ function test_it_can_be_created() ], $tag); } + public function test_get_id(): void + { + $tag = new GoogleTagManager('UA-12345678-1'); + + $this->assertSame('UA-12345678-1', $tag->getCounterId()); + } + function test_it_can_be_add_to_meta_class() { $meta = $this->makeMetaTags(); diff --git a/tests/MetaTags/Entities/JavascriptVariablesTest.php b/tests/MetaTags/Entities/JavascriptVariablesTest.php index 74a451b..1f536e3 100644 --- a/tests/MetaTags/Entities/JavascriptVariablesTest.php +++ b/tests/MetaTags/Entities/JavascriptVariablesTest.php @@ -23,7 +23,8 @@ function test_js_vars_can_be_rendered() 'object_to_string' => new ToStringTransformable(), ]); - $this->assertHtmlableEquals(<<assertHtmlableEquals( + << window.array = ["jquery","vuejs"]; window.string = 'Hello world'; @@ -35,7 +36,9 @@ function test_js_vars_can_be_rendered() window.object_to_string = 'Hello world'; VAR -, $container); + , + $container + ); } function test_a_variable_can_be_put() @@ -46,14 +49,16 @@ function test_a_variable_can_be_put() ->put('string', 'Hello world') ->put('json', new JsonSerilizable()); - $this->assertHtmlableEquals(<<assertHtmlableEquals( + << window.string = 'Hello world'; window.json = {"hello":"world"}; VAR - , $container); - + , + $container + ); } function test_a_namespace_can_be_set_by_constructor() @@ -63,14 +68,17 @@ function test_a_namespace_can_be_set_by_constructor() 'number' => 4815162342, ], 'custom'); - $this->assertHtmlableEquals(<<assertHtmlableEquals( + << window.custom = window.custom || {}; custom.string = 'Hello world'; custom.number = 4815162342; VAR - , $container); + , + $container + ); } function test_throw_an_exception_if_variable_can_not_transformable() @@ -101,15 +109,17 @@ function test_it_can_be_add_to_meta_object() $meta->addTag('variables', $container); - $this->assertHtmlableEquals(<<assertHtmlableEquals( + << window.custom = window.custom || {}; custom.string = 'Hello world'; custom.number = 4815162342; VAR - , $meta); - + , + $meta + ); } function test_by_default_variables_is_visible() @@ -124,14 +134,23 @@ function test_visibility_condition_can_be_set() $container = new JavascriptVariables(); // Make it invisible - $this->assertFalse($container->visibleWhen(function () {return false;})->isVisible()); + $this->assertFalse( + $container->visibleWhen(function () { + return false; + })->isVisible() + ); // Make it visible - $this->assertTrue($container->visibleWhen(function () {return true;})->isVisible()); + $this->assertTrue( + $container->visibleWhen(function () { + return true; + })->isVisible() + ); } } -class NonTransformable { +class NonTransformable +{ } @@ -140,7 +159,7 @@ class JsonSerilizable implements Jsonable public function toJson($options = 0) { return json_encode([ - 'hello' => 'world' + 'hello' => 'world', ]); } } @@ -150,7 +169,7 @@ class ArraySerializable implements Arrayable public function toArray() { return [ - 'hello' => 'world' + 'hello' => 'world', ]; } } diff --git a/tests/MetaTags/Entities/KeywordsTest.php b/tests/MetaTags/Entities/KeywordsTest.php index c41a94e..e821cb8 100644 --- a/tests/MetaTags/Entities/KeywordsTest.php +++ b/tests/MetaTags/Entities/KeywordsTest.php @@ -15,6 +15,14 @@ function test_it_can_be_rendered_to_html() ); } + public function test_get_keywords(): void + { + $this->assertSame( + ['tag, another-tag'], + (new Keywords('tag, another-tag'))->getKeywords() + ); + } + function test_keywords_can_be_set_from_array_be_rendered_to_html() { $this->assertHtmlableEquals( @@ -45,12 +53,12 @@ function test_it_can_support_utf8() function test_if_max_length_is_null_then_text_should_not_cut() { - $text = 'Lorem ipsum dolor sit amet, ei omnium accommodare definitionem sed, cum ut esse nihil fabellas. - Ne eam autem nihil eloquentiam, eius ornatus no ius. Sint oportere scripserit vel cu, eu nec debitis - mediocrem gubergren. Vim sonet sensibus ea, est justo adipisci constituam ex. Percipitur voluptatibus + $text = 'Lorem ipsum dolor sit amet, ei omnium accommodare definitionem sed, cum ut esse nihil fabellas. + Ne eam autem nihil eloquentiam, eius ornatus no ius. Sint oportere scripserit vel cu, eu nec debitis + mediocrem gubergren. Vim sonet sensibus ea, est justo adipisci constituam ex. Percipitur voluptatibus usu ad. Ea audiam ornatus eum, eros homero ridens et vim.'; $keywords = new Keywords($text); $this->assertHtmlableContains($text, $keywords); } -} \ No newline at end of file +} diff --git a/tests/MetaTags/Entities/ScriptTest.php b/tests/MetaTags/Entities/ScriptTest.php index b81f3bf..05279e5 100644 --- a/tests/MetaTags/Entities/ScriptTest.php +++ b/tests/MetaTags/Entities/ScriptTest.php @@ -16,6 +16,16 @@ function test_it_can_be_created() ); } + function test_get_name() + { + $this->assertEquals('script_name', (new Script('script_name', 'http://site.com'))->getName()); + } + + function test_get_src() + { + $this->assertEquals('http://site.com', (new Script('script_name', 'http://site.com'))->getSrc()); + } + function test_it_can_has_attributes() { $this->assertHtmlableEquals( diff --git a/tests/MetaTags/Entities/StyleTest.php b/tests/MetaTags/Entities/StyleTest.php index 83fa78e..1cbe805 100644 --- a/tests/MetaTags/Entities/StyleTest.php +++ b/tests/MetaTags/Entities/StyleTest.php @@ -2,11 +2,30 @@ namespace Butschster\Tests\MetaTags\Entities; +use Butschster\Head\MetaTags\Entities\Script; use Butschster\Head\MetaTags\Entities\Style; use Butschster\Tests\TestCase; class StyleTest extends TestCase { + function test_get_name() + { + $this->assertEquals('style_name', (new Style('style_name', 'http://site.com', [ + 'type' => 'text/test', + 'media' => 'test', + 'rel' => 'css' + ]))->getName()); + } + + function test_get_src() + { + $this->assertEquals('http://site.com', (new Style('style_name', 'http://site.com', [ + 'type' => 'text/test', + 'media' => 'test', + 'rel' => 'css' + ]))->getSrc()); + } + function test_it_can_be_created() { $this->assertHtmlableEquals( @@ -47,4 +66,4 @@ function test_if_rek_attribute_not_set_use_default_value() new Style('style_name', 'http://site.com') ); } -} \ No newline at end of file +} diff --git a/tests/MetaTags/FaviconMetaTagsTest.php b/tests/MetaTags/FaviconMetaTagsTest.php index 77f8662..ad4b933 100644 --- a/tests/MetaTags/FaviconMetaTagsTest.php +++ b/tests/MetaTags/FaviconMetaTagsTest.php @@ -2,6 +2,7 @@ namespace Butschster\Tests\MetaTags; +use Butschster\Head\MetaTags\Entities\Favicon; use Butschster\Tests\TestCase; class FaviconMetaTagsTest extends TestCase @@ -14,6 +15,14 @@ function test_favicon_can_be_set() ); } + function test_get_href() + { + $this->assertSame( + 'http://example.com/favicon.ico', + (new Favicon('http://example.com/favicon.ico'))->getHref() + ); + } + function test_favicon_can_have_attributes() { $this->assertHtmlableContains( diff --git a/tests/MetaTags/TitleMetaTagsTest.php b/tests/MetaTags/TitleMetaTagsTest.php index 20e02f2..806da2b 100644 --- a/tests/MetaTags/TitleMetaTagsTest.php +++ b/tests/MetaTags/TitleMetaTagsTest.php @@ -17,6 +17,17 @@ function test_title_can_be_set() ); } + function test_get_title() + { + $meta = $this->makeMetaTags() + ->setTitle('test title'); + + $this->assertSame( + 'test title', + $meta->getTitle()->getTitle() + ); + } + function test_title_can_be_null() { $meta = $this->makeMetaTags()