prim.php ➔ flagword()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4
Metric Value
cc 4
eloc 13
nc 1
nop 1
dl 0
loc 32
ccs 13
cts 13
cp 1
crap 4
rs 8.5806
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 123.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * @file
5
 * Primitive functions.
6
 *
7
 * Overall implementation notes:
8
 *  - Exceptions are closer to MDL's <ERROR> behavior and are used instead of
9
 *    trigger_error().
10
 */
11
12
namespace Itafroma\Zork;
13
14
use Itafroma\Zork\Exception\ConstantAlreadyDefinedException;
15
use Itafroma\Zork\Exception\FlagwordException;
16
use Itafroma\Zork\Exception\PsetgDuplicateException;
17
use Itafroma\Zork\Exception\SlotNameAlreadyUsedException;
18
use Itafroma\Zork\Struc\StrucInterface;
19
use \BadFunctionCallException;
20
21
/**
22
 * Defines a global constant, throwing an exception if it's already set.
23
 *
24
 * Effectively acts like a declare(), but elevates PHP's built-in E_NOTICE on
25
 * redefining constants to an exception.
26
 *
27
 * @param string $foo The constant name to set.
28
 * @param mixed  $bar The value to set $foo to.
29
 * @throws Itafroma\Zork\Exception\ConstantAlreadyDefinedException
30
 */
31
function msetg($foo, $bar) {
32 3
    if (gassigned($foo) && $bar != gval($foo)) {
33 1
        throw new ConstantAlreadyDefinedException();
34
    }
35
36 3
    return setg($foo, $bar);
37
}
38
39
/**
40
 * Sets a global variable and adds its value to the global "pure_list" array.
41
 *
42
 * @param string $foo The global variable to set.
43
 * @param mixed  $bar The value to set $foo to.
44
 * @return mixed The value of $bar.
45
 * @throws Itafroma\Zork\Exception\PsetgDuplicateException
46
 */
47
function psetg($foo, $bar) {
48 3
    setg($foo, $bar);
49 3
    $pl = gassigned('PURE-LIST') ? gval('PURE-LIST') : setg('PURE-LIST', []);
50
51 3
    if (!in_array($foo, $pl)) {
52 3
        array_unshift($pl, $foo);
53 3
        setg('PURE-LIST', $pl);
54 3
    }
55 2
    elseif (gassigned('PURE-CAREFUL') && gval('PURE-CAREFUL')) {
56 1
        throw new PsetgDuplicateException();
57
    }
58
59 3
    return $bar;
60
}
61
62
/**
63
 * Creates a flag/bit field.
64
 *
65
 * In the original source, there's a hard limit of 35 flags that can be added to
66
 * the flag field, presumably done for memory conservation.
67
 *
68
 * @param string ... $fs A list of flags to add.
69
 * @return int The number of flags added.
70
 * @throws Itafroma\Zork\Exception\FlagwordException
71
 */
72
function flagword(...$fs) {
73 3
    $container = ServiceContainer::getContainer();
74 3
    $oblists = $container->get('oblists');
75 3
    $tot = 1;
76 3
    $cnt = 1;
77
78
    // Given <FLAGWORD>'s usage in the rest of the original source, this could
79
    // be simplified to a simple foreach loop. The use of array_walk_recursive()
80
    // here is to emulate the use of MDL's <MAPF> SUBR in the original source.
81
    array_walk_recursive($fs, function($f) use ($oblists, &$tot, &$cnt) {
82
        // It's unknown what the GROUP-GLUE symbol is in the oblist. It appears
83
        // to be always empty.
84 3
        if (is_scalar($f) && !lookup('GROUP-GLUE', $oblists->get('INITIAL'))) {
85 2
            msetg($f, $tot);
86 2
        }
87
88 3
        $tot *= 2;
89
90
        // This appears to be a bug in the original source: the intention seems
91
        // to be to allow bit fields with up to 36 flags, but because the size
92
        // is incremented and checked after the last value is set, it only
93
        // allows 35 flags.
94 3
        $cnt++;
95 3
        if ($cnt > 36) {
96 1
            throw new FlagwordException();
97
        }
98 3
    });
99
100
    // Also a bug in the original source: since count is incremented after the
101
    // last value is added, the reported number of flags added is off by one.
102 2
    return $cnt;
103
}
104
105
/**
106
 * Generates a new structure.
107
 *
108
 * This function is intentionally left unimplemented. The purpose of it was to
109
 * emulate C's struct language feature. In PHP, struct emulation is done through
110
 * simple class declarations using public properties.
111
 *
112
 * @param string    $nam  The name of the structure.
113
 * @param string    $prim The underlying type of the structure.
114
 * @param mixed ... $elem A series of elements defining the structure,
115
 *                        alternating between the element name and the element
116
 *                        type.
117
 * @throws \BadFunctionCallException
118
 */
119
function newstruc($nam, $prim, ...$elem) {
0 ignored issues
show
Unused Code introduced by
The parameter $nam 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...
Unused Code introduced by
The parameter $prim 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...
Unused Code introduced by
The parameter $elem 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...
120 1
    throw new BadFunctionCallException('newstruc() has been removed: implement Itafroma\Zork\Struc\StrucInterface instead.');
121
}
122
123
setg('SLOTS', []);
124
125
/**
126
 * "Define a funny slot in an object." (sic)
127
 *
128
 * Essentially adds arbitrary named properties to objects. Not sure why they're
129
 * called "funny slots" yet.
130
 *
131
 * Usage:
132
 *  - Create: make_slot('foo', 'default value');
133
 *  - Read: $GLOBALS['zork']['foo']($object);
134
 *  - Update: $GLOBALS['zork']['foo']($object, 'new value');
135
 *
136
 * @param string $name The name of the slot to define.
137
 * @param mixed  $def  The default value of the slot defined.
138
 * @throws Itafroma\Zork|Exception\SlotNameAlreadyUsedException;
139
 */
140
function make_slot($name, $def) {
141
    // Slot names can only be used once globally.
142
    // REDEFINE is apparently a local variable in the original <DEFINE> and is
143
    // never bound.
144 4
    if (!gassigned($name) || !empty($redefine)) {
145 3
        $slots = gval('SLOTS');
146
        $slots[$name] = function (StrucInterface $obj, $val = null) use ($name, $def) {
147 2
            if (isset($val)) {
148 2
                return oput($obj, $name, $val);
149
            }
150
151 1
            return oget($obj, $name) ?: $def;
152
        };
153
154 3
        return setg('SLOTS', $slots);
155
    }
156
157 1
    throw new SlotNameAlreadyUsedException("$name already used.");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
158
}
159