defs.php ➔ oput()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8
Metric Value
cc 8
eloc 10
nc 7
nop 4
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 8
rs 7.7777
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 22 and the first side effect is on line 87.

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
 * Defines.
6
 */
7
8
namespace Itafroma\Zork;
9
10
use Itafroma\Zork\Struc\Adv;
11
use Itafroma\Zork\Struc\Object;
12
use Itafroma\Zork\Struc\Room;
13
use Itafroma\Zork\Struc\StrucInterface;
14
use Itafroma\Zork\Struc\Syntax;
15
use \InvalidArgumentException;
16
use function Itafroma\Zork\flagword;
17
use function Itafroma\Zork\make_slot;
18
use function Itafroma\Zork\setg;
19
20
// Generalized oflags tester
21
22
function trnn(Object $obj, $bit) {
23 1
    return ($bit & $obj->oflags) !== 0;
24
}
25
26
function rtrnn(Room $rm, $bit) {
27 1
    return ($bit & $rm->rbits) !== 0;
28
}
29
30
function gtrnn(Room $rm, $bit) {
31 1
    return ($bit & $rm->rglobal) !== 0;
0 ignored issues
show
Bug introduced by
The property rglobal does not seem to exist in Itafroma\Zork\Struc\Room.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
}
33
34
function rtrz(Room $rm, $bit) {
35 1
    $rm->rbits &= ($bit ^ -1);
36
37 1
    return $rm->rbits;
38
}
39
40
function trc(Object $obj, $bit) {
41 1
    $obj->oflags ^= $bit;
42
43 1
    return $obj->oflags;
44
}
45
46
function trz(Object $obj, $bit) {
47 1
    $obj->oflags &= ($bit ^ -1);
48
49 1
    return $obj->oflags;
50
}
51
52
function tro(Object $obj, $bit) {
53 1
    $obj->oflags |= $bit;
54
55 1
    return $obj->oflags;
56
}
57
58
function rtro(Room $rm, $bit) {
59 1
    $rm->rbits |= $bit;
60
61 1
    return $rm->rbits;
62
}
63
64
function rtrc(Room $rm, $bit) {
65 1
    $rm->rbits ^= $bit;
66
67 1
    return $rm->rbits;
68
}
69
70
function atrnn(Adv $adv, $bit) {
71 1
    return ($bit & $adv->aflags) !== 0;
72
}
73
74
function atrz(Adv $adv, $bit) {
75 1
    $adv->aflags &= ($bit ^ -1);
76
77 1
    return $adv->aflags;
78
}
79
80
function atro(Adv $adv, $bit) {
81 1
    $adv->aflags |= $bit;
82
83 1
    return $adv->aflags;
84
}
85
86
// Slots for room
87
make_slot('RVAL', 0);
88
89
// Value for entering
90
make_slot('RGLOBAL', gval('STAR_BITS'));
91
92
// Globals for room
93
flagword(...[
94
    'RSEENBIT',   // Visited?
95
    'RLIGHTBIT',  // Endogenous light source?
96
    'RLANDBIT',   // On land
97
    'RWATERBIT',  // Water room
98
    'RAIRBIT',    // Mid-air room
99
    'RSACREDBIT', // Thief not allowed
100
    'RFILLBIT',   // Can fill bottle here
101
    'RMUNGBIT',   // Room has been munged
102
    'RBUCKBIT',   // This room is a bucket
103
    'RHOUSEBIT',  // This room is part of the house
104
    'RENDGAME',   // This room is in the end game
105
    'RNWALLBIT',  // This room doesn't have walls
106
]);
107
108
// SFLAGs of a SYNTAX
109
flagword(...[
110
    'SFLIP', // T -- Flip args (for verbs like PICK)
111
    'SDRIVER', // T -- Default syntax for gwimming (sic) and orphanery
112
]);
113
114
/**
115
 * Test a bit in the SFLAGs slot of a SYNTAX
116
 *
117
 * @param Itafroma\Zork\Struc\Syntax $s   The syntax to test
118
 * @param int                       $bit The bit to test
119
 * @return boolean FALSE if bit is set, TRUE otherwise
120
 */
121
function strnn(Syntax $s, $bit) {
122 1
    return ($bit & $s->sflags) !== 0;
123
}
124
125
/**
126
 * Retrieves an object property.
127
 *
128
 * @param Itafroma\Zork\Struc\StrucInterface $o The object to access.
129
 * @param mixed                              $p The property to retrieve.
130
 * @return mixed The property value.
131
 */
132
function oget(StrucInterface $o, $p) {
133 3
    if (!($o instanceof Object || $o instanceof Room)) {
134 1
        throw new InvalidArgumentException('$o must be of type Itafroma\Zork\Struc\Object or Itafroma\Zorks\Struc\Room');
135
    }
136
137 2
    $v = ($o instanceof Object) ? $o->oprops : $o->rprops;
138
139 2
    if (empty($v)) {
140 1
        return null;
141
    }
142
143 1
    return isset($v[$p]) ? $v[$p] : null;
144
}
145
146
/**
147
 * Sets an object property.
148
 *
149
 * @param Itafroma\Zork\Struc\StrucInterface $o The object to modify.
150
 * @param mixed                              $p The property to modify.
151
 * @param mixed                              $x The value to set.
152
 */
153
function oput(StrucInterface $o, $p, $x, $add = true) {
154 4
    if (!($o instanceof Object || $o instanceof Room)) {
155 1
        throw new InvalidArgumentException('$o must be of type Itafroma\Zork\Struc\Object or Itafroma\Zork\Struc\Room');
156
    }
157
158 3
    $v = ($o instanceof Object) ? $o->oprops : $o->rprops;
159
160 3
    if ((empty($v) && $add) || isset($v[$p])) {
161 2
        if ($o instanceof Object) {
162 1
            $o->oprops[$p] = $x;
163 1
        }
164
        else {
165 1
            $o->rprops[$p] = $x;
166
        }
167 2
    }
168
169 3
    return $o;
170
}
171
172
setg('ROOMS', []);
173
174
setg('OBJECTS', []);
175
176
setg('ACTORS', []);
177