Total Complexity | 118 |
Total Lines | 950 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 0 | Features | 2 |
Complex classes like ExpressionTypeChecker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExpressionTypeChecker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | abstract class ExpressionTypeChecker |
||
53 | { |
||
54 | private static $promotionMap = null; |
||
55 | |||
56 | private static function getPromotionMap(): array |
||
120 | ], |
||
121 | ]; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Determines if the type of an expression is compatible with the provided type. |
||
126 | * |
||
127 | * If the expression has an associated type, this function will check that it matches the expected type and stop |
||
128 | * looking further. If an expression claims a type, it must be validated that the type is valid for the expression. |
||
129 | * If the expression does not claim a type this method will attempt to check the validity of the expression itself |
||
130 | * with the asserted type. |
||
131 | * |
||
132 | * @param IExpression|null $expression the expression to assert the type of |
||
133 | * @param ITypeReference|null $type the type to assert the expression as |
||
134 | * @param IType|null $context the context paths are to be evaluated in |
||
135 | * @param bool $matchExactly Must the expression must match the asserted type exactly, or simply be compatible? |
||
136 | * @param iterable $discoveredErrors errors produced if the expression does not match the specified type |
||
137 | * @return bool a value indicating whether the expression is valid for the given type or not |
||
138 | */ |
||
139 | public static function tryAssertType( |
||
300 | } |
||
301 | } |
||
302 | |||
303 | public static function tryAssertPrimitiveAsType( |
||
304 | IPrimitiveValue $expression, |
||
305 | ITypeReference $type, |
||
306 | iterable &$discoveredErrors |
||
307 | ): bool { |
||
308 | if (!$type->isPrimitive()) { |
||
309 | $discoveredErrors = [ |
||
310 | new EdmError( |
||
311 | $expression->location(), |
||
312 | EdmErrorCode::PrimitiveConstantExpressionNotValidForNonPrimitiveType(), |
||
313 | StringConst::EdmModel_Validator_Semantic_PrimitiveConstantExpressionNotValidForNonPrimitiveType() |
||
314 | ) |
||
315 | ]; |
||
316 | return false; |
||
317 | } |
||
318 | |||
319 | switch ($expression->getValueKind()) { |
||
320 | case ValueKind::Binary(): |
||
321 | assert($expression instanceof IBinaryConstantExpression); |
||
322 | return self::tryAssertBinaryConstantAsType($expression, $type, $discoveredErrors); |
||
323 | case ValueKind::Boolean(): |
||
324 | assert($expression instanceof IBooleanConstantExpression); |
||
325 | return self::tryAssertBooleanConstantAsType($expression, $type, $discoveredErrors); |
||
326 | case ValueKind::DateTime(): |
||
327 | assert($expression instanceof IDateTimeConstantExpression); |
||
328 | return self::tryAssertDateTimeConstantAsType($expression, $type, $discoveredErrors); |
||
329 | case ValueKind::DateTimeOffset(): |
||
330 | assert($expression instanceof IDateTimeOffsetConstantExpression); |
||
331 | return self::tryAssertDateTimeOffsetConstantAsType($expression, $type, $discoveredErrors); |
||
332 | case ValueKind::Decimal(): |
||
333 | assert($expression instanceof IDecimalConstantExpression); |
||
334 | return self::tryAssertDecimalConstantAsType($expression, $type, $discoveredErrors); |
||
335 | case ValueKind::Floating(): |
||
336 | assert($expression instanceof IFloatingConstantExpression); |
||
337 | return self::tryAssertFloatingConstantAsType($expression, $type, $discoveredErrors); |
||
338 | case ValueKind::Guid(): |
||
339 | assert($expression instanceof IGuidConstantExpression); |
||
340 | return self::tryAssertGuidConstantAsType($expression, $type, $discoveredErrors); |
||
341 | case ValueKind::Integer(): |
||
342 | assert($expression instanceof IIntegerConstantExpression); |
||
343 | return self::tryAssertIntegerConstantAsType($expression, $type, $discoveredErrors); |
||
344 | case ValueKind::String(): |
||
345 | assert($expression instanceof IStringConstantExpression); |
||
346 | return self::tryAssertStringConstantAsType($expression, $type, $discoveredErrors); |
||
347 | case ValueKind::Time(): |
||
348 | assert($expression instanceof ITimeConstantExpression); |
||
349 | return self::tryAssertTimeConstantAsType($expression, $type, $discoveredErrors); |
||
350 | default: |
||
351 | $discoveredErrors = [ |
||
352 | new EdmError( |
||
353 | $expression->location(), |
||
354 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
355 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
356 | ) |
||
357 | ]; |
||
358 | return false; |
||
359 | } |
||
360 | } |
||
361 | |||
362 | protected static function tryAssertNullAsType( |
||
363 | INullExpression $expression, |
||
364 | ITypeReference $type, |
||
365 | iterable &$discoveredErrors |
||
366 | ): bool { |
||
367 | if (!$type->getNullable()) { |
||
368 | $discoveredErrors = [ |
||
369 | new EdmError( |
||
370 | $expression->location(), |
||
371 | EdmErrorCode::NullCannotBeAssertedToBeANonNullableType(), |
||
372 | StringConst::EdmModel_Validator_Semantic_NullCannotBeAssertedToBeANonNullableType() |
||
373 | ) |
||
374 | ]; |
||
375 | return false; |
||
376 | } |
||
377 | |||
378 | $discoveredErrors = []; |
||
379 | return true; |
||
380 | } |
||
381 | |||
382 | protected static function tryAssertPathAsType( |
||
383 | IPathExpression $expression, |
||
384 | ITypeReference $type, |
||
385 | IType $context, |
||
386 | bool $matchExactly, |
||
387 | iterable &$discoveredErrors |
||
388 | ): bool { |
||
389 | $structuredContext = $context; |
||
390 | assert($structuredContext instanceof IStructuredType); |
||
391 | |||
392 | $result = $context; |
||
393 | $loc = $expression->location(); |
||
394 | EdmUtil::checkArgumentNull($loc, 'expression->Location'); |
||
395 | EdmUtil::checkArgumentNull($type->getDefinition(), 'type->getDefinition'); |
||
396 | |||
397 | foreach ($expression->getPath() as $segment) { |
||
398 | $structuredResult = $result; |
||
399 | if (!$structuredResult instanceof IStructuredType) { |
||
400 | $discoveredErrors = [ |
||
401 | new EdmError( |
||
402 | $loc, |
||
403 | EdmErrorCode::PathIsNotValidForTheGivenContext(), |
||
404 | StringConst::EdmModel_Validator_Semantic_PathIsNotValidForTheGivenContext($segment) |
||
405 | ) |
||
406 | ]; |
||
407 | return false; |
||
408 | } |
||
409 | |||
410 | $resultProperty = $structuredResult->findProperty($segment); |
||
411 | $result = (null !== $resultProperty) ? $resultProperty->getType()->getDefinition() : null; |
||
412 | |||
413 | // If the path is not resolved, it could refer to an open type, and we can't assert its type. |
||
414 | if (null === $result) { |
||
415 | $discoveredErrors = []; |
||
416 | return true; |
||
417 | } |
||
418 | } |
||
419 | |||
420 | return self::testTypeMatch( |
||
421 | $result, |
||
422 | $type->getDefinition(), |
||
423 | $loc, |
||
424 | $matchExactly, |
||
425 | $discoveredErrors |
||
426 | ); |
||
427 | } |
||
428 | |||
429 | protected static function tryAssertIfAsType( |
||
453 | } |
||
454 | |||
455 | public static function tryAssertRecordAsType( |
||
456 | IRecordExpression $expression, |
||
457 | ITypeReference $type, |
||
458 | ?IType $context, |
||
459 | bool $matchExactly, |
||
460 | iterable &$discoveredErrors |
||
461 | ): bool { |
||
462 | EdmUtil::checkArgumentNull($expression, 'expression'); |
||
463 | EdmUtil::checkArgumentNull($type, 'type'); |
||
464 | |||
465 | if (!$type->isStructured()) { |
||
466 | $discoveredErrors = [ |
||
467 | new EdmError( |
||
468 | $expression->location(), |
||
469 | EdmErrorCode::RecordExpressionNotValidForNonStructuredType(), |
||
470 | StringConst::EdmModel_Validator_Semantic_RecordExpressionNotValidForNonStructuredType() |
||
471 | ) |
||
472 | ]; |
||
473 | return false; |
||
474 | } |
||
475 | |||
476 | $foundProperties = new HashSetInternal(); |
||
477 | $errors = []; |
||
478 | |||
479 | $structuredType = $type->asStructured(); |
||
480 | $definition = $structuredType->getDefinition(); |
||
481 | assert($definition instanceof IStructuredType); |
||
482 | foreach ($definition->properties() as $typeProperty) { |
||
483 | $expressionProperty = null; |
||
484 | foreach ($expression->getProperties() as $p) { |
||
485 | if ($p->getName() === $typeProperty->getName()) { |
||
486 | $expressionProperty = $p; |
||
487 | break; |
||
488 | } |
||
489 | } |
||
490 | if (null === $expressionProperty) { |
||
491 | $errors[] = new EdmError( |
||
492 | $expression->location(), |
||
493 | EdmErrorCode::RecordExpressionMissingRequiredProperty(), |
||
494 | StringConst::EdmModel_Validator_Semantic_RecordExpressionMissingProperty($typeProperty->getName()) |
||
495 | ); |
||
496 | } else { |
||
497 | $recursiveErrors = []; |
||
498 | if (!self::tryAssertType( |
||
499 | $expressionProperty->getValue(), |
||
500 | $typeProperty->getType(), |
||
501 | $context, |
||
502 | $matchExactly, |
||
503 | $recursiveErrors |
||
504 | )) { |
||
505 | foreach ($recursiveErrors as $error) { |
||
506 | $errors[] = $error; |
||
507 | } |
||
508 | } |
||
509 | |||
510 | $foundProperties[] = $typeProperty->getName(); |
||
511 | } |
||
512 | } |
||
513 | $definition = $structuredType->getDefinition(); |
||
514 | assert($definition instanceof IStructuredType); |
||
515 | if (!$definition->isOpen()) { |
||
516 | foreach ($expression->getProperties() as $property) { |
||
517 | if (!$foundProperties->contains($property->getName())) { |
||
518 | $errors[] = new EdmError( |
||
519 | $expression->location(), |
||
520 | EdmErrorCode::RecordExpressionHasExtraProperties(), |
||
521 | StringConst::EdmModel_Validator_Semantic_RecordExpressionHasExtraProperties( |
||
522 | $property->getName() |
||
523 | ) |
||
524 | ); |
||
525 | } |
||
526 | } |
||
527 | } |
||
528 | |||
529 | if (count($errors) > 0 || $errors[0]) { |
||
530 | $discoveredErrors = $errors; |
||
531 | return false; |
||
532 | } |
||
533 | |||
534 | $discoveredErrors = []; |
||
535 | return true; |
||
536 | } |
||
537 | |||
538 | public static function tryAssertCollectionAsType( |
||
539 | ICollectionExpression $expression, |
||
540 | ITypeReference $type, |
||
541 | IType $context, |
||
542 | bool $matchExactly, |
||
543 | &$discoveredErrors |
||
544 | ): bool { |
||
545 | if (!$type->isCollection()) { |
||
546 | $discoveredErrors = [ |
||
547 | new EdmError( |
||
548 | $expression->location(), |
||
549 | EdmErrorCode::CollectionExpressionNotValidForNonCollectionType(), |
||
550 | StringConst::EdmModel_Validator_Semantic_CollectionExpressionNotValidForNonCollectionType() |
||
551 | ) |
||
552 | ]; |
||
553 | return false; |
||
554 | } |
||
555 | |||
556 | $collectionElementType = $type->asCollection()->elementType(); |
||
557 | $success = true; |
||
558 | $errors = []; |
||
559 | $recursiveErrors = []; |
||
560 | foreach ($expression->getElements() as $element) { |
||
561 | $result = self::tryAssertType( |
||
562 | $element, |
||
563 | $collectionElementType, |
||
564 | $context, |
||
565 | $matchExactly, |
||
566 | $recursiveErrors |
||
567 | ); |
||
568 | $success &= boolval($result); |
||
569 | $errors = array_merge($errors, $recursiveErrors); |
||
570 | } |
||
571 | |||
572 | $discoveredErrors = $errors; |
||
573 | return boolval($success); |
||
574 | } |
||
575 | |||
576 | private static function tryAssertGuidConstantAsType( |
||
577 | IGuidConstantExpression $expression, |
||
578 | ITypeReference $type, |
||
579 | &$discoveredErrors |
||
580 | ): bool { |
||
581 | if (!$type->isGuid()) { |
||
582 | $discoveredErrors = [ |
||
583 | new EdmError( |
||
584 | $expression->location(), |
||
585 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
586 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
587 | ) |
||
588 | ]; |
||
589 | return false; |
||
590 | } |
||
591 | |||
592 | $discoveredErrors = []; |
||
593 | return true; |
||
594 | } |
||
595 | |||
596 | private static function tryAssertFloatingConstantAsType( |
||
597 | IFloatingConstantExpression $expression, |
||
598 | ITypeReference $type, |
||
599 | &$discoveredErrors |
||
600 | ): bool { |
||
601 | if (!$type->isFloating()) { |
||
602 | $discoveredErrors = [ |
||
603 | new EdmError( |
||
604 | $expression->location(), |
||
605 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
606 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
607 | ) |
||
608 | ]; |
||
609 | return false; |
||
610 | } |
||
611 | |||
612 | $discoveredErrors = []; |
||
613 | return true; |
||
614 | } |
||
615 | |||
616 | private static function tryAssertDecimalConstantAsType( |
||
617 | IDecimalConstantExpression $expression, |
||
618 | ITypeReference $type, |
||
619 | &$discoveredErrors |
||
620 | ): bool { |
||
621 | if (!$type->isDecimal()) { |
||
622 | $discoveredErrors = [ |
||
623 | new EdmError( |
||
624 | $expression->location(), |
||
625 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
626 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
627 | ) |
||
628 | ]; |
||
629 | return false; |
||
630 | } |
||
631 | |||
632 | $discoveredErrors = []; |
||
633 | return true; |
||
634 | } |
||
635 | |||
636 | private static function tryAssertDateTimeOffsetConstantAsType( |
||
637 | IDateTimeOffsetConstantExpression $expression, |
||
638 | ITypeReference $type, |
||
639 | &$discoveredErrors |
||
640 | ): bool { |
||
641 | if (!$type->isDateTimeOffset()) { |
||
642 | $discoveredErrors = [ |
||
643 | new EdmError( |
||
644 | $expression->location(), |
||
645 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
646 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
647 | ) |
||
648 | ]; |
||
649 | return false; |
||
650 | } |
||
651 | |||
652 | $discoveredErrors = []; |
||
653 | return true; |
||
654 | } |
||
655 | |||
656 | private static function tryAssertDateTimeConstantAsType( |
||
657 | IDateTimeConstantExpression $expression, |
||
658 | ITypeReference $type, |
||
659 | &$discoveredErrors |
||
660 | ): bool { |
||
661 | if (!$type->isDateTime()) { |
||
662 | $discoveredErrors = [ |
||
663 | new EdmError( |
||
664 | $expression->location(), |
||
665 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
666 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
667 | ) |
||
668 | ]; |
||
669 | return false; |
||
670 | } |
||
671 | |||
672 | $discoveredErrors = []; |
||
673 | return true; |
||
674 | } |
||
675 | |||
676 | private static function tryAssertTimeConstantAsType( |
||
677 | ITimeConstantExpression $expression, |
||
678 | ITypeReference $type, |
||
679 | &$discoveredErrors |
||
680 | ): bool { |
||
681 | if (!$type->isTime()) { |
||
682 | $discoveredErrors = [ |
||
683 | new EdmError( |
||
684 | $expression->location(), |
||
685 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
686 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
687 | ) |
||
688 | ]; |
||
689 | return false; |
||
690 | } |
||
691 | |||
692 | $discoveredErrors = []; |
||
693 | return true; |
||
694 | } |
||
695 | |||
696 | private static function tryAssertBooleanConstantAsType( |
||
697 | IBooleanConstantExpression $expression, |
||
698 | ITypeReference $type, |
||
699 | &$discoveredErrors |
||
700 | ): bool { |
||
701 | if (!$type->isBoolean()) { |
||
702 | $discoveredErrors = [ |
||
703 | new EdmError( |
||
704 | $expression->location(), |
||
705 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
706 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
707 | ) |
||
708 | ]; |
||
709 | return false; |
||
710 | } |
||
711 | |||
712 | $discoveredErrors = []; |
||
713 | return true; |
||
714 | } |
||
715 | |||
716 | private static function tryAssertStringConstantAsType( |
||
717 | IStringConstantExpression $expression, |
||
718 | ITypeReference $type, |
||
719 | &$discoveredErrors |
||
720 | ) { |
||
721 | if (!$type->isString()) { |
||
722 | $discoveredErrors = [ |
||
723 | new EdmError( |
||
724 | $expression->location(), |
||
725 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
726 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
727 | ) |
||
728 | ]; |
||
729 | return false; |
||
730 | } |
||
731 | |||
732 | EdmUtil::checkArgumentNull($expression->getValue(), 'expression->getValue'); |
||
733 | $stringType = $type->asString(); |
||
734 | if (null !== $stringType->getMaxLength() && mb_strlen($expression->getValue()) > $stringType->getMaxLength()) { |
||
735 | $discoveredErrors = [ |
||
736 | new EdmError( |
||
737 | $expression->location(), |
||
738 | EdmErrorCode::StringConstantLengthOutOfRange(), |
||
739 | StringConst::EdmModel_Validator_Semantic_StringConstantLengthOutOfRange( |
||
740 | mb_strlen($expression->getValue()), |
||
741 | $stringType->getMaxLength() |
||
742 | ) |
||
743 | ) |
||
744 | ]; |
||
745 | return false; |
||
746 | } |
||
747 | |||
748 | $discoveredErrors = []; |
||
749 | return true; |
||
750 | } |
||
751 | |||
752 | private static function tryAssertIntegerConstantAsType( |
||
813 | } |
||
814 | } |
||
815 | |||
816 | private static function tryAssertIntegerConstantInRange( |
||
817 | IIntegerConstantExpression $expression, |
||
818 | int $min, |
||
819 | int $max, |
||
820 | &$discoveredErrors |
||
821 | ): bool { |
||
822 | if ($expression->getValue() < $min || $expression->getValue() > $max) { |
||
823 | $discoveredErrors = [ |
||
824 | new EdmError( |
||
825 | $expression->location(), |
||
826 | EdmErrorCode::IntegerConstantValueOutOfRange(), |
||
827 | StringConst::EdmModel_Validator_Semantic_IntegerConstantValueOutOfRange() |
||
828 | ) |
||
829 | ]; |
||
830 | return false; |
||
831 | } |
||
832 | |||
833 | $discoveredErrors = []; |
||
834 | return true; |
||
835 | } |
||
836 | |||
837 | private static function tryAssertBinaryConstantAsType( |
||
838 | IBinaryConstantExpression $expression, |
||
839 | ITypeReference $type, |
||
840 | &$discoveredErrors |
||
841 | ): bool { |
||
842 | if (!$type->isBinary()) { |
||
843 | $discoveredErrors = [ |
||
844 | new EdmError( |
||
845 | $expression->location(), |
||
846 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
847 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType() |
||
848 | ) |
||
849 | ]; |
||
850 | return false; |
||
851 | } |
||
852 | |||
853 | EdmUtil::checkArgumentNull($expression->getValue(), 'expression->getValue'); |
||
854 | $binaryType = $type->asBinary(); |
||
855 | if (null !== $binaryType->getMaxLength() && count($expression->getValue()) > $binaryType->getMaxLength()) { |
||
856 | $discoveredErrors = [ |
||
857 | new EdmError( |
||
858 | $expression->location(), |
||
859 | EdmErrorCode::BinaryConstantLengthOutOfRange(), |
||
860 | StringConst::EdmModel_Validator_Semantic_BinaryConstantLengthOutOfRange( |
||
861 | implode('', $expression->getValue()), |
||
862 | $binaryType->getMaxLength() |
||
863 | ) |
||
864 | ) |
||
865 | ]; |
||
866 | return false; |
||
867 | } |
||
868 | |||
869 | $discoveredErrors = []; |
||
870 | return true; |
||
871 | } |
||
872 | |||
873 | private static function testTypeReferenceMatch( |
||
874 | ITypeReference $expressionType, |
||
875 | ITypeReference $assertedType, |
||
876 | ?ILocation $location, |
||
877 | bool $matchExactly, |
||
878 | &$discoveredErrors |
||
879 | ): bool { |
||
880 | if (!self::testNullabilityMatch($expressionType, $assertedType, $location, $discoveredErrors)) { |
||
881 | return false; |
||
882 | } |
||
883 | |||
884 | // A bad type reference matches anything (so as to avoid generating spurious errors). |
||
885 | if (0 !== count($expressionType->getErrors())) { |
||
886 | $discoveredErrors = []; |
||
887 | return true; |
||
888 | } |
||
889 | |||
890 | EdmUtil::checkArgumentNull($expressionType->getDefinition(), 'expressionType->getDefinition'); |
||
891 | EdmUtil::checkArgumentNull($assertedType->getDefinition(), 'assertedType->getDefinition'); |
||
892 | |||
893 | return self::testTypeMatch( |
||
894 | $expressionType->getDefinition(), |
||
895 | $assertedType->getDefinition(), |
||
896 | $location, |
||
897 | $matchExactly, |
||
898 | $discoveredErrors |
||
899 | ); |
||
900 | } |
||
901 | |||
902 | private static function testTypeMatch( |
||
903 | IType $expressionType, |
||
904 | IType $assertedType, |
||
905 | ?ILocation $location, |
||
906 | bool $matchExactly, |
||
907 | &$discoveredErrors |
||
908 | ): bool { |
||
909 | if ($matchExactly) { |
||
910 | if (!EdmElementComparer::isEquivalentTo($expressionType, $assertedType)) { |
||
911 | $discoveredErrors = [ |
||
912 | new EdmError( |
||
913 | $location, |
||
914 | EdmErrorCode::ExpressionNotValidForTheAssertedType(), |
||
915 | StringConst::EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType() |
||
916 | ) |
||
917 | ]; |
||
918 | return false; |
||
919 | } |
||
920 | } else { |
||
921 | // A bad type matches anything (so as to avoid generating spurious errors). |
||
922 | if ($expressionType->getTypeKind()->isNone() || 0 !== count($expressionType->getErrors())) { |
||
923 | $discoveredErrors = []; |
||
924 | return true; |
||
925 | } |
||
926 | |||
927 | if ($expressionType->getTypeKind()->isPrimitive() && $assertedType->getTypeKind()->isPrimitive()) { |
||
928 | $primitiveExpressionType = $expressionType; |
||
929 | $primitiveAssertedType = $assertedType ; |
||
930 | assert($primitiveExpressionType instanceof IPrimitiveType); |
||
931 | assert($primitiveAssertedType instanceof IPrimitiveType); |
||
932 | if (!self::promotesTo( |
||
933 | $primitiveExpressionType->getPrimitiveKind(), |
||
934 | $primitiveAssertedType->getPrimitiveKind() |
||
935 | )) { |
||
936 | $discoveredErrors = [ |
||
937 | new EdmError( |
||
938 | $location, |
||
939 | EdmErrorCode::ExpressionPrimitiveKindNotValidForAssertedType(), |
||
940 | StringConst::EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType( |
||
941 | ToTraceString::toTraceString( |
||
942 | $expressionType |
||
943 | ), |
||
944 | ToTraceString::toTraceString( |
||
945 | $assertedType |
||
946 | ) |
||
947 | ) |
||
948 | ) |
||
949 | ]; |
||
950 | return false; |
||
951 | } |
||
952 | } else { |
||
953 | assert($expressionType instanceof IType); |
||
954 | if (!$expressionType->isOrInheritsFrom($assertedType)) { |
||
955 | $discoveredErrors = [ |
||
956 | new EdmError( |
||
957 | $location, |
||
958 | EdmErrorCode::ExpressionNotValidForTheAssertedType(), |
||
959 | StringConst::EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType() |
||
960 | ) |
||
961 | ]; |
||
962 | return false; |
||
963 | } |
||
964 | } |
||
965 | } |
||
966 | |||
967 | $discoveredErrors = []; |
||
968 | return true; |
||
969 | } |
||
970 | |||
971 | private static function testNullabilityMatch( |
||
992 | } |
||
993 | |||
994 | private static function promotesTo(PrimitiveTypeKind $startingKind, PrimitiveTypeKind $target): bool |
||
1002 | ); |
||
1003 | } |
||
1004 | } |
||
1005 |