/
Contract.php
519 lines (467 loc) · 17.7 KB
/
Contract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
/**
* Copyright (c) 2011-present Stuart Herbert.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Phix_Project
* @subpackage ContractLib2
* @author Stuart Herbert <stuart@stuartherbert.com>
* @copyright 2011-present Stuart Herbert
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://phix-project.org
* @version @@PACKAGE_VERSION@@
*/
namespace Phix_Project\ContractLib2;
class Contract
{
/**
* Are we currently enforcing any contracts passed to
* self::Enforce()?
*
* By default, we do not!
*
* @var boolean
*/
static protected $enforcing = false;
/**
* A set of OldValues objects
*
* You can get the right object for your code's current scope by
* calling Contract::OldValues()
*
* Objects in this array that are no longer required are nuked by
* the Contract::Postconditions() wrapper
*
* @var array(OldValues)
*/
static protected $oldValues = array();
/**
* Global library; cannot instantiate
*
* You can't instantiate this library because it needs to preserve
* state even as the execution scope in your code changes :(
*
* @codeCoverageIgnore
*/
protected function __construct()
{
// do nothing
}
/**
* Precondition: is the expression $expr true?
*
* Use this method at the start of your method to make sure you're
* happy with the data that you have been passed, and with the
* current state of your object
*
* Throws an E5xx_ContractPreconditionException if the parameter
* passed in is false
*
* @throw E5xx_ContractPreconditionException
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function Requires($expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('Requires', $reason);
}
return true;
}
/**
* Precondition: is the expression $expr true?
*
* Use this method at the start of your method to make sure you're
* happy with the data that you have been passed, and with the
* current state of your object
*
* Throws an E5xx_ContractPreconditionException if $expr is false,
* and adds $value to the exception's error message so that you
* can see which value failed the test
*
* @param mixed $value
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function RequiresValue($value, $expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('RequiresValue', $reason, true, $value);
}
return true;
}
/**
* Postcondition: is the expression $expr true?
*
* Use this method at the end of your method to make sure you're
* happy with the results before your method returns to the caller
*
* Throws an E5xx_ContractPostconditionException if $expr is false.
*
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function Ensures($expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('Ensures', $reason);
}
return true;
}
/**
* Postcondition: is the expression $expr true?
*
* Use this method at the end of your method to make sure you're
* happy with the results before your method returns to the caller
*
* Throws an E5xx_ContractPostConditionException if $expr is false,
* and adds $value to the exception's error message so that you
* can see which value failed the test
*
* @param mixed $value
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function EnsuresValue($value, $expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('EnsuresValue', $reason, true, $value);
}
return true;
}
/**
* Condition: is the expr $expr true?
*
* Use this method in the middle of your method, to check the
* workings of your method before continuing.
*
* Throws an E5xx_ContractConditionException if $expr is false.
*
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function Asserts($expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('Asserts', $reason);
}
return true;
}
/**
* Condition: is the expr $expr true?
*
* Use this method in the middle of your method, to check the
* workings of your method before continuing.
*
* Throws an E5xx_ContractConditionException if $expr is false,
* and adds $value to the exception's error message so that you
* can see which value failed the test
*
* @param mixed $value
* @param boolean $expr
* @param string $reason error message to show on failure
* @return boolean true on success
*/
static public function AssertsValue($value, $expr, $reason = null)
{
if (!$expr)
{
throw new E5xx_ContractFailedException('AssertsValue', $reason, true, $value);
}
return true;
}
/**
* Apply the same condition (or set of conditions) to the values
* in an array
*
* @param array $values
* @param callback $callback
* @param boolean true on success
*/
static public function ForAll($values, $callback)
{
array_walk($values, $callback);
return true;
}
// ================================================================
//
// Old values support
//
// ----------------------------------------------------------------
/**
* Obtain a value, suitable for use as an array key, based on the
* current execution scope in an app
*
* @return array(string, string)
* The caller, plus the scope
*/
static protected function determineScope()
{
// the scope comes from interpreting the current
// execution stack
//
// we are looking for the function or method that has
// called either Contract::Preconditions or
// Contract::Postconditions
//
// this algorithm is annoyingly expensive, but it should
// ensure that there are no problems with actions in one
// scope ever affecting the old values remembered in any
// other scope
$debug_backtrace = debug_backtrace();
$caller = null;
$maxIndex = count($debug_backtrace);
$i = 0;
while ($caller == null && $i < $maxIndex)
{
// var_dump(substr($debug_backtrace[$i]['function'], -10, 10));
if (isset($debug_backtrace[$i]['class'])
&& $debug_backtrace[$i]['class'] == 'Phix_Project\ContractLib2\Contract'
&& substr($debug_backtrace[$i]['function'], -10, 10) == 'conditions'
)
{
$caller = $debug_backtrace[$i]['function'];
}
$i++;
}
// did we find what we are looking for?
if ($caller == null)
{
// no - throw an exception
throw new \RuntimeException('You can only use ContractLib2 Old Values support inside Preconditions or Postconditions');
}
// at this point, $debug_backtrace[0] points at the caller
// to the precondition or postcondition
//
// we want to remember the file and function, but not the
// specific line number
//
// this makes sure that we return the same result when
// called in both the preconditions and postconditions
// in the same function, with the same callstack
if (isset($debug_backtrace[$i]['line']))
{
// this never seems to get called, but I assume
// that one day the backtrace might get 'fixed'
// @codeCoverageIgnoreStart
unset($debug_backtrace[$i]['line']);
// @codeCoverageIgnoreEnd
}
// now, let's build up this scope
$scope = '';
for (;$i < $maxIndex; $i++)
{
foreach (array('file', 'line', 'function') as $key)
{
if (isset($debug_backtrace[$i][$key]))
{
$scope .= $debug_backtrace[$i][$key];
}
}
}
// var_dump($scope);
return array($caller, $scope);
}
/**
* Obtain the old value of an argument
*
* @param string $argName
* @return mixed
*/
static public function OldValue($argName)
{
// work out the current scope
list($caller, $scope) = self::determineScope();
// do we have an existing OldValues object for this scope?
if (!isset(self::$oldValues[$scope]))
{
return null;
}
// do we have a stashed value for this argument name?
if (self::$oldValues[$scope]->hasStashed($argName))
{
// yes we do
return self::$oldValues[$scope]->unpack($argName);
}
// no we don't
return null;
}
/**
* Remember an old value for future comparisons
*
* @param string $argName
* The name of the value to remember
* @param mixed $argValue
* The value to remember
*/
static public function RememberOldValue($argName, $argValue)
{
// work out the current scope
list($caller, $scope) = self::determineScope();
// we must have been called from Preconditions
if ($caller !== 'Preconditions')
{
throw new \RuntimeException('You can only remember old values inside Contract::Preconditions');
}
// do we have an existing OldValues object for this scope?
if (!isset(self::$oldValues[$scope]))
{
// no - create one!
self::$oldValues[$scope] = new OldValues();
}
// stash the value
self::$oldValues[$scope]->stash($argName, $argValue);
}
/**
* Free up memory by forgetting the old values we may have
* remembered in the current scope
*/
static public function ForgetOldValues()
{
// work out the current scope
list($caller, $scope) = self::determineScope();
// release an object, if we have one
if (isset(self::$oldValues[$scope]))
{
unset(self::$oldValues[$scope]);
}
}
/**
* Get the internal list of scopes that are remembering old
* values
*
* This method exists only to help with debugging
*
* @return array
*/
static public function _rememberedScopes()
{
return self::$oldValues;
}
// ================================================================
//
// Unreachable code support
//
// ----------------------------------------------------------------
static public function Unreachable($file, $line)
{
throw new E5xx_ContractFailedException('Unreachable', "Unreachable code in file $file at line $line has somehow been reached. Go figure!");
}
// ================================================================
//
// Wrapped contract support
//
// ----------------------------------------------------------------
/**
* Tell us to enforce contracts passed to self::Enforce()
*/
static public function EnforceWrappedContracts()
{
self::$enforcing = true;
}
/**
* Tell us to enforce only calls made directly to the individual
* contract conditions: Requires, Assert, Ensures et al
*/
static public function EnforceOnlyDirectContracts()
{
self::$enforcing = false;
}
/**
* Check a set of preconditions *if* we are enforcing wrapped
* contracts.
*
* This exists as a performance boost, allowing us to leave
* contracts in the code even in production environments
*
* @param callback $callback
* @param array $params
* @return boolean true on success
*/
static public function Preconditions($callback, $params = array())
{
if (self::$enforcing)
{
call_user_func_array($callback, $params);
}
return true;
}
/**
* Check a set of postconditions *if* we are enforcing wrapped
* contracts.
*
* This exists as a performance boost, allowing us to leave
* contracts in the code even in production environments
*
* @param callback $callback
* @param array $params
* @return boolean true on success
*/
static public function Postconditions($callback, $params = array())
{
if (self::$enforcing)
{
call_user_func_array($callback, $params);
}
// success!
return true;
}
/**
* Check a set of conditions mid-method *if* we are enforcing
* wrapped contracts.
*
* This exists as a performance boost, allowing us to leave
* contracts in the code even in production environments
*
* @param callback $callback
* @param array $params
* @return boolean true on success
*/
static public function Conditionals($callback, $params = array())
{
if (self::$enforcing)
{
call_user_func_array($callback, $params);
}
return true;
}
}