Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit e528d48

Browse files
author
Elliot Levin
committed
Introduce Analysis\INativeType::TYPE_NUMERIC acting as a union type for Analysis\INativeType::TYPE_INT and Analysis\INativeType::TYPE_DOUBLE
1 parent 00514d8 commit e528d48

File tree

6 files changed

+52
-26
lines changed

6 files changed

+52
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ dev
44
- Fix bug when attempting to parse function with a magic scope parameter type hint (eg `function (self $param) { ... }`.
55
- Implement `Analysis\TolerantExpressionAnalyser` which will convert analysis exceptions into the *mixed* type.
66
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::contains` to check whether the collection contains a parameter.
7-
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::remove` to remove a previously added parameter.
7+
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::remove` to remove a previously added parameter.\
8+
- Introduce `Analysis\INativeType::TYPE_NUMERIC` acting as a union type for `Analysis\INativeType::TYPE_INT` and `Analysis\INativeType::TYPE_DOUBLE`
89

910
3.1.0 (29/3/15)
1011
===============

Source/Analysis/INativeType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
interface INativeType extends IType
1111
{
1212
const TYPE_MIXED = 'native:mixed';
13+
const TYPE_NUMERIC = 'native:numeric';
1314
const TYPE_STRING = 'native:string';
1415
const TYPE_INT = 'native:int';
1516
const TYPE_ARRAY = 'native:array';

Source/Analysis/ITypeSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getObjectType($classType);
7373
* @param IType[] $types
7474
*
7575
* @return IType
76-
* @throws TypeException If the types is not supported.
76+
* @throws TypeException If the type is not supported.
7777
*/
7878
public function getCompositeType(array $types);
7979

Source/Analysis/PhpTypeSystem.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,14 @@ protected function nativeCasts()
190190

191191
protected function nativeType(
192192
$typeOfType,
193+
IType $parentType,
193194
IIndexer $indexer = null,
194195
array $unaryOperatorMap = [],
195196
array $castMap = []
196197
) {
197198
return new NativeType(
198199
$typeOfType,
199-
$this->nativeTypes[INativeType::TYPE_MIXED],
200+
$parentType,
200201
$typeOfType,
201202
$indexer,
202203
$this->buildTypeOperations($typeOfType, array_filter($castMap + $this->nativeCasts())),
@@ -217,12 +218,25 @@ protected function commonNativeUnaryOperations()
217218

218219
protected function nativeTypes()
219220
{
220-
$this->nativeTypes[INativeType::TYPE_MIXED] = new MixedType(INativeType::TYPE_MIXED);
221-
222221
return [
223-
$this->nativeTypes[INativeType::TYPE_MIXED],
222+
$mixedType = new MixedType(INativeType::TYPE_MIXED),
223+
$numericType = $this->nativeType(
224+
INativeType::TYPE_NUMERIC,
225+
$mixedType,
226+
null,
227+
[
228+
Operators\Unary::BITWISE_NOT => INativeType::TYPE_INT,
229+
Operators\Unary::PLUS => INativeType::TYPE_NUMERIC,
230+
Operators\Unary::NEGATION => INativeType::TYPE_NUMERIC,
231+
Operators\Unary::INCREMENT => INativeType::TYPE_NUMERIC,
232+
Operators\Unary::DECREMENT => INativeType::TYPE_NUMERIC,
233+
Operators\Unary::PRE_INCREMENT => INativeType::TYPE_NUMERIC,
234+
Operators\Unary::PRE_DECREMENT => INativeType::TYPE_NUMERIC,
235+
]
236+
),
224237
$this->nativeType(
225238
INativeType::TYPE_STRING,
239+
$mixedType,
226240
new Indexer($this, INativeType::TYPE_STRING, INativeType::TYPE_STRING),
227241
[
228242
Operators\Unary::BITWISE_NOT => INativeType::TYPE_STRING,
@@ -234,6 +248,7 @@ protected function nativeTypes()
234248
),
235249
$this->nativeType(
236250
INativeType::TYPE_ARRAY,
251+
$mixedType,
237252
new Indexer($this, INativeType::TYPE_ARRAY, INativeType::TYPE_MIXED),
238253
[
239254
Operators\Unary::PLUS => null,
@@ -245,6 +260,7 @@ protected function nativeTypes()
245260
),
246261
$this->nativeType(
247262
INativeType::TYPE_INT,
263+
$numericType,
248264
null,
249265
[
250266
Operators\Unary::BITWISE_NOT => INativeType::TYPE_INT,
@@ -254,18 +270,9 @@ protected function nativeTypes()
254270
Operators\Unary::PRE_DECREMENT => INativeType::TYPE_INT,
255271
]
256272
),
257-
$this->nativeType(
258-
INativeType::TYPE_BOOL,
259-
null,
260-
[
261-
Operators\Unary::INCREMENT => INativeType::TYPE_BOOL,
262-
Operators\Unary::DECREMENT => INativeType::TYPE_BOOL,
263-
Operators\Unary::PRE_INCREMENT => INativeType::TYPE_BOOL,
264-
Operators\Unary::PRE_DECREMENT => INativeType::TYPE_BOOL,
265-
]
266-
),
267273
$this->nativeType(
268274
INativeType::TYPE_DOUBLE,
275+
$numericType,
269276
null,
270277
[
271278
Operators\Unary::BITWISE_NOT => INativeType::TYPE_INT,
@@ -277,8 +284,19 @@ protected function nativeTypes()
277284
Operators\Unary::PRE_DECREMENT => INativeType::TYPE_DOUBLE,
278285
]
279286
),
280-
$this->nativeType(INativeType::TYPE_NULL),
281-
$this->nativeType(INativeType::TYPE_RESOURCE),
287+
$this->nativeType(
288+
INativeType::TYPE_BOOL,
289+
$mixedType,
290+
null,
291+
[
292+
Operators\Unary::INCREMENT => INativeType::TYPE_BOOL,
293+
Operators\Unary::DECREMENT => INativeType::TYPE_BOOL,
294+
Operators\Unary::PRE_INCREMENT => INativeType::TYPE_BOOL,
295+
Operators\Unary::PRE_DECREMENT => INativeType::TYPE_BOOL,
296+
]
297+
),
298+
$this->nativeType(INativeType::TYPE_NULL, $mixedType),
299+
$this->nativeType(INativeType::TYPE_RESOURCE, $mixedType),
282300
];
283301
}
284302

@@ -431,6 +449,7 @@ protected function mathOperators($operator, $otherIntReturnType = INativeType::T
431449
foreach ([
432450
INativeType::TYPE_INT,
433451
INativeType::TYPE_DOUBLE,
452+
INativeType::TYPE_NUMERIC,
434453
INativeType::TYPE_STRING,
435454
INativeType::TYPE_RESOURCE,
436455
INativeType::TYPE_BOOL,
@@ -441,7 +460,7 @@ protected function mathOperators($operator, $otherIntReturnType = INativeType::T
441460
[
442461
[$type, $operator, INativeType::TYPE_NULL, 'return' => $otherIntReturnType],
443462
[$type, $operator, INativeType::TYPE_BOOL, 'return' => $otherIntReturnType],
444-
[$type, $operator, INativeType::TYPE_STRING, 'return' => INativeType::TYPE_MIXED],
463+
[$type, $operator, INativeType::TYPE_STRING, 'return' => INativeType::TYPE_NUMERIC],
445464
[$type, $operator, INativeType::TYPE_RESOURCE, 'return' => $otherIntReturnType],
446465
]
447466
);
@@ -471,6 +490,7 @@ protected function bitwiseOperators($operator)
471490
foreach ([
472491
INativeType::TYPE_INT,
473492
INativeType::TYPE_DOUBLE,
493+
INativeType::TYPE_NUMERIC,
474494
INativeType::TYPE_STRING,
475495
INativeType::TYPE_RESOURCE,
476496
INativeType::TYPE_BOOL,
@@ -524,7 +544,7 @@ protected function binaryOperations()
524544
$this->mathOperators(Operators\Binary::ADDITION),
525545
$this->mathOperators(Operators\Binary::SUBTRACTION),
526546
$this->mathOperators(Operators\Binary::MULTIPLICATION),
527-
$this->mathOperators(Operators\Binary::DIVISION, INativeType::TYPE_MIXED),
547+
$this->mathOperators(Operators\Binary::DIVISION, INativeType::TYPE_NUMERIC),
528548
$this->mathOperators(Operators\Binary::MODULUS),
529549
$this->mathOperators(Operators\Binary::POWER),
530550
$this->bitwiseOperators(Operators\Binary::BITWISE_AND),

Tests/Integration/Analysis/BasicExpressionAnalysisTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function () { false . true; },
342342
function () { false . 3.2; },
343343
function () { 3 . 9; },
344344
],
345-
INativeType::TYPE_MIXED => [
345+
INativeType::TYPE_NUMERIC => [
346346
function () { '123' + 1; },
347347
function () { '123' - 1; },
348348
function () { '123' * 1; },
@@ -395,11 +395,13 @@ public function testAssignmentOperators()
395395
INativeType::TYPE_STRING => [
396396
[function ($var) { $var .= 1; }],
397397
],
398+
INativeType::TYPE_NUMERIC => [
399+
[function () { $i += 1; }, ['i' => $this->typeSystem->getNativeType(INativeType::TYPE_STRING)]],
400+
[function () { $i -= 1; }, ['i' => $this->typeSystem->getNativeType(INativeType::TYPE_STRING)]],
401+
],
398402
INativeType::TYPE_MIXED => [
399403
[function ($var) { $i = $var; }],
400404
[function ($var) { $i =& $var; }],
401-
[function () { $i += 1; }, ['i' => $this->typeSystem->getNativeType(INativeType::TYPE_STRING)]],
402-
[function () { $i -= 1; }, ['i' => $this->typeSystem->getNativeType(INativeType::TYPE_STRING)]],
403405
],
404406
];
405407

@@ -443,9 +445,10 @@ function () { true ? '22' : ''; },
443445
function () { true ? 'abc' : '343'; },
444446
function () { true ? (string)2 : '343'; },
445447
],
446-
INativeType::TYPE_MIXED => [
447-
function () { true ? (string)2 : 343; },
448+
INativeType::TYPE_NUMERIC => [
448449
function () { true ? 1 : 2.0; },
450+
],
451+
INativeType::TYPE_MIXED => [
449452
function () { true ? strlen('') : 'abc'; },
450453
function () { true ? [] : 123; },
451454
function () { true ? [] : new \stdClass(); },

Tests/Integration/Analysis/TypeAnalysisTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ public function testTernary()
293293
INativeType::TYPE_DOUBLE => function () { 3 ? 343.23 : 2.34; },
294294
INativeType::TYPE_STRING => function () { false ? 'abce' : '1234.3'; },
295295
INativeType::TYPE_ARRAY => function () { false ? ['abc'] : [1,2,3]; },
296-
INativeType::TYPE_MIXED => function () { '' ? 3 : 4.3; },
296+
INativeType::TYPE_NUMERIC => function () { '' ? 3 : 4.3; },
297+
INativeType::TYPE_MIXED => function () { '' ? '3' : 4.3; },
297298
];
298299

299300
foreach($values as $expectedType => $expression) {

0 commit comments

Comments
 (0)