2828
2929/**
3030 * Type representation
31+ *
32+ * TODO Not nullable
3133 */
3234class Type {
3335
@@ -38,6 +40,11 @@ class Type {
3840 */
3941 protected $ name = '' ;
4042
43+ /**
44+ * @var bool Is field nullable?
45+ */
46+ protected $ nullable = true ;
47+
4148 /**
4249 * @var bool Is type mixed?
4350 */
@@ -52,15 +59,50 @@ class Type {
5259 * @param string $type Type name of class
5360 */
5461 public function __construct (string $ type ) {
55- $ type = strtolower ($ type );
56-
5762 $ this ->name = $ type ;
5863
64+ $ type = strtolower ($ type );
65+
5966 if ($ type === 'mixed ' ) {
6067 $ this ->mixed = true ;
6168 } else if (!in_array ($ type , self ::TYPES )) {
6269 $ this ->class = true ;
6370 }
71+
72+ if (!$ this ->class ) {
73+ $ this ->name = $ type ;
74+ }
75+ }
76+
77+ /**
78+ * Returns default value for this type
79+ *
80+ * @return string
81+ */
82+ public function getDefaultValue (): string {
83+ if ($ this ->nullable || $ this ->mixed || $ this ->class ) {
84+ return 'null ' ;
85+ }
86+
87+ switch ($ this ->name ) {
88+ case 'array ' :
89+ return '[] ' ;
90+
91+ case 'bool ' :
92+ return 'false ' ;
93+
94+ case 'float ' :
95+ return '0.0 ' ;
96+
97+ case 'int ' :
98+ return '0 ' ;
99+
100+ case 'string ' :
101+ return '"" ' ;
102+
103+ }
104+
105+ return 'null ' ;
64106 }
65107
66108 /**
@@ -78,37 +120,35 @@ public function fixClassName(FileModel $fileModel) {
78120 return ;
79121 }
80122
81- $ this ->name = $ fileModel ->getClassPath ($ this ->name );
123+ $ this ->name = '\\' . $ fileModel ->getClassPath ($ this ->name );
82124 }
83125
84126 /**
85- * Makes type tip for return
127+ * Makes validation code for this type
86128 *
87129 * If type is mixed returns empty string
88130 *
131+ * @param string $expr Expression for validating
132+ * @param string $exception Message for exception
133+ *
89134 * @return string Type tip prefixed by ": " or empty
90135 */
91- public function makeReturnTypeTip ( ): string {
136+ public function makeValidator ( string $ expr , string $ exception = ' Invalid type ' ): string {
92137 if ($ this ->mixed ) {
93138 return '' ;
94139 }
95140
96- return ': ' .$ this ->name ;
97- }
141+ $ result = 'if (!is_ ' ;
98142
99- /**
100- * Makes type tip for argument
101- *
102- * If type is mixed returns empty string
103- *
104- * @return string Type tip suffixed by " " or empty
105- */
106- public function makeArgumentTypeTip (): string {
107- if ($ this ->mixed ) {
108- return '' ;
143+ if ($ this ->class ) {
144+ $ result .= "a( $ expr, {$ this ->name }::class) " ;
145+ } else {
146+ $ result .= "{$ this ->name }( $ expr) " ;
109147 }
110148
111- return $ this ->name .' ' ;
149+ $ result .= " && !is_null( $ expr)) { throw new \InvalidArgumentException(' $ exception'); } " ;
150+
151+ return "$ result \n" ;
112152 }
113153
114154 public function getName (): string {
0 commit comments