Passed
Push — master ( 04b495...4a17b9 )
by Jean
01:53
created

ExportTrait::toString()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 20
nop 1
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)
0 ignored issues
show
Bug introduced by
-$keep_length of type double is incompatible with the type integer expected by parameter $start of substr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
                    . substr($variable, /** @scrutinizer ignore-type */ -$keep_length)
Loading history...
103
                    ;
104
            }
105
        }
106
        
107
        return $export;
108
    }
109
110
    /**/
111
}
112