Skip to content

Commit 26a1915

Browse files
committed
Splitting DeferredCallChain in traits for single responsability
1 parent 04b4953 commit 26a1915

File tree

3 files changed

+180
-144
lines changed

3 files changed

+180
-144
lines changed

src/ArrayAccessTrait.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* ArrayAccessTrait
4+
*
5+
* @package php-deferred-callchain
6+
* @author Jean Claveau
7+
*/
8+
namespace JClaveau\Async;
9+
use BadMethodCallException;
10+
11+
/**
12+
* Trait gathering unused array access required methods
13+
*/
14+
trait ArrayAccessTrait
15+
{
16+
/**
17+
* Unused part of the ArrayAccess interface
18+
*
19+
* @param $offset
20+
* @param $value
21+
* @throws \BadMethodCallException
22+
*/
23+
public function offsetSet(/** @scrutinizer ignore-unused */ $offset, $value)
24+
{
25+
throw new BadMethodCallException(
26+
"not implemented"
27+
);
28+
}
29+
30+
/**
31+
* Unused part of the ArrayAccess interface
32+
*
33+
* @param $offset
34+
* @throws \BadMethodCallException
35+
*/
36+
public function offsetExists(/** @scrutinizer ignore-unused */ $offset)
37+
{
38+
throw new BadMethodCallException(
39+
"not implemented"
40+
);
41+
}
42+
43+
/**
44+
* Unused part of the ArrayAccess interface
45+
*
46+
* @param $offset
47+
* @throws \BadMethodCallException
48+
*/
49+
public function offsetUnset(/** @scrutinizer ignore-unused */ $offset)
50+
{
51+
throw new BadMethodCallException(
52+
"not implemented"
53+
);
54+
}
55+
56+
/**/
57+
}

src/DeferredCallChain.php

Lines changed: 12 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class DeferredCallChain implements \JsonSerializable, \ArrayAccess
2222
{
2323
use \JClaveau\Traits\Fluent\New_;
2424
use FunctionCallTrait;
25+
use ArrayAccessTrait;
26+
use ExportTrait;
2527

2628
/** @var array $stack The stack of deferred calls */
2729
protected $stack = [];
@@ -41,24 +43,6 @@ public function __construct($class_type_interface_or_instance=null)
4143
}
4244
}
4345

44-
/**
45-
* ArrayAccess interface
46-
*
47-
* @param string $key The entry to acces
48-
*/
49-
public function &offsetGet($key)
50-
{
51-
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
52-
53-
$this->stack[] = [
54-
'entry' => $key,
55-
'file' => isset($caller['file']) ? $caller['file'] : null,
56-
'line' => isset($caller['line']) ? $caller['line'] : null,
57-
];
58-
59-
return $this;
60-
}
61-
6246
/**
6347
* Stores any call in the the stack.
6448
*
@@ -82,97 +66,21 @@ public final function __call($method, array $arguments)
8266
}
8367

8468
/**
85-
* For implementing JsonSerializable interface.
86-
*
87-
* @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php
88-
*/
89-
public function jsonSerialize()
90-
{
91-
return $this->stack;
92-
}
93-
94-
/**
95-
* Outputs the PHP code producing the current call chain while it's casted
96-
* as a string.
97-
*
98-
* @return string The PHP code corresponding to this call chain
99-
*/
100-
public function __toString()
101-
{
102-
return $this->toString();
103-
}
104-
105-
/**
106-
* Outputs the PHP code producing the current call chain while it's casted
107-
* as a string.
69+
* ArrayAccess interface
10870
*
109-
* @return string The PHP code corresponding to this call chain
71+
* @param string $key The entry to acces
11072
*/
111-
protected function toString(array $options=[])
73+
public function &offsetGet($key)
11274
{
113-
$target = isset($options['target']) ? $options['target'] : $this->expectedTarget;
75+
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
11476

115-
$string = '(new ' . get_called_class();
116-
$target && $string .= '(' . static::varExport($target, ['short_objects']) . ')';
117-
$string .= ')';
118-
119-
foreach ($this->stack as $i => $call) {
120-
if (isset($call['method'])) {
121-
$string .= '->';
122-
$string .= $call['method'].'(';
123-
$string .= implode(', ', array_map(function($argument) {
124-
return static::varExport($argument, ['short_objects']);
125-
}, $call['arguments']));
126-
$string .= ')';
127-
}
128-
else {
129-
$string .= '[' . static::varExport($call['entry'], ['short_objects']) . ']';
130-
}
131-
132-
if (! empty($options['limit']) && $options['limit'] == $i) {
133-
break;
134-
}
135-
}
77+
$this->stack[] = [
78+
'entry' => $key,
79+
'file' => isset($caller['file']) ? $caller['file'] : null,
80+
'line' => isset($caller['line']) ? $caller['line'] : null,
81+
];
13682

137-
return $string;
138-
}
139-
140-
/**
141-
* Enhanced var_export() required for dumps.
142-
*
143-
* @param mixed $variable
144-
* @param array $options max_length | alias_instances
145-
* @return string The PHP code of the variable
146-
*/
147-
protected static function varExport($variable, array $options=[])
148-
{
149-
$options['max_length'] = isset($options['max_length']) ? $options['max_length'] : 512;
150-
$options['short_objects'] = ! empty($options['short_objects']) || in_array('short_objects', $options);
151-
152-
$export = var_export($variable, true);
153-
154-
if ($options['short_objects']) {
155-
if (is_object($variable)) {
156-
$export = ' ' . get_class($variable) . ' #' . spl_object_id($variable) . ' ';
157-
}
158-
}
159-
160-
if (strlen($export) > $options['max_length']) {
161-
162-
if (is_object($variable)) {
163-
$export = get_class($variable) . ' #' . spl_object_id($variable);
164-
}
165-
elseif (is_string($variable)) {
166-
$keep_length = floor(($options['max_length'] - 5) / 2);
167-
168-
$export = substr($variable, 0, (int) $keep_length)
169-
. ' ... '
170-
. substr($variable, -$keep_length)
171-
;
172-
}
173-
}
174-
175-
return $export;
83+
return $this;
17684
}
17785

17886
/**
@@ -350,45 +258,5 @@ public function __invoke($target=null)
350258
return $out;
351259
}
352260

353-
/**
354-
* Unused part of the ArrayAccess interface
355-
*
356-
* @param $offset
357-
* @param $value
358-
* @throws \BadMethodCallException
359-
*/
360-
public function offsetSet($offset, $value)
361-
{
362-
throw new BadMethodCallException(
363-
"not implemented"
364-
);
365-
}
366-
367-
/**
368-
* Unused part of the ArrayAccess interface
369-
*
370-
* @param $offset
371-
* @throws \BadMethodCallException
372-
*/
373-
public function offsetExists($offset)
374-
{
375-
throw new BadMethodCallException(
376-
"not implemented"
377-
);
378-
}
379-
380-
/**
381-
* Unused part of the ArrayAccess interface
382-
*
383-
* @param $offset
384-
* @throws \BadMethodCallException
385-
*/
386-
public function offsetUnset($offset)
387-
{
388-
throw new BadMethodCallException(
389-
"not implemented"
390-
);
391-
}
392-
393261
/**/
394262
}

src/ExportTrait.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* ExportTrait
4+
*
5+
* @package php-deferred-callchain
6+
* @author Jean Claveau
7+
*/
8+
namespace JClaveau\Async;
9+
10+
/**
11+
* Trait gathering support of export functions like toString() or
12+
* jsonSerialize()
13+
*/
14+
trait ExportTrait
15+
{
16+
/**
17+
* For implementing JsonSerializable interface.
18+
*
19+
* @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php
20+
*/
21+
public function jsonSerialize()
22+
{
23+
return $this->stack;
24+
}
25+
26+
/**
27+
* Outputs the PHP code producing the current call chain while it's casted
28+
* as a string.
29+
*
30+
* @return string The PHP code corresponding to this call chain
31+
*/
32+
public function __toString()
33+
{
34+
return $this->toString();
35+
}
36+
37+
/**
38+
* Outputs the PHP code producing the current call chain while it's casted
39+
* as a string.
40+
*
41+
* @return string The PHP code corresponding to this call chain
42+
*/
43+
protected function toString(array $options=[])
44+
{
45+
$target = isset($options['target']) ? $options['target'] : $this->expectedTarget;
46+
47+
$string = '(new ' . get_called_class();
48+
$target && $string .= '(' . static::varExport($target, ['short_objects']) . ')';
49+
$string .= ')';
50+
51+
foreach ($this->stack as $i => $call) {
52+
if (isset($call['method'])) {
53+
$string .= '->';
54+
$string .= $call['method'].'(';
55+
$string .= implode(', ', array_map(function($argument) {
56+
return static::varExport($argument, ['short_objects']);
57+
}, $call['arguments']));
58+
$string .= ')';
59+
}
60+
else {
61+
$string .= '[' . static::varExport($call['entry'], ['short_objects']) . ']';
62+
}
63+
64+
if (! empty($options['limit']) && $options['limit'] == $i) {
65+
break;
66+
}
67+
}
68+
69+
return $string;
70+
}
71+
72+
/**
73+
* Enhanced var_export() required for dumps.
74+
*
75+
* @param mixed $variable
76+
* @param array $options max_length | alias_instances
77+
* @return string The PHP code of the variable
78+
*/
79+
protected static function varExport($variable, array $options=[])
80+
{
81+
$options['max_length'] = isset($options['max_length']) ? $options['max_length'] : 512;
82+
$options['short_objects'] = ! empty($options['short_objects']) || in_array('short_objects', $options);
83+
84+
$export = var_export($variable, true);
85+
86+
if ($options['short_objects']) {
87+
if (is_object($variable)) {
88+
$export = ' ' . get_class($variable) . ' #' . spl_object_id($variable) . ' ';
89+
}
90+
}
91+
92+
if (strlen($export) > $options['max_length']) {
93+
94+
if (is_object($variable)) {
95+
$export = get_class($variable) . ' #' . spl_object_id($variable);
96+
}
97+
elseif (is_string($variable)) {
98+
$keep_length = floor(($options['max_length'] - 5) / 2);
99+
100+
$export = substr($variable, 0, (int) $keep_length)
101+
. ' ... '
102+
. substr($variable, -$keep_length)
103+
;
104+
}
105+
}
106+
107+
return $export;
108+
}
109+
110+
/**/
111+
}

0 commit comments

Comments
 (0)