1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: namespace Kint\Object;
27:
28: use Kint\Utils;
29: use ReflectionException;
30: use ReflectionParameter;
31:
32: class ParameterObject extends BasicObject
33: {
34: public $type_hint;
35: public $default;
36: public $position;
37: public $hints = array('parameter');
38:
39: public function __construct(ReflectionParameter $param)
40: {
41: parent::__construct();
42:
43: if (KINT_PHP70) {
44: if ($type = $param->getType()) {
45: $this->type_hint = Utils::getTypeString($type);
46: }
47: } else {
48: if ($param->isArray()) {
49: $this->type_hint = 'array';
50: } else {
51: try {
52: if ($this->type_hint = $param->getClass()) {
53: $this->type_hint = $this->type_hint->name;
54: }
55: } catch (ReflectionException $e) {
56: \preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches);
57: $this->type_hint = isset($matches[1]) ? $matches[1] : '';
58: }
59: }
60: }
61:
62: $this->reference = $param->isPassedByReference();
63: $this->name = $param->getName();
64: $this->position = $param->getPosition();
65:
66: if ($param->isDefaultValueAvailable()) {
67: /** @var mixed Psalm bug workaround */
68: $default = $param->getDefaultValue();
69: switch (\gettype($default)) {
70: case 'NULL':
71: $this->default = 'null';
72: break;
73: case 'boolean':
74: $this->default = $default ? 'true' : 'false';
75: break;
76: case 'array':
77: $this->default = \count($default) ? 'array(...)' : 'array()';
78: break;
79: default:
80: $this->default = \var_export($default, true);
81: break;
82: }
83: }
84: }
85:
86: public function getType()
87: {
88: return $this->type_hint;
89: }
90:
91: public function getName()
92: {
93: return '$'.$this->name;
94: }
95:
96: public function getDefault()
97: {
98: return $this->default;
99: }
100: }
101: