GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 14b885...ce7f54 )
by Pascal
01:06
created

BladeOnDemandRenderer::addMissingVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
namespace ProtoneMedia\BladeOnDemand;
4
5
use Illuminate\Mail\Markdown;
6
use Illuminate\View\Factory as ViewFactory;
7
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
8
9
class BladeOnDemandRenderer
10
{
11
    /**
12
     * @var \Illuminate\View\Factory
13
     */
14
    private $viewFactory;
15
16
    /**
17
     * @var \Illuminate\Mail\Markdown
18
     */
19
    private $markdown;
20
21
    /**
22
     * @var \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles
23
     */
24
    private $cssInliner;
25
26
    /**
27
     * Wether to fill the missing variables from the template.
28
     *
29
     * @var boolean
30
     */
31
    private $fillMissingVariables = false;
32
33
    public function __construct(ViewFactory $viewFactory, Markdown $markdown, CssToInlineStyles $cssInliner)
34
    {
35
        $this->viewFactory = $viewFactory;
36
        $this->markdown    = $markdown;
37
        $this->cssInliner  = $cssInliner;
38
    }
39
40
    /**
41
     * Fills the missing variables in the template
42
     *
43
     * @param callable $callback
44
     * @return $this
45
     */
46
    public function fillMissingVariables(callable $callback = null)
47
    {
48
        $this->fillMissingVariables = $callback ?: true;
0 ignored issues
show
Documentation Bug introduced by
It seems like $callback ?: true can also be of type callable. However, the property $fillMissingVariables is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
50
        return $this;
51
    }
52
53
    /**
54
     * Renders the content with the given data.
55
     *
56
     * @param string $contents
57
     * @param array $data
58
     * @return string
59
     */
60
    public function render(string $contents, array $data = []): string
61
    {
62
        if ($this->fillMissingVariables) {
63
            $data = $this->addMissingVariables($contents, $data);
64
        }
65
66
        file_put_contents(
67
            $path = tempnam(sys_get_temp_dir(), 'blade-on-demand') . '.blade.php',
68
            $contents
69
        );
70
71
        $this->viewFactory->flushFinderCache();
72
73
        return tap($this->viewFactory->file($path, $data)->render(), function () use ($path) {
74
            unlink($path);
75
76
            $this->fillMissingVariables = false;
77
        });
78
    }
79
80
    /**
81
     * Finds all missing variables.
82
     * Source: https://stackoverflow.com/a/19563063
83
     *
84
     * @param string $contents
85
     * @param array $data
86
     * @return array
87
     */
88
    public function getMissingVariables(string $contents, array $data = []): array
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

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

Loading history...
89
    {
90
        $pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
91
92
        preg_match_all($pattern, $contents, $matches);
93
94
        return $matches[1] ?? [];
95
    }
96
97
    /**
98
     * Makes sure each variable is present in the data array.
99
     *
100
     * @param string $contents
101
     * @param array $data
102
     * @return array
103
     */
104
    private function addMissingVariables(string $contents, array $data = []): array
105
    {
106
        foreach (static::getMissingVariables($contents, $data) as $variable) {
107
            if (array_key_exists($variable, $data)) {
108
                continue;
109
            }
110
111
            if (!is_callable($this->fillMissingVariables)) {
112
                $data[$variable] = $variable;
113
                continue;
114
            }
115
116
            $data[$variable] = call_user_func_array($this->fillMissingVariables, [$variable]);
117
        }
118
119
        return $data;
120
    }
121
122
    /**
123
     * Renders the markdown content to a HTML mail.
124
     *
125
     * @param string $contents
126
     * @param array $data
127
     * @return string
128
     */
129
    public function renderMarkdownMailToHtml(string $contents, array $data = []): string
130
    {
131
        $this->viewFactory->replaceNamespace('mail', $this->markdown->htmlComponentPaths());
132
133
        $rendered = $this->render($contents, $data);
134
135
        return $this->cssInliner->convert(
136
            $rendered,
137
            $this->viewFactory->make('mail::themes.' . config('mail.markdown.theme', 'default'))->render()
138
        );
139
    }
140
141
    /**
142
     * Renders the markdown content to a Text mail.
143
     *
144
     * @param string $contents
145
     * @param array $data
146
     * @return string
147
     */
148
    public function renderMarkdownMailToText(string $contents, array $data = []): string
149
    {
150
        $this->viewFactory->replaceNamespace('mail', $this->markdown->textComponentPaths());
151
152
        $rendered = $this->render($contents, $data);
153
154
        return html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $rendered), ENT_QUOTES, 'UTF-8');
155
    }
156
157
    /**
158
     * Parses the markdown content.
159
     *
160
     * @param string $contents
161
     * @param array $data
162
     * @return string
163
     */
164
    public function parseMarkdownMail(string $contents, array $data = []): string
165
    {
166
        return $this->markdown->parse($this->renderMarkdownMailToText($contents, $data));
167
    }
168
}
169