コード例 #1
0
        public override T VisitArray(BoundArrayEx x)
        {
            if (x.Access.IsNone)
            {
                // The expression is not being read. Did you mean to assign it somewhere?
                _diagnostics.Add(_routine, x.PhpSyntax, ErrorCode.WRN_ExpressionNotRead);
            }

            return(base.VisitArray(x));
        }
コード例 #2
0
ファイル: DiagnosticWalker.cs プロジェクト: gabema/peachpie
        public override T VisitArray(BoundArrayEx x)
        {
            if (x.Access.IsNone)
            {
                // The expression is not being read. Did you mean to assign it somewhere?
                _diagnostics.Add(_routine, x.PhpSyntax, ErrorCode.WRN_ExpressionNotRead);
            }

            // Check valid types and uniqueness of the keys
            HashSet <(string, long)> lazyKeyConstSet = null;             // Stores canonic string representations of the keys to check for duplicates

            for (int i = 0; i < x.Items.Length; i++)
            {
                var item = x.Items[i];
                if (item.Key == null)
                {
                    continue;
                }

                var keyTypeMask = item.Key.TypeRefMask;
                if (!keyTypeMask.IsAnyType && !keyTypeMask.IsRef)    // Disallowing 'mixed' for key type would have caused too many false positives
                {
                    var  keyTypes         = _routine.TypeRefContext.GetTypes(keyTypeMask);
                    bool allKeyTypesValid = keyTypes.Count > 0 && keyTypes.All(AnalysisFacts.IsValidKeyType);
                    if (!allKeyTypesValid)
                    {
                        string keyTypeStr = _routine.TypeRefContext.ToString(keyTypeMask);
                        _diagnostics.Add(_routine, item.Key.PhpSyntax, ErrorCode.WRN_InvalidArrayKeyType, keyTypeStr);
                    }
                }

                if (AnalysisFacts.TryGetCanonicKeyStringConstant(item.Key.ConstantValue, out var keyConst))
                {
                    if (lazyKeyConstSet == null)
                    {
                        lazyKeyConstSet = new HashSet <(string, long)>();
                    }

                    if (!lazyKeyConstSet.Add(keyConst))
                    {
                        // Duplicate array key: '{0}'
                        _diagnostics.Add(
                            _routine,
                            item.Key.PhpSyntax ?? item.Value.PhpSyntax,
                            ErrorCode.WRN_DuplicateArrayKey,
                            keyConst.Item1 ?? keyConst.Item2.ToString());
                    }
                }
            }

            return(base.VisitArray(x));
        }
コード例 #3
0
 public override object VisitArray(BoundArrayEx x)
 {
     return(x.Update(VisitImmutableArrayPairs(x.Items)));
 }
コード例 #4
0
        public static AttributeData CreateDefaultValueAttribute(this PhpCompilation compilation, IMethodSymbol method, BoundArrayEx arr)
        {
            var typeParameter   = new KeyValuePair <string, TypedConstant>("Type", new TypedConstant(compilation.CoreTypes.DefaultValueType.Symbol, TypedConstantKind.Enum, 1 /*PhpArray*/));
            var namedparameters = ImmutableArray.Create(typeParameter);

            if (arr.Items.Length != 0)
            {
                try
                {
                    var byteSymbol      = compilation.GetSpecialType(SpecialType.System_Byte);
                    var serializedValue = Encoding.UTF8.GetBytes(arr.PhpSerializeOrThrow());
                    var p = new KeyValuePair <string, TypedConstant>(
                        "SerializedValue",
                        new TypedConstant(compilation.CreateArrayTypeSymbol(byteSymbol), serializedValue.Select(compilation.CreateTypedConstant).AsImmutable()));

                    namedparameters = namedparameters.Add(p);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Cannot construct serialized parameter default value. Routine '{method.Name}', {ex.Message}.", ex);
                }
            }

            return(new SynthesizedAttributeData(
                       compilation.CoreMethods.Ctors.DefaultValueAttribute,
                       ImmutableArray <TypedConstant> .Empty, namedparameters));
        }