VariableLocal   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
dl 0
loc 37
rs 10
c 1
b 0
f 1
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 28 5
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Shea Lewis <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Cortex\Commands;
12
13
use Axiom\Rivescript\Contracts\Command;
14
use Axiom\Rivescript\Cortex\Node;
15
16
/**
17
 * VariableLocal class
18
 *
19
 * This class handles the global variable command (! local)
20
 * and stores the definition in memory.
21
 *
22
 * PHP version 7.4 and higher.
23
 *
24
 * @category Core
25
 * @package  Cortext\Commands
26
 * @author   Shea Lewis <[email protected]>
27
 * @license  https://opensource.org/licenses/MIT MIT
28
 * @link     https://github.com/axiom-labs/rivescript-php
29
 * @since    0.3.0
30
 */
31
class VariableLocal implements Command
32
{
33
    /**
34
     * Parse the command.
35
     *
36
     * @param Node $node The node is a line from the Rivescript file.
37
     *
38
     * @return void
39
     */
40
    public function parse(Node $node): void
41
    {
42
        if ($node->command() === '!') {
43
            $type = strtok($node->value(), ' ');
44
45
            if ($type === 'local') {
46
                $value = str_replace('local', '', $node->value());
47
                [$key, $value] = explode('=', $value);
48
49
                $valid = [
50
                    'concat' => [
51
                        'none',
52
                        'space',
53
                        'newline'
54
                    ]
55
                ];
56
57
                $key = trim($key);
58
                $value = trim($value);
59
60
                if (isset($valid[$key]) === true) {
61
                    $default = current($valid[$key]);
62
63
                    if (in_array($value, $valid[$key]) === false) {
64
                        $value = $default;
65
                    }
66
67
                    synapse()->memory->local()->put($key, $value);
68
                }
69
            }
70
        }
71
    }
72
}
73