Skip to content

fix: overloading behaviour #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions features/access.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ Feature: Session access
Scenario: Iterate over non-populated session
When data does not exist
Then data is not iterated
Scenario: Overload existing array
When empty array for overload exists
Then overloading using property access succeeds
And overloading using property access succeeds
Scenario: Overload non existing array
When data does not exist
Then overloading using array access fails
And overloading using property access fails
20 changes: 16 additions & 4 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Countable;
use ArrayAccess;
use RuntimeException;
use Stringable;

class Session implements ArrayAccess, Iterator, Countable
{
Expand Down Expand Up @@ -46,13 +47,17 @@ public function __construct(string $name, ?string $id = null, array $contents =
*
* @throws RuntimeException if not initialized
*/
public function __get(string $name)
public function &__get(string $name)
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
if(!isset($this->contents[$name])){
\trigger_error("Array key not found: '$name'", \E_USER_NOTICE);
return null;
}

return $this->contents[$name];
}

Expand Down Expand Up @@ -133,13 +138,20 @@ public function offsetUnset($name): void
unset($this->contents[$name]);
}

public function offsetGet($name): mixed
public function &offsetGet($name): mixed
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line

if(!isset($this->contents[$name])){
if($name === null || \is_scalar($name) || $name instanceof Stringable){
\trigger_error("Array key not found: '$name'", \E_USER_NOTICE);
}
return null;
}

return $this->contents[$name];
}

Expand Down
64 changes: 64 additions & 0 deletions tests/behavior/AccessContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function dataExists(): void
Assert::assertCount(1, $this->session);
}

/**
* @When empty array for overload exists
*/
public function emptyArrayForOverloadExists(): void
{
$this->session = new Session('foo', 'bar', ['foo' => []]);
Assert::assertTrue($this->session->isWriteable());
Assert::assertCount(1, $this->session);
}

/**
* @Then property check returns false
*/
Expand Down Expand Up @@ -199,4 +209,58 @@ public function iteratorFails(): void

Assert::assertSame(0, $counter);
}

/**
* @Then overloading using array access succeeds
*/
public function arrayOverloadSucceeds(): void
{
// @phpstan-ignore-next-line
$this->session['foo'][] = 'baz';
// @phpstan-ignore-next-line
Assert::assertSame('baz', $this->session['foo'][0]);
}

/**
* @Then overloading using property access succeeds
*/
public function objectOverloadSucceeds(): void
{
// @phpstan-ignore-next-line
$this->session->foo[] = 'baz';
// @phpstan-ignore-next-line
Assert::assertSame('baz', $this->session->foo[0]);
}

/**
* @Then overloading using array access fails
*/
public function arrayOverloadFails(): void
{
try {
$errorThrown = false;
// @phpstan-ignore-next-line
$this->session['foo'][] = 'baz';
} catch (Throwable $e) {
$errorThrown = true;
} finally {
Assert::assertTrue($errorThrown);
}
}

/**
* @Then overloading using property access fails
*/
public function objectOverloadFails(): void
{
try {
$errorThrown = false;
// @phpstan-ignore-next-line
$this->session->foo[] = 'baz';
} catch (Throwable $e) {
$errorThrown = true;
} finally {
Assert::assertTrue($errorThrown);
}
}
}