Passed
Push — v3 ( 1e51d3...178405 )
by Andrew
25:46
created

Helper   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 522
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 89
dl 0
loc 522
ccs 0
cts 201
cp 0
rs 5.04
c 8
b 0
f 0
wmc 57

33 Methods

Rating   Name   Duplication   Size   Complexity  
A sitemapIndexForSiteId() 0 3 1
A siteUrl() 0 3 1
A loadMetadataForUri() 0 3 1
A sanitizeUserInput() 0 3 1
A seoFileLink() 0 22 4
A twitterTransform() 0 9 2
A truncate() 0 3 1
A isPreview() 0 10 3
A paginate() 0 3 1
A siteLinksQueryInput() 0 10 3
A truncateOnWord() 0 3 1
A getLocalizedUrls() 0 3 1
B sameAsByHandle() 0 15 8
A craft31() 0 3 1
A socialTransformWidth() 0 7 1
A socialTransform() 0 3 1
A extractTextFromTags() 0 3 1
A extractTextFromField() 0 3 1
A craft32() 0 3 1
A craft33() 0 3 1
A extractTextFromMatrix() 0 3 1
A getEntityPath() 0 3 1
A socialTransformHeight() 0 7 1
A sitemapUrlForBundle() 0 3 1
A extractSummary() 0 3 1
A siteGroupSitemaps() 0 3 1
A sitemapIndex() 0 3 1
A getSingleTypeMenu() 0 3 1
A getTypeMenu() 0 3 1
A extractKeywords() 0 3 1
A safeCanonicalUrl() 0 11 2
A findInheritableBundle() 0 14 6
A isInherited() 0 10 4

How to fix   Complexity   

Complex Class

Complex classes like Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Helper, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\services;
13
14
use Craft;
15
use craft\base\Component;
16
use craft\elements\Asset;
17
use craft\elements\db\MatrixBlockQuery;
18
use craft\elements\db\TagQuery;
19
use craft\helpers\Template;
20
use craft\web\twig\variables\Paginate;
21
use nystudio107\seomatic\base\InheritableSettingsModel;
22
use nystudio107\seomatic\helpers\DynamicMeta as DynamicMetaHelper;
23
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper;
24
use nystudio107\seomatic\helpers\Schema as SchemaHelper;
25
use nystudio107\seomatic\helpers\Text as TextHelper;
26
use nystudio107\seomatic\helpers\UrlHelper;
27
use nystudio107\seomatic\models\MetaBundle;
28
use nystudio107\seomatic\Seomatic;
29
use yii\base\Exception;
30
use yii\base\InvalidConfigException;
31
32
/**
33
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
34
 * @package   Seomatic
35
 * @since     3.0.0
36
 */
37
class Helper extends Component
38
{
39
    // Constants
40
    // =========================================================================
41
42
    const TWITTER_TRANSFORM_MAP = [
43
            'summary' => 'twitter-summary',
44
            'summary_large_image' => 'twitter-large',
45
            'app' => 'twitter-large',
46
            'player' => 'twitter-large',
47
        ];
48
49
    // Public Methods
50
    // =========================================================================
51
52
    /**
53
     * Sanitize user input by decoding any HTML Entities, URL decoding the text,
54
     * then removing any newlines, stripping tags, stripping Twig tags, and changing
55
     * single {}'s into ()'s
56
     *
57
     * @param $str
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
59
     */
60
    public static function sanitizeUserInput($str): string
61
    {
62
        return TextHelper::sanitizeUserInput($str);
63
    }
64
65
    /**
66
     * Return the appropriate Twitter Transform based on the current $metaGlobalVars->twitterCard
67
     *
68
     * @return string
69
     */
70
    public static function twitterTransform(): string
71
    {
72
        $transform = 'twitter-summary';
73
        $metaGlobalVars = Seomatic::$plugin->metaContainers->metaGlobalVars;
74
        if ($metaGlobalVars) {
0 ignored issues
show
introduced by
$metaGlobalVars is of type nystudio107\seomatic\models\MetaGlobalVars, thus it always evaluated to true.
Loading history...
75
            $transform = self::TWITTER_TRANSFORM_MAP[$metaGlobalVars->twitterCard] ?? $transform;
76
        }
77
78
        return $transform;
79
    }
80
81
    /**
82
     * Return the proper content for the `query-input` JSON-LD property
83
     *
84
     * @return string
85
     */
86
    public static function siteLinksQueryInput(): string
87
    {
88
        $result = '';
89
90
        $metaSiteVars = Seomatic::$plugin->metaContainers->metaSiteVars;
91
        if ($metaSiteVars && !empty($metaSiteVars->siteLinksQueryInput)) {
92
            $result = 'required name='.$metaSiteVars->siteLinksQueryInput;
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Return whether this is a preview request of any kind
100
     *
101
     * @return bool
102
     */
103
    public static function isPreview(): bool
104
    {
105
        $isPreview = false;
106
        $request = Craft::$app->getRequest();
107
        if (Seomatic::$craft32) {
108
            $isPreview = $request->getIsPreview();
109
        }
110
        $isLivePreview = $request->getIsLivePreview();
111
112
        return ($isPreview || $isLivePreview);
113
    }
114
115
    /**
116
     * Return the Same As Links info as an array or null
117
     *
118
     * @param string $handle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
119
     * @return array|null
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
120
     */
121
    public static function sameAsByHandle(string $handle) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
122
        $result = null;
123
124
        $sameAs = Seomatic::$plugin->metaContainers->metaSiteVars->sameAsLinks;
125
        if (!empty($sameAs) && !empty($handle)) {
126
            foreach ($sameAs as $sameAsInfo) {
127
                if (!empty($sameAsInfo) && is_array($sameAsInfo) && !empty($sameAsInfo['handle'])) {
128
                    if ($sameAsInfo['handle'] === $handle) {
129
                        return $sameAsInfo;
130
                    }
131
                }
132
            }
133
        }
134
135
        return $result;
136
    }
137
138
    /**
139
     * Return the canonical URL for the request, with the query string stripped
140
     *
141
     * @return string
142
     */
143
    public static function safeCanonicalUrl(): string
144
    {
145
        $url = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
146
        try {
147
            $url = Craft::$app->getRequest()->getPathInfo();
148
        } catch (InvalidConfigException $e) {
149
            Craft::error($e->getMessage(), __METHOD__);
150
        }
151
        $url = DynamicMetaHelper::sanitizeUrl($url);
152
153
        return UrlHelper::absoluteUrlWithProtocol($url);
154
    }
155
156
    /**
157
     * Return the site URL for a given URL. This gives SEOmatic a chance to override it
158
     *
159
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
160
     * @param array|string|null $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
161
     * @param string|null $scheme
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
162
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
163
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
164
     * @throws Exception if|null $siteId is invalid
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
165
     */
166
    public static function siteUrl(string $path = '', $params = null, string $scheme = null, int $siteId = null): string
0 ignored issues
show
Unused Code introduced by
The parameter $siteId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

166
    public static function siteUrl(string $path = '', $params = null, string $scheme = null, /** @scrutinizer ignore-unused */ int $siteId = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $scheme is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

166
    public static function siteUrl(string $path = '', $params = null, /** @scrutinizer ignore-unused */ string $scheme = null, int $siteId = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

166
    public static function siteUrl(string $path = '', /** @scrutinizer ignore-unused */ $params = null, string $scheme = null, int $siteId = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        return UrlHelper::siteUrl($path);
169
    }
170
171
    /**
172
     * Paginate based on the passed in Paginate variable as returned from the
173
     * Twig {% paginate %} tag:
174
     * https://docs.craftcms.com/v3/templating/tags/paginate.html#the-pageInfo-variable
175
     *
176
     * @param Paginate $pageInfo
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
177
     */
178
    public static function paginate(Paginate $pageInfo)
179
    {
180
        DynamicMetaHelper::paginate($pageInfo);
181
    }
182
183
    /**
184
     * Truncates the string to a given length. If $substring is provided, and
185
     * truncating occurs, the string is further truncated so that the substring
186
     * may be appended without exceeding the desired length.
187
     *
188
     * @param  string $string    The string to truncate
189
     * @param  int    $length    Desired length of the truncated string
190
     * @param  string $substring The substring to append if it can fit
191
     *
192
     * @return string with the resulting $str after truncating
193
     */
194
    public static function truncate($string, $length, $substring = '…'): string
195
    {
196
        return TextHelper::truncate($string, $length, $substring);
197
    }
198
199
    /**
200
     * Truncates the string to a given length, while ensuring that it does not
201
     * split words. If $substring is provided, and truncating occurs, the
202
     * string is further truncated so that the substring may be appended without
203
     * exceeding the desired length.
204
     *
205
     * @param  string $string    The string to truncate
206
     * @param  int    $length    Desired length of the truncated string
207
     * @param  string $substring The substring to append if it can fit
208
     *
209
     * @return string with the resulting $str after truncating
210
     */
211
    public static function truncateOnWord($string, $length, $substring = '…'): string
212
    {
213
        return TextHelper::truncateOnWord($string, $length, $substring);
214
    }
215
216
    /**
217
     * Return a list of localized URLs that are in the current site's group
218
     * The current URI is used if $uri is null. Similarly, the current site is
219
     * used if $siteId is null.
220
     * The resulting array of arrays has `id`, `language`, `ogLanguage`,
221
     * `hreflangLanguage`, and `url` as keys.
222
     *
223
     * @param string|null $uri
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
224
     * @param int|null    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
225
     *
226
     * @return array
227
     */
228
    public static function getLocalizedUrls(string $uri = null, int $siteId = null): array
229
    {
230
        return DynamicMetaHelper::getLocalizedUrls($uri, $siteId);
231
    }
232
233
    /**
234
     * Allow setting the X-Robots-Tag and Link headers on static files as per:
235
     * https://moz.com/blog/how-to-advanced-relcanonical-http-headers
236
     *
237
     * @param        $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
238
     * @param string $robots
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
239
     * @param string $canonical
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
240
     * @param bool   $inline
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
241
     *
242
     * @return \Twig\Markup
243
     * @throws \yii\base\Exception
244
     */
245
    public static function seoFileLink($url, $robots = '', $canonical = '', $inline = true): \Twig\Markup
246
    {
247
        // Get the file name
248
        $path = parse_url($url, PHP_URL_PATH);
249
        $fileName = pathinfo($path, PATHINFO_BASENAME);
250
        // Set some defaults
251
        $robots = empty($robots) ? 'all' : $robots;
252
        $canonical = empty($canonical) ? $url : $canonical;
253
        $inlineStr = $inline === true ? '1' : '0';
254
        // Compose the base64 encoded URL
255
        $seoFileLink = 'seomatic/seo-file-link/'
256
            .base64_encode($url)
257
            .'/'
258
            .base64_encode($robots)
259
            .'/'
260
            .base64_encode($canonical)
261
            .'/'
262
            .$inlineStr
263
            .'/'
264
            .$fileName;
0 ignored issues
show
Bug introduced by
Are you sure $fileName of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

264
            ./** @scrutinizer ignore-type */ $fileName;
Loading history...
265
266
        return Template::raw(UrlHelper::siteUrl($seoFileLink));
267
    }
268
269
    /**
270
     * Load the appropriate meta containers for the given $uri and optional
271
     * $siteId
272
     *
273
     * @param string   $uri
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
274
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
275
     */
276
    public static function loadMetadataForUri(string $uri = '', int $siteId = null)
277
    {
278
        Seomatic::$plugin->metaContainers->loadMetaContainers($uri, $siteId);
279
    }
280
281
    /**
282
     * Get the URL to the $siteId's sitemap index
283
     *
284
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
285
     *
286
     * @return string
287
     */
288
    public static function sitemapIndexForSiteId(int $siteId = null): string
289
    {
290
        return Seomatic::$plugin->sitemaps->sitemapIndexUrlForSiteId($siteId);
291
    }
292
293
    /**
294
     * Return a sitemap for each site in the same site group
295
     *
296
     * @return string
297
     */
298
    public static function sitemapIndex(): string
299
    {
300
        return Seomatic::$plugin->sitemaps->sitemapIndex();
301
    }
302
303
    /**
304
     * Return a sitemap for each site in the same site group
305
     *
306
     * @deprecated use sitemapIndex() instead
307
     * @return string
308
     */
309
    public static function siteGroupSitemaps(): string
310
    {
311
        return Seomatic::$plugin->sitemaps->sitemapIndex();
312
    }
313
314
    /**
315
     * @param string   $sourceType
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
316
     * @param string   $sourceHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
317
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
318
     *
319
     * @return string
320
     */
321
    public static function sitemapUrlForBundle(string $sourceType, string $sourceHandle, int $siteId = null): string
322
    {
323
        return Seomatic::$plugin->sitemaps->sitemapUrlForBundle($sourceType, $sourceHandle, $siteId);
324
    }
325
326
    /**
327
     * Extract plain old text from a field
328
     *
329
     * @param $field
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
330
     *
331
     * @return string
332
     */
333
    public static function extractTextFromField($field = null): string
334
    {
335
        return TextHelper::extractTextFromField($field);
336
    }
337
338
    /**
339
     * Extract concatenated text from all of the tags in the $tagElement and
340
     * return as a comma-delimited string
341
     *
342
     * @param TagQuery $tagQuery
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
343
     *
344
     * @return string
345
     */
346
    public static function extractTextFromTags($tagQuery = null): string
347
    {
348
        return TextHelper::extractTextFromTags($tagQuery);
349
    }
350
351
    /**
352
     * Extract text from all of the blocks in a matrix field, concatenating it
353
     * together.
354
     *
355
     * @param MatrixBlockQuery $matrixQuery
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
356
     * @param string           $fieldHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
357
     *
358
     * @return string
359
     */
360
    public static function extractTextFromMatrix($matrixQuery = null, $fieldHandle = ''): string
361
    {
362
        return TextHelper::extractTextFromMatrix($matrixQuery, $fieldHandle);
363
    }
364
365
    /**
366
     * Return the most important keywords extracted from the text as a comma-
367
     * delimited string
368
     *
369
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
370
     * @param int    $limit
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
371
     * @param bool   $useStopWords
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
372
     *
373
     * @return string
374
     */
375
    public static function extractKeywords($text = '', $limit = 15, $useStopWords = true): string
376
    {
377
        return TextHelper::extractKeywords($text, $limit, $useStopWords);
378
    }
379
380
    /**
381
     * Extract a summary consisting of the 3 most important sentences from the
382
     * text
383
     *
384
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
385
     * @param bool   $useStopWords
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
386
     *
387
     * @return string
388
     */
389
    public static function extractSummary($text = '', $useStopWords = true): string
390
    {
391
        return TextHelper::extractSummary($text, $useStopWords);
392
    }
393
394
    /**
395
     * Return a period-delimited schema.org path from the $settings
396
     *
397
     * @param $settings
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
398
     *
399
     * @return string
400
     */
401
    public static function getEntityPath($settings): string
402
    {
403
        return SchemaHelper::getEntityPath($settings);
404
    }
405
406
    /**
407
     * Return a flattened, indented menu of the given $path
408
     *
409
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
410
     *
411
     * @return array
412
     */
413
    public static function getTypeMenu($path): array
414
    {
415
        return SchemaHelper::getTypeMenu($path);
416
    }
417
418
    /**
419
     * Return a single menu of schemas starting at $path
420
     *
421
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
422
     *
423
     * @return array
424
     */
425
    public static function getSingleTypeMenu($path): array
426
    {
427
        return SchemaHelper::getSingleTypeMenu($path);
428
    }
429
430
    /**
431
     * Transform the $asset for social media sites in $transformName and
432
     * optional $siteId
433
     *
434
     * @param int|Asset $asset         the Asset or Asset ID
435
     * @param string    $transformName the name of the transform to apply
436
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
437
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
438
     *
439
     * @return string URL to the transformed image
440
     */
441
    public function socialTransform($asset, $transformName = '', $siteId = null, $transformMode = null): string
442
    {
443
        return ImageTransformHelper::socialTransform($asset, $transformName, $siteId, $transformMode);
444
    }
445
446
    /**
447
     * Get the width of the transformed social image for $transformName and
448
     * optional $siteId
449
     *
450
     * @param int|Asset $asset         the Asset or Asset ID
451
     * @param string    $transformName the name of the transform to apply
452
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
453
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
454
     *
455
     * @return string URL to the transformed image
456
     */
457
    public function socialTransformWidth(
458
        $asset,
459
        $transformName = '',
460
        $siteId = null,
461
        $transformMode = null
462
    ): string {
463
        return ImageTransformHelper::socialTransformWidth($asset, $transformName, $siteId, $transformMode);
464
    }
465
466
    /**
467
     * Get the height of the transformed social image for $transformName and
468
     * optional $siteId
469
     *
470
     * @param int|Asset $asset         the Asset or Asset ID
471
     * @param string    $transformName the name of the transform to apply
472
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
473
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
474
     *
475
     * @return string URL to the transformed image
476
     */
477
    public function socialTransformHeight(
478
        $asset,
479
        $transformName = '',
480
        $siteId = null,
481
        $transformMode = null
482
    ): string {
483
        return ImageTransformHelper::socialTransformHeight($asset, $transformName, $siteId, $transformMode);
484
    }
485
486
    /**
487
     * Return whether we are running Craft 3.1 or later
488
     *
489
     * @return bool
490
     */
491
    public function craft31(): bool
492
    {
493
        return Seomatic::$craft31;
494
    }
495
496
    /**
497
     * Return whether we are running Craft 3.2 or later
498
     *
499
     * @return bool
500
     */
501
    public function craft32(): bool
502
    {
503
        return Seomatic::$craft32;
504
    }
505
506
    /**
507
     * Return whether we are running Craft 3.3 or later
508
     *
509
     * @return bool
510
     */
511
    public function craft33(): bool
512
    {
513
        return Seomatic::$craft33;
514
    }
515
516
    /**
517
     * Given a list of meta bundles in order of descending distance, return the bundle that has inheritable value.
518
     *
519
     * @param array $inheritedValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
520
     * @param string $settingName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
521
     * @param string $collectionName The name off the collection to search
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
522
     * @return MetaBundle|null
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
523
     * @since 3.4.0
0 ignored issues
show
Coding Style introduced by
Tag @since cannot be grouped with parameter tags in a doc comment
Loading history...
524
     */
525
    public function findInheritableBundle(array $inheritedValues, string $settingName, string $collectionName = "metaGlobalVars")
526
    {
527
        if (in_array($collectionName, ['metaGlobalVars', 'metaSitemapVars'], true)) {
528
            foreach ($inheritedValues as $bundle) {
529
                /** @var $bundle MetaBundle */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
530
                if (isset($bundle->{$collectionName}[$settingName])) {
531
                    if (is_bool($bundle->{$collectionName}[$settingName]) || !empty($bundle->{$collectionName}[$settingName])) {
532
                        return $bundle;
533
                    }
534
                }
535
            }
536
        }
537
538
        return null;
539
    }
540
541
    /**
542
     * Return true if a setting is inherited.
543
     *
544
     * @param InheritableSettingsModel $settingCollection
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
545
     * @param $settingName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
546
     * @return bool
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
547
     * @since 3.4.0
0 ignored issues
show
Coding Style introduced by
Tag @since cannot be grouped with parameter tags in a doc comment
Loading history...
548
     */
549
    public function isInherited(InheritableSettingsModel $settingCollection, $settingName)
550
    {
551
        $explicitInherit = array_key_exists($settingName, $settingCollection->inherited);
552
        $explicitOverride = array_key_exists($settingName, $settingCollection->overrides);
553
554
        if ($explicitInherit || $explicitOverride) {
555
            return $explicitInherit && !$explicitOverride;
556
        }
557
558
        return empty($settingCollection->{$settingName});
559
    }
560
}
561