apache / apache/netbeans

[PHP] Autovivification on value other than undefined, null or array must produce error

Open
#5,070 0 comments 0 reactions 0 assignees View on GitHub
kind:feature PHP
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 16

### What happened

Autovivification is automatic creation of new array as required every time a value is dereferenced.

In autovivification context, PHP 7.x and 8.x treat values in different ways:
- undefined and null are always produce new array
- other values produce error or warning

Reference: [Deprecate autovivification on false](https://wiki.php.net/rfc/autovivification_false)

At the moment, Netbeans does not produce warning/error message on any value

### How to reproduce

```php
propObject = new stdClass;
$this->propCallable = [self::class, "statFunc"];
}
}

function returnArray(): array {
return [];
}

function returnNull(): null{
return null;
}

function returnInt(): int {
return 10;
}

function returnFloat(): float{
return 20.34;
}

function returnBool(): bool {
return false;
}

function returnString(): bool {
return false;
}

function returnObject(): object {
return new MyClass;
}

function returnClosure(): closure {
return fn($a) => $a;
}

function returnCallable(): callable {
return [MyClass::class, "statFunc"];
}

$myInst = new MyClass;

// --- array value ------------------------------------------------------------
$var1a = [];
$var1a[] = 100; // OK

$var1b = returnArray();
$var1b[] = 100; // OK

$var1c = $myInst->propArray;
$var1c[] = 100; // OK

$closure1a = function() use ($var1a) {
$var1a[] = 100; // OK
};

$closure1b = fn() => $var1a[] = 100; // OK

// --- null value -------------------------------------------------------------
$var2a = null;
$var2a[] = 100; // OK

$var2b = returnNull();
$var2b[] = 100; // OK

$var2c = $myInst->propNull;
$var2c[] = 100; // OK

$closure2a = function() use ($var2a) {
$var2a[] = 100; // OK
};

$closure2b = fn() => $var2a[] = 100; // OK

// --- undefined variable -----------------------------------------------------
$var3[] = 100; // OK

// --- boolean value ----------------------------------------------------------
$var4a = false;
$var4a[] = 100; // this statement must display error

$var4b = returnBool();
$var4b[] = 100; // this statement must display error

$var4c = $myInst->propBool;
$var4c[] = 100; // this statement must display error

$closure4a = function() use ($var4a) {
$var4a[] = 100; // this statement must display error
};

$closure4b = fn() => $var4a[] = 100; // this statement must display error

// --- int value --------------------------------------------------------------
$var5a = 10;
$var5a[] = 100; // this statement must display error

$var5b = returnInt();
$var5b[] = 100; // this statement must display error

$var5c = $myInst->propInt;
$var5c[] = 100; // this statement must display error

$closure5a = function() use ($var5a) {
$var5a[] = 100; // this statement must display error
};

$closure5b = fn() => $var5a[] = 100; // this statement must display error

// --- float value ------------------------------------------------------------
$var6a = 20.34;
$var6a[] = 100; // this statement must display error

$var6b = returnFloat();
$var6b[] = 100; // this statement must display error

$var6c = $myInst->propFloat;
$var6c[] = 100; // this statement must display error

$closure6a = function() use ($var6a) {
$var6a[] = 100; // this statement must display error
};

$closure6b = fn() => $var6a[] = 100; // this statement must display error

// --- string value -----------------------------------------------------------
$var7a = "Hello";
$var7a[] = 100; // this statement must display error

$var7b = returnString();
$var7b[] = 100; // this statement must display error

$var7c = $myInst->propString;
$var7c[] = 100; // this statement must display error

$closure7a = function() use ($var7a) {
$var7a[] = 100; // this statement must display error
};

$closure7b = fn() => $var7a[] = 100; // this statement must display error

// --- object value -----------------------------------------------------------
$var8a = new MyClass;
$var8a[] = 100; // this statement must display error

$var8b = returnObject();
$var8b[] = 100; // this statement must display error

$var8c = $myInst->propObject;
$var8c[] = 100; // this statement must display error

$closure8a = function() use ($var8a) {
$var8a[] = 100; // this statement must display error
};

$closure8b = fn() => $var8a[] = 100; // this statement must display error

// --- closure value ----------------------------------------------------------
$var9a = fn($a) => $a*10;
$var9a[] = 100; // this statement must display error

$var9b = returnClosure();
$var9b[] = 100; // this statement must display error

$var9c = $myInst->propClosure;
$var9c[] = 100; // this statement must display error

$closure9a = function() use ($var9a) {
$var9a[] = 100; // this statement must display error
};

$closure9b = fn() => $var9a[] = 100; // this statement must display error

// --- callable value ---------------------------------------------------------
$var10a = returnCallable();
$var10a[] = 100; // this statement must display error

$var10b = $myInst->propCallable;
$var10b[] = 100; // this statement must display error

$closure10a = function() use ($var10a) {
$var10a[] = 100; // this statement must display error
};

$closure10b = fn() => $var10a[] = 100; // this statement must display error

// ----------------------------------------------------------------------------

function test(
array $argArray,
null $argNull,
int $argInt,
float $argFloat,
bool $argBool,
string $argString,
object $argObject,
closure $argClosure,
callable $argCallable,
) {
$argArray[] = 100; // OK
$argNull[] = 100; // OK
$argInt[] = 100; // this statement must display error
$argFloat[] = 100; // this statement must display error
$argBool[] = 100; // this statement must display error
$argString[] = 100; // this statement must display error
$argObject[] = 100; // this statement must display error
$argClosure[] = 100; // this statement must display error
$argCallable[] = 100; // this statement must display error
}
```

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Windows 10

### JDK

Java: 14.0.1; Java HotSpot(TM) 64-Bit Server VM 14.0.1+7 Runtime: Java(TM) SE Runtime Environment 14.0.1+7

### Apache NetBeans packaging

Apache NetBeans provided installer

### Anything else

_No response_

### Are you willing to submit a pull request?

No

### Code of Conduct

Yes

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied PHP reproduction in NetBeans 16 and compare diagnostics for undefined, null, array, and other values in the listed variable, property, return-value, and parameter cases. Done means NetBeans reports an error or warning for autovivification on every non-undefined, non-null, non-array value while preserving the allowed cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.