@@ -30,109 +30,109 @@ |
||
30 | 30 | */ |
31 | 31 | class Prophet |
32 | 32 | { |
33 | - private $doubler; |
|
34 | - private $revealer; |
|
35 | - private $util; |
|
33 | + private $doubler; |
|
34 | + private $revealer; |
|
35 | + private $util; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var ObjectProphecy[] |
|
39 | - */ |
|
40 | - private $prophecies = array(); |
|
37 | + /** |
|
38 | + * @var ObjectProphecy[] |
|
39 | + */ |
|
40 | + private $prophecies = array(); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Initializes Prophet. |
|
44 | - * |
|
45 | - * @param null|Doubler $doubler |
|
46 | - * @param null|RevealerInterface $revealer |
|
47 | - * @param null|StringUtil $util |
|
48 | - */ |
|
49 | - public function __construct( |
|
50 | - Doubler $doubler = null, |
|
51 | - RevealerInterface $revealer = null, |
|
52 | - StringUtil $util = null |
|
53 | - ) { |
|
54 | - if (null === $doubler) { |
|
55 | - $doubler = new CachedDoubler(); |
|
56 | - $doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch); |
|
57 | - $doubler->registerClassPatch(new ClassPatch\TraversablePatch); |
|
58 | - $doubler->registerClassPatch(new ClassPatch\ThrowablePatch); |
|
59 | - $doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch); |
|
60 | - $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch); |
|
61 | - $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch); |
|
62 | - $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch()); |
|
63 | - $doubler->registerClassPatch(new ClassPatch\MagicCallPatch); |
|
64 | - $doubler->registerClassPatch(new ClassPatch\KeywordPatch); |
|
65 | - } |
|
42 | + /** |
|
43 | + * Initializes Prophet. |
|
44 | + * |
|
45 | + * @param null|Doubler $doubler |
|
46 | + * @param null|RevealerInterface $revealer |
|
47 | + * @param null|StringUtil $util |
|
48 | + */ |
|
49 | + public function __construct( |
|
50 | + Doubler $doubler = null, |
|
51 | + RevealerInterface $revealer = null, |
|
52 | + StringUtil $util = null |
|
53 | + ) { |
|
54 | + if (null === $doubler) { |
|
55 | + $doubler = new CachedDoubler(); |
|
56 | + $doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch); |
|
57 | + $doubler->registerClassPatch(new ClassPatch\TraversablePatch); |
|
58 | + $doubler->registerClassPatch(new ClassPatch\ThrowablePatch); |
|
59 | + $doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch); |
|
60 | + $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch); |
|
61 | + $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch); |
|
62 | + $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch()); |
|
63 | + $doubler->registerClassPatch(new ClassPatch\MagicCallPatch); |
|
64 | + $doubler->registerClassPatch(new ClassPatch\KeywordPatch); |
|
65 | + } |
|
66 | 66 | |
67 | - $this->doubler = $doubler; |
|
68 | - $this->revealer = $revealer ?: new Revealer; |
|
69 | - $this->util = $util ?: new StringUtil; |
|
70 | - } |
|
67 | + $this->doubler = $doubler; |
|
68 | + $this->revealer = $revealer ?: new Revealer; |
|
69 | + $this->util = $util ?: new StringUtil; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Creates new object prophecy. |
|
74 | - * |
|
75 | - * @param null|string $classOrInterface Class or interface name |
|
76 | - * |
|
77 | - * @return ObjectProphecy |
|
78 | - */ |
|
79 | - public function prophesize($classOrInterface = null) |
|
80 | - { |
|
81 | - $this->prophecies[] = $prophecy = new ObjectProphecy( |
|
82 | - new LazyDouble($this->doubler), |
|
83 | - new CallCenter($this->util), |
|
84 | - $this->revealer |
|
85 | - ); |
|
72 | + /** |
|
73 | + * Creates new object prophecy. |
|
74 | + * |
|
75 | + * @param null|string $classOrInterface Class or interface name |
|
76 | + * |
|
77 | + * @return ObjectProphecy |
|
78 | + */ |
|
79 | + public function prophesize($classOrInterface = null) |
|
80 | + { |
|
81 | + $this->prophecies[] = $prophecy = new ObjectProphecy( |
|
82 | + new LazyDouble($this->doubler), |
|
83 | + new CallCenter($this->util), |
|
84 | + $this->revealer |
|
85 | + ); |
|
86 | 86 | |
87 | - if ($classOrInterface && class_exists($classOrInterface)) { |
|
88 | - return $prophecy->willExtend($classOrInterface); |
|
89 | - } |
|
87 | + if ($classOrInterface && class_exists($classOrInterface)) { |
|
88 | + return $prophecy->willExtend($classOrInterface); |
|
89 | + } |
|
90 | 90 | |
91 | - if ($classOrInterface && interface_exists($classOrInterface)) { |
|
92 | - return $prophecy->willImplement($classOrInterface); |
|
93 | - } |
|
91 | + if ($classOrInterface && interface_exists($classOrInterface)) { |
|
92 | + return $prophecy->willImplement($classOrInterface); |
|
93 | + } |
|
94 | 94 | |
95 | - return $prophecy; |
|
96 | - } |
|
95 | + return $prophecy; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Returns all created object prophecies. |
|
100 | - * |
|
101 | - * @return ObjectProphecy[] |
|
102 | - */ |
|
103 | - public function getProphecies() |
|
104 | - { |
|
105 | - return $this->prophecies; |
|
106 | - } |
|
98 | + /** |
|
99 | + * Returns all created object prophecies. |
|
100 | + * |
|
101 | + * @return ObjectProphecy[] |
|
102 | + */ |
|
103 | + public function getProphecies() |
|
104 | + { |
|
105 | + return $this->prophecies; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Returns Doubler instance assigned to this Prophet. |
|
110 | - * |
|
111 | - * @return Doubler |
|
112 | - */ |
|
113 | - public function getDoubler() |
|
114 | - { |
|
115 | - return $this->doubler; |
|
116 | - } |
|
108 | + /** |
|
109 | + * Returns Doubler instance assigned to this Prophet. |
|
110 | + * |
|
111 | + * @return Doubler |
|
112 | + */ |
|
113 | + public function getDoubler() |
|
114 | + { |
|
115 | + return $this->doubler; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Checks all predictions defined by prophecies of this Prophet. |
|
120 | - * |
|
121 | - * @throws Exception\Prediction\AggregateException If any prediction fails |
|
122 | - */ |
|
123 | - public function checkPredictions() |
|
124 | - { |
|
125 | - $exception = new AggregateException("Some predictions failed:\n"); |
|
126 | - foreach ($this->prophecies as $prophecy) { |
|
127 | - try { |
|
128 | - $prophecy->checkProphecyMethodsPredictions(); |
|
129 | - } catch (PredictionException $e) { |
|
130 | - $exception->append($e); |
|
131 | - } |
|
132 | - } |
|
118 | + /** |
|
119 | + * Checks all predictions defined by prophecies of this Prophet. |
|
120 | + * |
|
121 | + * @throws Exception\Prediction\AggregateException If any prediction fails |
|
122 | + */ |
|
123 | + public function checkPredictions() |
|
124 | + { |
|
125 | + $exception = new AggregateException("Some predictions failed:\n"); |
|
126 | + foreach ($this->prophecies as $prophecy) { |
|
127 | + try { |
|
128 | + $prophecy->checkProphecyMethodsPredictions(); |
|
129 | + } catch (PredictionException $e) { |
|
130 | + $exception->append($e); |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - if (count($exception->getExceptions())) { |
|
135 | - throw $exception; |
|
136 | - } |
|
137 | - } |
|
134 | + if (count($exception->getExceptions())) { |
|
135 | + throw $exception; |
|
136 | + } |
|
137 | + } |
|
138 | 138 | } |
@@ -20,80 +20,80 @@ |
||
20 | 20 | */ |
21 | 21 | class StringUtil |
22 | 22 | { |
23 | - private $verbose; |
|
23 | + private $verbose; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param bool $verbose |
|
27 | - */ |
|
28 | - public function __construct($verbose = true) |
|
29 | - { |
|
30 | - $this->verbose = $verbose; |
|
31 | - } |
|
25 | + /** |
|
26 | + * @param bool $verbose |
|
27 | + */ |
|
28 | + public function __construct($verbose = true) |
|
29 | + { |
|
30 | + $this->verbose = $verbose; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Stringifies any provided value. |
|
35 | - * |
|
36 | - * @param mixed $value |
|
37 | - * @param boolean $exportObject |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function stringify($value, $exportObject = true) |
|
42 | - { |
|
43 | - if (is_array($value)) { |
|
44 | - if (range(0, count($value) - 1) === array_keys($value)) { |
|
45 | - return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']'; |
|
46 | - } |
|
33 | + /** |
|
34 | + * Stringifies any provided value. |
|
35 | + * |
|
36 | + * @param mixed $value |
|
37 | + * @param boolean $exportObject |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function stringify($value, $exportObject = true) |
|
42 | + { |
|
43 | + if (is_array($value)) { |
|
44 | + if (range(0, count($value) - 1) === array_keys($value)) { |
|
45 | + return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']'; |
|
46 | + } |
|
47 | 47 | |
48 | - $stringify = array($this, __FUNCTION__); |
|
48 | + $stringify = array($this, __FUNCTION__); |
|
49 | 49 | |
50 | - return '['.implode(', ', array_map(function ($item, $key) use ($stringify) { |
|
51 | - return (is_integer($key) ? $key : '"'.$key.'"'). |
|
52 | - ' => '.call_user_func($stringify, $item); |
|
53 | - }, $value, array_keys($value))).']'; |
|
54 | - } |
|
55 | - if (is_resource($value)) { |
|
56 | - return get_resource_type($value).':'.$value; |
|
57 | - } |
|
58 | - if (is_object($value)) { |
|
59 | - return $exportObject ? ExportUtil::export($value) : sprintf('%s:%s', get_class($value), spl_object_hash($value)); |
|
60 | - } |
|
61 | - if (true === $value || false === $value) { |
|
62 | - return $value ? 'true' : 'false'; |
|
63 | - } |
|
64 | - if (is_string($value)) { |
|
65 | - $str = sprintf('"%s"', str_replace("\n", '\\n', $value)); |
|
50 | + return '['.implode(', ', array_map(function ($item, $key) use ($stringify) { |
|
51 | + return (is_integer($key) ? $key : '"'.$key.'"'). |
|
52 | + ' => '.call_user_func($stringify, $item); |
|
53 | + }, $value, array_keys($value))).']'; |
|
54 | + } |
|
55 | + if (is_resource($value)) { |
|
56 | + return get_resource_type($value).':'.$value; |
|
57 | + } |
|
58 | + if (is_object($value)) { |
|
59 | + return $exportObject ? ExportUtil::export($value) : sprintf('%s:%s', get_class($value), spl_object_hash($value)); |
|
60 | + } |
|
61 | + if (true === $value || false === $value) { |
|
62 | + return $value ? 'true' : 'false'; |
|
63 | + } |
|
64 | + if (is_string($value)) { |
|
65 | + $str = sprintf('"%s"', str_replace("\n", '\\n', $value)); |
|
66 | 66 | |
67 | - if (!$this->verbose && 50 <= strlen($str)) { |
|
68 | - return substr($str, 0, 50).'"...'; |
|
69 | - } |
|
67 | + if (!$this->verbose && 50 <= strlen($str)) { |
|
68 | + return substr($str, 0, 50).'"...'; |
|
69 | + } |
|
70 | 70 | |
71 | - return $str; |
|
72 | - } |
|
73 | - if (null === $value) { |
|
74 | - return 'null'; |
|
75 | - } |
|
71 | + return $str; |
|
72 | + } |
|
73 | + if (null === $value) { |
|
74 | + return 'null'; |
|
75 | + } |
|
76 | 76 | |
77 | - return (string) $value; |
|
78 | - } |
|
77 | + return (string) $value; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Stringifies provided array of calls. |
|
82 | - * |
|
83 | - * @param Call[] $calls Array of Call instances |
|
84 | - * |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function stringifyCalls(array $calls) |
|
88 | - { |
|
89 | - $self = $this; |
|
80 | + /** |
|
81 | + * Stringifies provided array of calls. |
|
82 | + * |
|
83 | + * @param Call[] $calls Array of Call instances |
|
84 | + * |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function stringifyCalls(array $calls) |
|
88 | + { |
|
89 | + $self = $this; |
|
90 | 90 | |
91 | - return implode(PHP_EOL, array_map(function (Call $call) use ($self) { |
|
92 | - return sprintf(' - %s(%s) @ %s', |
|
93 | - $call->getMethodName(), |
|
94 | - implode(', ', array_map(array($self, 'stringify'), $call->getArguments())), |
|
95 | - str_replace(GETCWD().DIRECTORY_SEPARATOR, '', $call->getCallPlace()) |
|
96 | - ); |
|
97 | - }, $calls)); |
|
98 | - } |
|
91 | + return implode(PHP_EOL, array_map(function (Call $call) use ($self) { |
|
92 | + return sprintf(' - %s(%s) @ %s', |
|
93 | + $call->getMethodName(), |
|
94 | + implode(', ', array_map(array($self, 'stringify'), $call->getArguments())), |
|
95 | + str_replace(GETCWD().DIRECTORY_SEPARATOR, '', $call->getCallPlace()) |
|
96 | + ); |
|
97 | + }, $calls)); |
|
98 | + } |
|
99 | 99 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | |
48 | 48 | $stringify = array($this, __FUNCTION__); |
49 | 49 | |
50 | - return '['.implode(', ', array_map(function ($item, $key) use ($stringify) { |
|
50 | + return '['.implode(', ', array_map(function($item, $key) use ($stringify) { |
|
51 | 51 | return (is_integer($key) ? $key : '"'.$key.'"'). |
52 | 52 | ' => '.call_user_func($stringify, $item); |
53 | 53 | }, $value, array_keys($value))).']'; |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | if (is_string($value)) { |
65 | 65 | $str = sprintf('"%s"', str_replace("\n", '\\n', $value)); |
66 | 66 | |
67 | - if (!$this->verbose && 50 <= strlen($str)) { |
|
67 | + if ( ! $this->verbose && 50 <= strlen($str)) { |
|
68 | 68 | return substr($str, 0, 50).'"...'; |
69 | 69 | } |
70 | 70 | |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | { |
89 | 89 | $self = $this; |
90 | 90 | |
91 | - return implode(PHP_EOL, array_map(function (Call $call) use ($self) { |
|
91 | + return implode(PHP_EOL, array_map(function(Call $call) use ($self) { |
|
92 | 92 | return sprintf(' - %s(%s) @ %s', |
93 | 93 | $call->getMethodName(), |
94 | 94 | implode(', ', array_map(array($self, 'stringify'), $call->getArguments())), |
@@ -20,191 +20,191 @@ |
||
20 | 20 | */ |
21 | 21 | class ExportUtil |
22 | 22 | { |
23 | - /** |
|
24 | - * Exports a value as a string |
|
25 | - * |
|
26 | - * The output of this method is similar to the output of print_r(), but |
|
27 | - * improved in various aspects: |
|
28 | - * |
|
29 | - * - NULL is rendered as "null" (instead of "") |
|
30 | - * - TRUE is rendered as "true" (instead of "1") |
|
31 | - * - FALSE is rendered as "false" (instead of "") |
|
32 | - * - Strings are always quoted with single quotes |
|
33 | - * - Carriage returns and newlines are normalized to \n |
|
34 | - * - Recursion and repeated rendering is treated properly |
|
35 | - * |
|
36 | - * @param mixed $value |
|
37 | - * @param int $indentation The indentation level of the 2nd+ line |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - public static function export($value, $indentation = 0) |
|
41 | - { |
|
42 | - return self::recursiveExport($value, $indentation); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Converts an object to an array containing all of its private, protected |
|
47 | - * and public properties. |
|
48 | - * |
|
49 | - * @param mixed $value |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public static function toArray($value) |
|
53 | - { |
|
54 | - if (!is_object($value)) { |
|
55 | - return (array) $value; |
|
56 | - } |
|
57 | - |
|
58 | - $array = array(); |
|
59 | - |
|
60 | - foreach ((array) $value as $key => $val) { |
|
61 | - // properties are transformed to keys in the following way: |
|
62 | - // private $property => "\0Classname\0property" |
|
63 | - // protected $property => "\0*\0property" |
|
64 | - // public $property => "property" |
|
65 | - if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) { |
|
66 | - $key = $matches[1]; |
|
67 | - } |
|
68 | - |
|
69 | - // See https://github.com/php/php-src/commit/5721132 |
|
70 | - if ($key === "\0gcdata") { |
|
71 | - continue; |
|
72 | - } |
|
73 | - |
|
74 | - $array[$key] = $val; |
|
75 | - } |
|
76 | - |
|
77 | - // Some internal classes like SplObjectStorage don't work with the |
|
78 | - // above (fast) mechanism nor with reflection in Zend. |
|
79 | - // Format the output similarly to print_r() in this case |
|
80 | - if ($value instanceof \SplObjectStorage) { |
|
81 | - // However, the fast method does work in HHVM, and exposes the |
|
82 | - // internal implementation. Hide it again. |
|
83 | - if (property_exists('\SplObjectStorage', '__storage')) { |
|
84 | - unset($array['__storage']); |
|
85 | - } elseif (property_exists('\SplObjectStorage', 'storage')) { |
|
86 | - unset($array['storage']); |
|
87 | - } |
|
88 | - |
|
89 | - if (property_exists('\SplObjectStorage', '__key')) { |
|
90 | - unset($array['__key']); |
|
91 | - } |
|
92 | - |
|
93 | - foreach ($value as $key => $val) { |
|
94 | - $array[spl_object_hash($val)] = array( |
|
95 | - 'obj' => $val, |
|
96 | - 'inf' => $value->getInfo(), |
|
97 | - ); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - return $array; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Recursive implementation of export |
|
106 | - * |
|
107 | - * @param mixed $value The value to export |
|
108 | - * @param int $indentation The indentation level of the 2nd+ line |
|
109 | - * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects |
|
110 | - * @return string |
|
111 | - * @see SebastianBergmann\Exporter\Exporter::export |
|
112 | - */ |
|
113 | - protected static function recursiveExport(&$value, $indentation, $processed = null) |
|
114 | - { |
|
115 | - if ($value === null) { |
|
116 | - return 'null'; |
|
117 | - } |
|
118 | - |
|
119 | - if ($value === true) { |
|
120 | - return 'true'; |
|
121 | - } |
|
122 | - |
|
123 | - if ($value === false) { |
|
124 | - return 'false'; |
|
125 | - } |
|
126 | - |
|
127 | - if (is_float($value) && floatval(intval($value)) === $value) { |
|
128 | - return "$value.0"; |
|
129 | - } |
|
130 | - |
|
131 | - if (is_resource($value)) { |
|
132 | - return sprintf( |
|
133 | - 'resource(%d) of type (%s)', |
|
134 | - $value, |
|
135 | - get_resource_type($value) |
|
136 | - ); |
|
137 | - } |
|
138 | - |
|
139 | - if (is_string($value)) { |
|
140 | - // Match for most non printable chars somewhat taking multibyte chars into account |
|
141 | - if (preg_match('/[^\x09-\x0d\x20-\xff]/', $value)) { |
|
142 | - return 'Binary String: 0x' . bin2hex($value); |
|
143 | - } |
|
144 | - |
|
145 | - return "'" . |
|
146 | - str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) . |
|
147 | - "'"; |
|
148 | - } |
|
149 | - |
|
150 | - $whitespace = str_repeat(' ', 4 * $indentation); |
|
151 | - |
|
152 | - if (!$processed) { |
|
153 | - $processed = new Context; |
|
154 | - } |
|
155 | - |
|
156 | - if (is_array($value)) { |
|
157 | - if (($key = $processed->contains($value)) !== false) { |
|
158 | - return 'Array &' . $key; |
|
159 | - } |
|
160 | - |
|
161 | - $array = $value; |
|
162 | - $key = $processed->add($value); |
|
163 | - $values = ''; |
|
164 | - |
|
165 | - if (count($array) > 0) { |
|
166 | - foreach ($array as $k => $v) { |
|
167 | - $values .= sprintf( |
|
168 | - '%s %s => %s' . "\n", |
|
169 | - $whitespace, |
|
170 | - self::recursiveExport($k, $indentation), |
|
171 | - self::recursiveExport($value[$k], $indentation + 1, $processed) |
|
172 | - ); |
|
173 | - } |
|
174 | - |
|
175 | - $values = "\n" . $values . $whitespace; |
|
176 | - } |
|
177 | - |
|
178 | - return sprintf('Array &%s (%s)', $key, $values); |
|
179 | - } |
|
180 | - |
|
181 | - if (is_object($value)) { |
|
182 | - $class = get_class($value); |
|
183 | - |
|
184 | - if ($hash = $processed->contains($value)) { |
|
185 | - return sprintf('%s:%s Object', $class, $hash); |
|
186 | - } |
|
187 | - |
|
188 | - $hash = $processed->add($value); |
|
189 | - $values = ''; |
|
190 | - $array = self::toArray($value); |
|
191 | - |
|
192 | - if (count($array) > 0) { |
|
193 | - foreach ($array as $k => $v) { |
|
194 | - $values .= sprintf( |
|
195 | - '%s %s => %s' . "\n", |
|
196 | - $whitespace, |
|
197 | - self::recursiveExport($k, $indentation), |
|
198 | - self::recursiveExport($v, $indentation + 1, $processed) |
|
199 | - ); |
|
200 | - } |
|
201 | - |
|
202 | - $values = "\n" . $values . $whitespace; |
|
203 | - } |
|
204 | - |
|
205 | - return sprintf('%s:%s Object (%s)', $class, $hash, $values); |
|
206 | - } |
|
207 | - |
|
208 | - return var_export($value, true); |
|
209 | - } |
|
23 | + /** |
|
24 | + * Exports a value as a string |
|
25 | + * |
|
26 | + * The output of this method is similar to the output of print_r(), but |
|
27 | + * improved in various aspects: |
|
28 | + * |
|
29 | + * - NULL is rendered as "null" (instead of "") |
|
30 | + * - TRUE is rendered as "true" (instead of "1") |
|
31 | + * - FALSE is rendered as "false" (instead of "") |
|
32 | + * - Strings are always quoted with single quotes |
|
33 | + * - Carriage returns and newlines are normalized to \n |
|
34 | + * - Recursion and repeated rendering is treated properly |
|
35 | + * |
|
36 | + * @param mixed $value |
|
37 | + * @param int $indentation The indentation level of the 2nd+ line |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + public static function export($value, $indentation = 0) |
|
41 | + { |
|
42 | + return self::recursiveExport($value, $indentation); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Converts an object to an array containing all of its private, protected |
|
47 | + * and public properties. |
|
48 | + * |
|
49 | + * @param mixed $value |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public static function toArray($value) |
|
53 | + { |
|
54 | + if (!is_object($value)) { |
|
55 | + return (array) $value; |
|
56 | + } |
|
57 | + |
|
58 | + $array = array(); |
|
59 | + |
|
60 | + foreach ((array) $value as $key => $val) { |
|
61 | + // properties are transformed to keys in the following way: |
|
62 | + // private $property => "\0Classname\0property" |
|
63 | + // protected $property => "\0*\0property" |
|
64 | + // public $property => "property" |
|
65 | + if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) { |
|
66 | + $key = $matches[1]; |
|
67 | + } |
|
68 | + |
|
69 | + // See https://github.com/php/php-src/commit/5721132 |
|
70 | + if ($key === "\0gcdata") { |
|
71 | + continue; |
|
72 | + } |
|
73 | + |
|
74 | + $array[$key] = $val; |
|
75 | + } |
|
76 | + |
|
77 | + // Some internal classes like SplObjectStorage don't work with the |
|
78 | + // above (fast) mechanism nor with reflection in Zend. |
|
79 | + // Format the output similarly to print_r() in this case |
|
80 | + if ($value instanceof \SplObjectStorage) { |
|
81 | + // However, the fast method does work in HHVM, and exposes the |
|
82 | + // internal implementation. Hide it again. |
|
83 | + if (property_exists('\SplObjectStorage', '__storage')) { |
|
84 | + unset($array['__storage']); |
|
85 | + } elseif (property_exists('\SplObjectStorage', 'storage')) { |
|
86 | + unset($array['storage']); |
|
87 | + } |
|
88 | + |
|
89 | + if (property_exists('\SplObjectStorage', '__key')) { |
|
90 | + unset($array['__key']); |
|
91 | + } |
|
92 | + |
|
93 | + foreach ($value as $key => $val) { |
|
94 | + $array[spl_object_hash($val)] = array( |
|
95 | + 'obj' => $val, |
|
96 | + 'inf' => $value->getInfo(), |
|
97 | + ); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + return $array; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Recursive implementation of export |
|
106 | + * |
|
107 | + * @param mixed $value The value to export |
|
108 | + * @param int $indentation The indentation level of the 2nd+ line |
|
109 | + * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects |
|
110 | + * @return string |
|
111 | + * @see SebastianBergmann\Exporter\Exporter::export |
|
112 | + */ |
|
113 | + protected static function recursiveExport(&$value, $indentation, $processed = null) |
|
114 | + { |
|
115 | + if ($value === null) { |
|
116 | + return 'null'; |
|
117 | + } |
|
118 | + |
|
119 | + if ($value === true) { |
|
120 | + return 'true'; |
|
121 | + } |
|
122 | + |
|
123 | + if ($value === false) { |
|
124 | + return 'false'; |
|
125 | + } |
|
126 | + |
|
127 | + if (is_float($value) && floatval(intval($value)) === $value) { |
|
128 | + return "$value.0"; |
|
129 | + } |
|
130 | + |
|
131 | + if (is_resource($value)) { |
|
132 | + return sprintf( |
|
133 | + 'resource(%d) of type (%s)', |
|
134 | + $value, |
|
135 | + get_resource_type($value) |
|
136 | + ); |
|
137 | + } |
|
138 | + |
|
139 | + if (is_string($value)) { |
|
140 | + // Match for most non printable chars somewhat taking multibyte chars into account |
|
141 | + if (preg_match('/[^\x09-\x0d\x20-\xff]/', $value)) { |
|
142 | + return 'Binary String: 0x' . bin2hex($value); |
|
143 | + } |
|
144 | + |
|
145 | + return "'" . |
|
146 | + str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) . |
|
147 | + "'"; |
|
148 | + } |
|
149 | + |
|
150 | + $whitespace = str_repeat(' ', 4 * $indentation); |
|
151 | + |
|
152 | + if (!$processed) { |
|
153 | + $processed = new Context; |
|
154 | + } |
|
155 | + |
|
156 | + if (is_array($value)) { |
|
157 | + if (($key = $processed->contains($value)) !== false) { |
|
158 | + return 'Array &' . $key; |
|
159 | + } |
|
160 | + |
|
161 | + $array = $value; |
|
162 | + $key = $processed->add($value); |
|
163 | + $values = ''; |
|
164 | + |
|
165 | + if (count($array) > 0) { |
|
166 | + foreach ($array as $k => $v) { |
|
167 | + $values .= sprintf( |
|
168 | + '%s %s => %s' . "\n", |
|
169 | + $whitespace, |
|
170 | + self::recursiveExport($k, $indentation), |
|
171 | + self::recursiveExport($value[$k], $indentation + 1, $processed) |
|
172 | + ); |
|
173 | + } |
|
174 | + |
|
175 | + $values = "\n" . $values . $whitespace; |
|
176 | + } |
|
177 | + |
|
178 | + return sprintf('Array &%s (%s)', $key, $values); |
|
179 | + } |
|
180 | + |
|
181 | + if (is_object($value)) { |
|
182 | + $class = get_class($value); |
|
183 | + |
|
184 | + if ($hash = $processed->contains($value)) { |
|
185 | + return sprintf('%s:%s Object', $class, $hash); |
|
186 | + } |
|
187 | + |
|
188 | + $hash = $processed->add($value); |
|
189 | + $values = ''; |
|
190 | + $array = self::toArray($value); |
|
191 | + |
|
192 | + if (count($array) > 0) { |
|
193 | + foreach ($array as $k => $v) { |
|
194 | + $values .= sprintf( |
|
195 | + '%s %s => %s' . "\n", |
|
196 | + $whitespace, |
|
197 | + self::recursiveExport($k, $indentation), |
|
198 | + self::recursiveExport($v, $indentation + 1, $processed) |
|
199 | + ); |
|
200 | + } |
|
201 | + |
|
202 | + $values = "\n" . $values . $whitespace; |
|
203 | + } |
|
204 | + |
|
205 | + return sprintf('%s:%s Object (%s)', $class, $hash, $values); |
|
206 | + } |
|
207 | + |
|
208 | + return var_export($value, true); |
|
209 | + } |
|
210 | 210 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public static function toArray($value) |
53 | 53 | { |
54 | - if (!is_object($value)) { |
|
54 | + if ( ! is_object($value)) { |
|
55 | 55 | return (array) $value; |
56 | 56 | } |
57 | 57 | |
@@ -139,23 +139,23 @@ discard block |
||
139 | 139 | if (is_string($value)) { |
140 | 140 | // Match for most non printable chars somewhat taking multibyte chars into account |
141 | 141 | if (preg_match('/[^\x09-\x0d\x20-\xff]/', $value)) { |
142 | - return 'Binary String: 0x' . bin2hex($value); |
|
142 | + return 'Binary String: 0x'.bin2hex($value); |
|
143 | 143 | } |
144 | 144 | |
145 | - return "'" . |
|
146 | - str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) . |
|
145 | + return "'". |
|
146 | + str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value). |
|
147 | 147 | "'"; |
148 | 148 | } |
149 | 149 | |
150 | 150 | $whitespace = str_repeat(' ', 4 * $indentation); |
151 | 151 | |
152 | - if (!$processed) { |
|
152 | + if ( ! $processed) { |
|
153 | 153 | $processed = new Context; |
154 | 154 | } |
155 | 155 | |
156 | 156 | if (is_array($value)) { |
157 | 157 | if (($key = $processed->contains($value)) !== false) { |
158 | - return 'Array &' . $key; |
|
158 | + return 'Array &'.$key; |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | $array = $value; |
@@ -165,14 +165,14 @@ discard block |
||
165 | 165 | if (count($array) > 0) { |
166 | 166 | foreach ($array as $k => $v) { |
167 | 167 | $values .= sprintf( |
168 | - '%s %s => %s' . "\n", |
|
168 | + '%s %s => %s'."\n", |
|
169 | 169 | $whitespace, |
170 | 170 | self::recursiveExport($k, $indentation), |
171 | 171 | self::recursiveExport($value[$k], $indentation + 1, $processed) |
172 | 172 | ); |
173 | 173 | } |
174 | 174 | |
175 | - $values = "\n" . $values . $whitespace; |
|
175 | + $values = "\n".$values.$whitespace; |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | return sprintf('Array &%s (%s)', $key, $values); |
@@ -192,14 +192,14 @@ discard block |
||
192 | 192 | if (count($array) > 0) { |
193 | 193 | foreach ($array as $k => $v) { |
194 | 194 | $values .= sprintf( |
195 | - '%s %s => %s' . "\n", |
|
195 | + '%s %s => %s'."\n", |
|
196 | 196 | $whitespace, |
197 | 197 | self::recursiveExport($k, $indentation), |
198 | 198 | self::recursiveExport($v, $indentation + 1, $processed) |
199 | 199 | ); |
200 | 200 | } |
201 | 201 | |
202 | - $values = "\n" . $values . $whitespace; |
|
202 | + $values = "\n".$values.$whitespace; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | return sprintf('%s:%s Object (%s)', $class, $hash, $values); |
@@ -31,59 +31,59 @@ |
||
31 | 31 | */ |
32 | 32 | final class Fqsen |
33 | 33 | { |
34 | - /** @var string full quallified class name */ |
|
35 | - private $fqsen; |
|
34 | + /** @var string full quallified class name */ |
|
35 | + private $fqsen; |
|
36 | 36 | |
37 | - /** @var string name of the element without path. */ |
|
38 | - private $name; |
|
37 | + /** @var string name of the element without path. */ |
|
38 | + private $name; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Initializes the object. |
|
42 | - * |
|
43 | - * @throws InvalidArgumentException when $fqsen is not matching the format. |
|
44 | - */ |
|
45 | - public function __construct(string $fqsen) |
|
46 | - { |
|
47 | - $matches = []; |
|
40 | + /** |
|
41 | + * Initializes the object. |
|
42 | + * |
|
43 | + * @throws InvalidArgumentException when $fqsen is not matching the format. |
|
44 | + */ |
|
45 | + public function __construct(string $fqsen) |
|
46 | + { |
|
47 | + $matches = []; |
|
48 | 48 | |
49 | - $result = preg_match( |
|
50 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
51 | - '/^\\\\([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff\\\\]*)?(?:[:]{2}\\$?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*))?(?:\\(\\))?$/', |
|
52 | - $fqsen, |
|
53 | - $matches |
|
54 | - ); |
|
49 | + $result = preg_match( |
|
50 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
51 | + '/^\\\\([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff\\\\]*)?(?:[:]{2}\\$?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*))?(?:\\(\\))?$/', |
|
52 | + $fqsen, |
|
53 | + $matches |
|
54 | + ); |
|
55 | 55 | |
56 | - if ($result === 0) { |
|
57 | - throw new InvalidArgumentException( |
|
58 | - sprintf('"%s" is not a valid Fqsen.', $fqsen) |
|
59 | - ); |
|
60 | - } |
|
56 | + if ($result === 0) { |
|
57 | + throw new InvalidArgumentException( |
|
58 | + sprintf('"%s" is not a valid Fqsen.', $fqsen) |
|
59 | + ); |
|
60 | + } |
|
61 | 61 | |
62 | - $this->fqsen = $fqsen; |
|
62 | + $this->fqsen = $fqsen; |
|
63 | 63 | |
64 | - if (isset($matches[2])) { |
|
65 | - $this->name = $matches[2]; |
|
66 | - } else { |
|
67 | - $matches = explode('\\', $fqsen); |
|
68 | - $name = end($matches); |
|
69 | - assert(is_string($name)); |
|
70 | - $this->name = trim($name, '()'); |
|
71 | - } |
|
72 | - } |
|
64 | + if (isset($matches[2])) { |
|
65 | + $this->name = $matches[2]; |
|
66 | + } else { |
|
67 | + $matches = explode('\\', $fqsen); |
|
68 | + $name = end($matches); |
|
69 | + assert(is_string($name)); |
|
70 | + $this->name = trim($name, '()'); |
|
71 | + } |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * converts this class to string. |
|
76 | - */ |
|
77 | - public function __toString() : string |
|
78 | - { |
|
79 | - return $this->fqsen; |
|
80 | - } |
|
74 | + /** |
|
75 | + * converts this class to string. |
|
76 | + */ |
|
77 | + public function __toString() : string |
|
78 | + { |
|
79 | + return $this->fqsen; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Returns the name of the element without path. |
|
84 | - */ |
|
85 | - public function getName() : string |
|
86 | - { |
|
87 | - return $this->name; |
|
88 | - } |
|
82 | + /** |
|
83 | + * Returns the name of the element without path. |
|
84 | + */ |
|
85 | + public function getName() : string |
|
86 | + { |
|
87 | + return $this->name; |
|
88 | + } |
|
89 | 89 | } |
@@ -18,18 +18,18 @@ |
||
18 | 18 | */ |
19 | 19 | interface File |
20 | 20 | { |
21 | - /** |
|
22 | - * Returns the content of the file as a string. |
|
23 | - */ |
|
24 | - public function getContents() : string; |
|
21 | + /** |
|
22 | + * Returns the content of the file as a string. |
|
23 | + */ |
|
24 | + public function getContents() : string; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns md5 hash of the file. |
|
28 | - */ |
|
29 | - public function md5() : string; |
|
26 | + /** |
|
27 | + * Returns md5 hash of the file. |
|
28 | + */ |
|
29 | + public function md5() : string; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Returns an relative path to the file. |
|
33 | - */ |
|
34 | - public function path() : string; |
|
31 | + /** |
|
32 | + * Returns an relative path to the file. |
|
33 | + */ |
|
34 | + public function path() : string; |
|
35 | 35 | } |
@@ -19,10 +19,10 @@ |
||
19 | 19 | */ |
20 | 20 | interface ProjectFactory |
21 | 21 | { |
22 | - /** |
|
23 | - * Creates a project from the set of files. |
|
24 | - * |
|
25 | - * @param File[] $files |
|
26 | - */ |
|
27 | - public function create(string $name, array $files) : Project; |
|
22 | + /** |
|
23 | + * Creates a project from the set of files. |
|
24 | + * |
|
25 | + * @param File[] $files |
|
26 | + */ |
|
27 | + public function create(string $name, array $files) : Project; |
|
28 | 28 | } |
@@ -18,8 +18,8 @@ |
||
18 | 18 | */ |
19 | 19 | interface Project |
20 | 20 | { |
21 | - /** |
|
22 | - * Returns the name of the project. |
|
23 | - */ |
|
24 | - public function getName() : string; |
|
21 | + /** |
|
22 | + * Returns the name of the project. |
|
23 | + */ |
|
24 | + public function getName() : string; |
|
25 | 25 | } |
@@ -20,34 +20,34 @@ |
||
20 | 20 | */ |
21 | 21 | final class Location |
22 | 22 | { |
23 | - /** @var int */ |
|
24 | - private $lineNumber = 0; |
|
25 | - |
|
26 | - /** @var int */ |
|
27 | - private $columnNumber = 0; |
|
28 | - |
|
29 | - /** |
|
30 | - * Initializes the location for an element using its line number in the file and optionally the column number. |
|
31 | - */ |
|
32 | - public function __construct(int $lineNumber, int $columnNumber = 0) |
|
33 | - { |
|
34 | - $this->lineNumber = $lineNumber; |
|
35 | - $this->columnNumber = $columnNumber; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Returns the line number that is covered by this location. |
|
40 | - */ |
|
41 | - public function getLineNumber() : int |
|
42 | - { |
|
43 | - return $this->lineNumber; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns the column number (character position on a line) for this location object. |
|
48 | - */ |
|
49 | - public function getColumnNumber() : int |
|
50 | - { |
|
51 | - return $this->columnNumber; |
|
52 | - } |
|
23 | + /** @var int */ |
|
24 | + private $lineNumber = 0; |
|
25 | + |
|
26 | + /** @var int */ |
|
27 | + private $columnNumber = 0; |
|
28 | + |
|
29 | + /** |
|
30 | + * Initializes the location for an element using its line number in the file and optionally the column number. |
|
31 | + */ |
|
32 | + public function __construct(int $lineNumber, int $columnNumber = 0) |
|
33 | + { |
|
34 | + $this->lineNumber = $lineNumber; |
|
35 | + $this->columnNumber = $columnNumber; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Returns the line number that is covered by this location. |
|
40 | + */ |
|
41 | + public function getLineNumber() : int |
|
42 | + { |
|
43 | + return $this->lineNumber; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns the column number (character position on a line) for this location object. |
|
48 | + */ |
|
49 | + public function getColumnNumber() : int |
|
50 | + { |
|
51 | + return $this->columnNumber; |
|
52 | + } |
|
53 | 53 | } |
@@ -18,13 +18,13 @@ |
||
18 | 18 | */ |
19 | 19 | interface Element |
20 | 20 | { |
21 | - /** |
|
22 | - * Returns the Fqsen of the element. |
|
23 | - */ |
|
24 | - public function getFqsen() : Fqsen; |
|
21 | + /** |
|
22 | + * Returns the Fqsen of the element. |
|
23 | + */ |
|
24 | + public function getFqsen() : Fqsen; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns the name of the element. |
|
28 | - */ |
|
29 | - public function getName() : string; |
|
26 | + /** |
|
27 | + * Returns the name of the element. |
|
28 | + */ |
|
29 | + public function getName() : string; |
|
30 | 30 | } |