PluginTemplate   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A renderStringTemplate() 0 14 2
B renderPluginTemplate() 0 56 8
1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2020 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\helpers;
13
14
use Craft;
15
use craft\helpers\Template;
16
use craft\web\View;
17
use Twig\Markup;
18
use yii\base\Exception;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.1.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
class PluginTemplate
26
{
27
    // Static Methods
28
    // =========================================================================
29
30
    public static function renderStringTemplate(string $templateString, array $params = []): string
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
31
    {
32
        try {
33
            $html = Craft::$app->getView()->renderString($templateString, $params);
34
        } catch (\Exception $exception) {
35
            $html = Craft::t(
36
                'recipe',
37
                'Error rendering template string -> {error}',
38
                ['error' => $exception->getMessage()]
39
            );
40
            Craft::error($html, __METHOD__);
41
        }
42
43
        return $html;
44
    }
45
46
    /**
47
     * Render a plugin template
48
     *
49
     * @param string $templatePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
50
     * @param array $params
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...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
51
     * @return Markup
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
52
     */
53
    public static function renderPluginTemplate(string $templatePath, array $params = []): Markup
54
    {
55
        $exception = null;
56
        $htmlText = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $htmlText is dead and can be removed.
Loading history...
57
        // Stash the old template mode, and set it Control Panel template mode
58
        $oldMode = Craft::$app->view->getTemplateMode();
59
        // Look for a frontend template to render first
60
        try {
61
            Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
62
        } catch (Exception $exception) {
63
            Craft::error($exception->getMessage(), __METHOD__);
64
        }
65
66
        // Render the template with our vars passed in
67
        try {
68
            $htmlText = Craft::$app->view->renderTemplate('recipe/' . $templatePath, $params);
69
            $templateRendered = true;
70
        } catch (\Exception $exception) {
71
            $templateRendered = false;
72
        }
73
74
        // If no frontend template was found, try our built-in template
75
        if (!$templateRendered) {
76
            try {
77
                Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
78
            } catch (Exception $exception) {
79
                Craft::error($exception->getMessage(), __METHOD__);
80
            }
81
82
            // Render the template with our vars passed in
83
            try {
84
                $htmlText = Craft::$app->view->renderTemplate('recipe/' . $templatePath, $params);
85
                $templateRendered = true;
86
            } catch (\Exception $exception) {
87
                $templateRendered = false;
88
            }
89
        }
90
91
        // Only if the template didn't render at all should we log an error
92
        if (!$templateRendered) {
93
            $htmlText = Craft::t(
94
                'recipe',
95
                'Error rendering `{template}` -> {error}',
96
                ['template' => $templatePath, 'error' => $exception->getMessage()]
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on null. ( Ignorable by Annotation )

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

96
                ['template' => $templatePath, 'error' => $exception->/** @scrutinizer ignore-call */ getMessage()]

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            );
98
            Craft::error($htmlText, __METHOD__);
99
        }
100
101
        // Restore the old template mode
102
        try {
103
            Craft::$app->view->setTemplateMode($oldMode);
104
        } catch (Exception $exception) {
105
            Craft::error($exception->getMessage(), __METHOD__);
106
        }
107
108
        return Template::raw($htmlText);
109
    }
110
}
111