use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project yii2support by nvlad.
the class PhpUtil method getArrayKeys.
@NotNull
public static Collection<String> getArrayKeys(ArrayCreationExpression array) {
final HashSet<String> result = new HashSet<>();
Iterable<ArrayHashElement> items = array.getHashElements();
for (ArrayHashElement item : items) {
if (item.getKey() != null && item.getKey() instanceof StringLiteralExpression) {
result.add(((StringLiteralExpression) item.getKey()).getContents());
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project phpinspectionsea by kalessil.
the class PackedHashtableOptimizationInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
/* TODO: docs, http://blog.jpauli.tech/2016/04/08/hashtables.html#packed-hashtable-optimization */
@Override
public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
/* requires PHP7 */
if (PhpLanguageLevel.get(holder.getProject()).below(PhpLanguageLevel.PHP700)) {
return;
}
/* requires at least 3 children - let array to grow enough */
final PsiElement[] children = expression.getChildren();
if (children.length < 3) {
return;
}
/* false-positives: test classes */
if (this.isTestContext(expression)) {
return;
}
/* step 1: collect indexes and verify array structure */
final List<PhpPsiElement> indexes = new ArrayList<>();
for (final PsiElement pairCandidate : children) {
if (pairCandidate instanceof ArrayHashElement) {
final PhpPsiElement key = ((ArrayHashElement) pairCandidate).getKey();
if ((key instanceof StringLiteralExpression && key.getFirstPsiChild() == null) || OpenapiTypesUtil.isNumber(key)) {
indexes.add(key);
continue;
}
}
break;
}
if (indexes.size() != children.length) {
indexes.clear();
return;
}
/* step 2: analyze collected indexes */
// if string literal is not numeric => stop
boolean hasStringIndexes = false;
boolean hasIncreasingIndexes = true;
int lastIndex = Integer.MIN_VALUE;
for (PhpPsiElement index : indexes) {
final String numericIndex;
final int integerIndex;
/* extract text representation of the index */
if (index instanceof StringLiteralExpression) {
hasStringIndexes = true;
numericIndex = ((StringLiteralExpression) index).getContents();
/* '01' and etc cases can not be converted */
if (numericIndex.length() > 1 && '0' == numericIndex.charAt(0)) {
indexes.clear();
return;
}
} else {
numericIndex = index.getText().replaceAll("\\s+", "");
}
/* try converting into integer */
try {
integerIndex = Integer.parseInt(numericIndex);
} catch (NumberFormatException error) {
indexes.clear();
return;
}
if (integerIndex < lastIndex) {
hasIncreasingIndexes = false;
}
lastIndex = integerIndex;
}
/* report if criteria are met */
if (!hasIncreasingIndexes) {
holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageReorder));
}
if (hasIncreasingIndexes && hasStringIndexes) {
holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageUseNumericKeys));
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project phpinspectionsea by kalessil.
the class DuplicateArrayKeysInspector method buildVisitor.
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
final Map<String, PsiElement> processed = new HashMap<>();
for (final ArrayHashElement pair : expression.getHashElements()) {
final PhpPsiElement key = pair.getKey();
if (key instanceof StringLiteralExpression && key.getFirstPsiChild() == null) {
final PsiElement value = pair.getValue();
if (value != null) {
final String literal = ((StringLiteralExpression) key).getContents();
if (processed.containsKey(literal)) {
final boolean isPairDuplicated = !(value instanceof ArrayCreationExpression) && OpenapiEquivalenceUtil.areEqual(value, processed.get(literal));
if (isPairDuplicated) {
holder.registerProblem(pair, MessagesPresentationUtil.prefixWithEa(messageDuplicatePair), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
} else {
holder.registerProblem(key, MessagesPresentationUtil.prefixWithEa(messageDuplicateKey));
}
}
processed.put(literal, value);
}
}
}
processed.clear();
}
};
}
Aggregations