• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Camelcade / Perl5-IDEA / #525521819

12 Apr 2026 11:03AM UTC coverage: 76.189% (+0.1%) from 76.061%
#525521819

push

github

hurricup
[qodana] Suppressed a warning on the api method

14764 of 22542 branches covered (65.5%)

Branch coverage included in aggregate %.

31091 of 37644 relevant lines covered (82.59%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.71
/plugin/common/src/main/java/com/perl5/lang/perl/psi/impl/PerlImplicitVariableDeclaration.java
1
/*
2
 * Copyright 2015-2026 Alexandr Evstigneev
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.perl5.lang.perl.psi.impl;
18

19
import com.intellij.openapi.editor.Document;
20
import com.intellij.psi.*;
21
import com.intellij.psi.stubs.IStubElementType;
22
import com.intellij.util.IncorrectOperationException;
23
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlScalarValue;
24
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue;
25
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValues;
26
import com.perl5.lang.perl.psi.PerlVariable;
27
import com.perl5.lang.perl.psi.PerlVariableDeclarationElement;
28
import com.perl5.lang.perl.psi.PerlVariableNameElement;
29
import com.perl5.lang.perl.psi.PerlVisitor;
30
import com.perl5.lang.perl.psi.mixins.PerlMethodDefinitionMixin;
31
import com.perl5.lang.perl.psi.stubs.variables.PerlVariableDeclarationStub;
32
import com.perl5.lang.perl.psi.utils.PerlVariableAnnotations;
33
import com.perl5.lang.perl.psi.utils.PerlVariableType;
34
import com.perl5.lang.perl.util.PerlPackageUtilCore;
35
import org.jetbrains.annotations.NonNls;
36
import org.jetbrains.annotations.NotNull;
37
import org.jetbrains.annotations.Nullable;
38

39
import javax.swing.*;
40
import java.util.Objects;
41

42

43
public class PerlImplicitVariableDeclaration extends PerlImplicitElement
44
  implements PerlVariable, PerlVariableNameElement, PerlVariableDeclarationElement {
45
  protected final PerlVariableType myVariableType;
46
  protected final @NotNull String myVariableName;
47
  protected final @NotNull PerlValue myDeclaredValue;
48
  protected final @Nullable String myNamespaceName;
49

50
  protected final boolean myIsLexical;
51
  protected final boolean myIsLocal;
52
  protected final boolean myIsInvocant;
53

54
  protected PerlImplicitVariableDeclaration(@NotNull PsiManager manager,
55
                                            @NotNull String variableNameWithSigil,
56
                                            @Nullable String namespaceName,
57
                                            @NotNull PerlValue variableValue,
58
                                            boolean isLexical,
59
                                            boolean isLocal,
60
                                            boolean isInvocant,
61
                                            @Nullable PsiElement parent) {
62
    this(manager,
1✔
63
         computeTypeFromNameWithSigil(variableNameWithSigil),
1✔
64
         variableNameWithSigil.substring(1),
1✔
65
         namespaceName,
66
         variableValue,
67
         isLexical,
68
         isLocal,
69
         isInvocant,
70
         parent);
71
  }
1✔
72

73
  protected PerlImplicitVariableDeclaration(@NotNull PsiManager manager,
74
                                            @NotNull PerlVariableType type,
75
                                            @NotNull String variableName,
76
                                            @Nullable String namespaceName,
77
                                            @NotNull PerlValue variableValue,
78
                                            boolean isLexical,
79
                                            boolean isLocal,
80
                                            boolean isInvocant,
81
                                            @Nullable PsiElement parent) {
82
    super(manager, parent);
1✔
83
    myVariableType = type;
1✔
84
    myVariableName = variableName;
1✔
85
    myDeclaredValue = variableValue;
1✔
86
    myIsLexical = isLexical;
1✔
87
    myIsLocal = isLocal;
1✔
88
    myIsInvocant = isInvocant;
1✔
89
    myNamespaceName = namespaceName;
1✔
90
  }
1✔
91

92
  private static PerlVariableType computeTypeFromNameWithSigil(@NotNull String variableNameWithSigil) {
93
    if (variableNameWithSigil.startsWith("$")) {
1✔
94
      return PerlVariableType.SCALAR;
1✔
95
    }
96
    else if (variableNameWithSigil.startsWith("@")) {
1✔
97
      return PerlVariableType.ARRAY;
1✔
98
    }
99
    else if (variableNameWithSigil.startsWith("%")) {
1✔
100
      return PerlVariableType.HASH;
1✔
101
    }
102
    else if (variableNameWithSigil.startsWith("*")) {
1!
103
      return PerlVariableType.GLOB;
1✔
104
    }
105
    throw new RuntimeException("Incorrect variable name, should start from sigil: " + variableNameWithSigil);
×
106
  }
107

108
  @Override
109
  public void accept(@NotNull PsiElementVisitor visitor) {
110
    if (visitor instanceof PerlVisitor perlVisitor) {
1!
111
      perlVisitor.visitPerlVariableDeclarationElement(this);
1✔
112
    }
113
    else {
114
      visitor.visitElement(this);
×
115
    }
116
  }
1✔
117

118
  @Override
119
  public final @NotNull String getNamespaceName() {
120
    String explicitPackageName = this.getExplicitNamespaceName();
1✔
121
    return explicitPackageName != null ? explicitPackageName : PerlPackageUtilCore.getContextNamespaceName(this);
1!
122
  }
123

124
  @Override
125
  public @Nullable String getExplicitNamespaceName() {
126
    return myNamespaceName;
1✔
127
  }
128

129
  @Override
130
  public String toString() {
131
    return "Implicit variable: " + getVariableType().getSigil() + getCanonicalName() + '@' + getDeclaredValue();
1✔
132
  }
133

134
  @Override
135
  public @NotNull PerlValue getDeclaredValue() {
136
    return myDeclaredValue;
1!
137
  }
138

139
  @Override
140
  public @NotNull PerlVariableType getActualType() {
141
    return getVariableType();
1!
142
  }
143

144
  @Override
145
  public @Nullable PerlVariableDeclarationElement getLexicalDeclaration() {
146
    return null;
×
147
  }
148

149
  @Override
150
  public int getLineNumber() {
151
    PsiFile file = getContainingFile();
1✔
152
    if (file == null) {
1!
153
      return 0;
1✔
154
    }
155
    Document document = PsiDocumentManager.getInstance(getProject()).getCachedDocument(file);
×
156
    return document == null ? 0 : document.getLineNumber(getTextOffset()) + 1;
×
157
  }
158

159
  @Override
160
  public PerlVariableNameElement getVariableNameElement() {
161
    return this;
×
162
  }
163

164
  @Override
165
  public boolean isBuiltIn() {
166
    return false;
×
167
  }
168

169
  @Override
170
  public boolean isDeprecated() {
171
    return false;
1✔
172
  }
173

174
  public PerlVariableType getVariableType() {
175
    return myVariableType;
1✔
176
  }
177

178
  @Override
179
  public @NotNull String getVariableName() {
180
    return myVariableName;
1!
181
  }
182

183
  @Override
184
  public @NotNull PerlVariable getVariable() {
185
    return this;
1!
186
  }
187

188
  @Override
189
  public boolean isLexicalDeclaration() {
190
    return myIsLexical;
×
191
  }
192

193
  @Override
194
  public boolean isLocalDeclaration() {
195
    return myIsLocal;
×
196
  }
197

198
  @Override
199
  public boolean isGlobalDeclaration() {
200
    return !myIsLexical;
1✔
201
  }
202

203
  @Override
204
  public boolean isInvocantDeclaration() {
205
    return myIsInvocant;
×
206
  }
207

208
  @Override
209
  public @Nullable String getPresentableName() {
210
    return isLocalDeclaration() ? getName() : getCanonicalName();
×
211
  }
212

213
  @Override
214
  public @Nullable Icon getIcon(int flags) {
215
    Icon iconByType = getIconByType(getActualType());
×
216
    return iconByType == null ? super.getIcon(flags) : iconByType;
×
217
  }
218

219
  @Override
220
  public @Nullable PsiElement getNameIdentifier() {
221
    return null;
×
222
  }
223

224
  @Override
225
  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
226
    return this;
×
227
  }
228

229
  @SuppressWarnings("deprecation")
230
  @Override
231
  public @Nullable IStubElementType<?, ?> getElementType() {
232
    return null;
1✔
233
  }
234

235
  @Override
236
  public @Nullable PerlVariableDeclarationStub getStub() {
237
    return null;
1✔
238
  }
239

240
  @Override
241
  public @NotNull String getName() {
242
    return getVariableName();
1!
243
  }
244

245
  @Override
246
  public boolean isDeclaration() {
247
    return true;
×
248
  }
249

250
  @Override
251
  public @Nullable PerlVariableAnnotations getVariableAnnotations() {
252
    return null;
×
253
  }
254

255
  @Override
256
  public @NotNull PerlVariableAnnotations getLocalVariableAnnotations() {
257
    return PerlVariableAnnotations.empty();
×
258
  }
259

260
  @Override
261
  public boolean equals(Object o) {
262
    if (this == o) {
1!
263
      return true;
×
264
    }
265
    if (o == null || getClass() != o.getClass()) {
1!
266
      return false;
1✔
267
    }
268
    if (!super.equals(o)) {
×
269
      return false;
×
270
    }
271

272
    PerlImplicitVariableDeclaration that = (PerlImplicitVariableDeclaration)o;
×
273

274
    if (myIsLexical != that.myIsLexical) {
×
275
      return false;
×
276
    }
277
    if (myIsLocal != that.myIsLocal) {
×
278
      return false;
×
279
    }
280
    if (myIsInvocant != that.myIsInvocant) {
×
281
      return false;
×
282
    }
283
    if (myVariableType != that.myVariableType) {
×
284
      return false;
×
285
    }
286
    if (!myVariableName.equals(that.myVariableName)) {
×
287
      return false;
×
288
    }
289
    if (!myDeclaredValue.equals(that.myDeclaredValue)) {
×
290
      return false;
×
291
    }
292
    return Objects.equals(myNamespaceName, that.myNamespaceName);
×
293
  }
294

295
  @Override
296
  public int hashCode() {
297
    int result = super.hashCode();
1✔
298
    result = 31 * result + (myVariableType != null ? myVariableType.hashCode() : 0);
1!
299
    result = 31 * result + myVariableName.hashCode();
1✔
300
    result = 31 * result + myDeclaredValue.hashCode();
1✔
301
    result = 31 * result + (myNamespaceName != null ? myNamespaceName.hashCode() : 0);
1✔
302
    result = 31 * result + (myIsLexical ? 1 : 0);
1✔
303
    result = 31 * result + (myIsLocal ? 1 : 0);
1!
304
    result = 31 * result + (myIsInvocant ? 1 : 0);
1✔
305
    return result;
1✔
306
  }
307

308
  public static @NotNull PerlImplicitVariableDeclaration createGlobal(@NotNull PsiElement parent,
309
                                                                      @NotNull String variableName,
310
                                                                      @Nullable String variableClass
311
  ) {
312
    return create(parent, variableName, variableClass, false, false, false);
1✔
313
  }
314

315
  public static @NotNull PerlImplicitVariableDeclaration createGlobal(@NotNull PsiManager psiManager,
316
                                                                      @NotNull String variableName,
317
                                                                      @Nullable String packageName) {
318
    return create(psiManager, variableName, packageName, null, false, false, false, null);
1✔
319
  }
320

321
  public static @NotNull PerlImplicitVariableDeclaration createLexical(@NotNull PsiElement parent,
322
                                                                       @NotNull String variableName
323
  ) {
324
    return createLexical(parent, variableName, null);
1✔
325
  }
326

327
  public static @NotNull PerlImplicitVariableDeclaration createLexical(@NotNull PsiElement parent,
328
                                                                       @NotNull String variableName,
329
                                                                       @Nullable String variableClass
330
  ) {
331
    return create(parent, variableName, variableClass, true, false, false);
1✔
332
  }
333

334
  public static @NotNull PerlImplicitVariableDeclaration createInvocant(@NotNull PsiElement parent) {
335
    return new PerlImplicitVariableDeclaration(
1✔
336
      parent.getManager(),
1✔
337
      PerlVariableType.SCALAR,
338
      PerlMethodDefinitionMixin.getDefaultInvocantName().substring(1),
1✔
339
      null,
340
      PerlValues.FIRST_ARGUMENT_VALUE,
341
      true,
342
      false,
343
      true,
344
      parent
345
    );
346
  }
347

348
  private static @NotNull PerlImplicitVariableDeclaration create(@NotNull PsiElement parent,
349
                                                                 @NotNull String variableName,
350
                                                                 @Nullable String variableClass,
351
                                                                 boolean isLexical,
352
                                                                 @SuppressWarnings("SameParameterValue") boolean isLocal,
353
                                                                 @SuppressWarnings("SameParameterValue") boolean isInvocant
354
  ) {
355
    return create(parent.getManager(), variableName, null, variableClass, isLexical, isLocal, isInvocant, parent);
1✔
356
  }
357

358
  private static @NotNull PerlImplicitVariableDeclaration create(@NotNull PsiManager psiManager,
359
                                                                 @NotNull String variableNameWithSigil,
360
                                                                 @Nullable String packageName,
361
                                                                 @Nullable String variableClass,
362
                                                                 boolean isLexical,
363
                                                                 boolean isLocal,
364
                                                                 boolean isInvocant,
365
                                                                 @Nullable PsiElement parent
366
  ) {
367
    return new PerlImplicitVariableDeclaration(
1✔
368
      psiManager, variableNameWithSigil, packageName, PerlScalarValue.create(variableClass), isLexical, isLocal, isInvocant, parent);
1✔
369
  }
370
}
371

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc