Skip to content
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

Add magic __isset to classes with __get #2231

Merged
merged 6 commits into from
Sep 19, 2019
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
14 changes: 14 additions & 0 deletions system/CLI/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ public function __get(string $key)

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

/**
* Makes it simple to check our protected properties.
*
* @param string $key
*
* @return bool
*/
public function __isset(string $key): bool
{
return isset($this->$key);
}

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

/**
* show Help include (usage,arguments,description,options)
*/
Expand Down
14 changes: 14 additions & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,4 +1864,18 @@ public function __get(string $key)

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

/**
* Checker for properties existence.
*
* @param string $key
*
* @return bool
*/
public function __isset(string $key): bool
{
return property_exists($this, $key);
}

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

}
11 changes: 11 additions & 0 deletions system/Encryption/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,15 @@ public function __get($key)
return null;
}

/**
* __isset() magic, providing checking for some of our protected properties
*
* @param string $key Property name
* @return bool
*/
public function __isset($key): bool
{
return in_array($key, ['key', 'digest', 'driver', 'drivers'], true);
}

}
10 changes: 10 additions & 0 deletions system/Encryption/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ public function __get($key)
return null;
}

/**
* __isset() magic, providing checking for some of our properties
*
* @param string $key Property name
* @return bool
*/
public function __isset($key): bool
{
return in_array($key, ['cipher', 'key'], true);
}
}
16 changes: 16 additions & 0 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -1342,4 +1342,20 @@ public function __get($name)
return null;
}

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

/**
* Allow for property-type checking to any getX method...
*
* @param $name
*
* @return bool
*/
public function __isset($name): bool
{
$method = 'get' . ucfirst($name);

return method_exists($this, $method);
}

}
15 changes: 15 additions & 0 deletions system/I18n/TimeDifference.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,19 @@ public function __get($name)

return null;
}

/**
* Allow property-like checking for our calculated values.
*
* @param $name
*
* @return bool
*/
public function __isset($name)
{
$name = ucfirst(strtolower($name));
$method = "get{$name}";

return method_exists($this, $method);
}
}
25 changes: 25 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,31 @@ public function __get(string $name)
return null;
}

/**
* Checks for the existence of properties across this model, builder, and db connection.
*
* @param string $name
*
* @return bool
*/
public function __isset(string $name): bool
{
if (property_exists($this, $name))
{
return true;
}
elseif (isset($this->db->$name))
{
return true;
}
elseif (isset($this->builder()->$name))
{
return true;
}

MGatner marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

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

/**
Expand Down
16 changes: 16 additions & 0 deletions system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,22 @@ public function __get(string $key)
return null;
}

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

/**
* Magic method to check for session variables.
* Different from has() in that it will validate 'session_id' as well.
* Mostly used by internal PHP functions, users should stick to has()
*
* @param string $key Identifier of the session property to remove.
*
* @return bool
*/
public function __isset(string $key): bool
{
return isset($_SESSION[$key]) || ($key === 'session_id');
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Flash Data Methods
Expand Down
46 changes: 46 additions & 0 deletions tests/system/Commands/CommandClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace CodeIgniter\Commands;

use Config\Services;
use CodeIgniter\CLI\CommandRunner;

class BaseCommandTest extends \CIUnitTestCase
{
protected $logger;
protected $runner;

protected function setUp()
{
parent::setUp();
$this->logger = Services::logger();
$this->runner = new CommandRunner();
}

public function testMagicIssetTrue()
{
$command = new \Tests\Support\Commands\AppInfo($this->logger, $this->runner);

$this->assertTrue(isset($command->group));
}

public function testMagicIssetFalse()
{
$command = new \Tests\Support\Commands\AppInfo($this->logger, $this->runner);

$this->assertFalse(isset($command->foobar));
}

public function testMagicGet()
{
$command = new \Tests\Support\Commands\AppInfo($this->logger, $this->runner);

$this->assertEquals('demo', $command->group);
}

public function testMagicGetMissing()
{
$command = new \Tests\Support\Commands\AppInfo($this->logger, $this->runner);

$this->assertNull($command->foobar);
}
}
36 changes: 36 additions & 0 deletions tests/system/Database/BaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,40 @@ public function testStoresConnectionTimings()
$this->assertGreaterThan($start, $db->getConnectStart());
$this->assertGreaterThan(0.0, $db->getConnectDuration());
}

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

public function testMagicIssetTrue()
{
$db = new MockConnection($this->options);

$this->assertTrue(isset($db->charset));
}

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

public function testMagicIssetFalse()
{
$db = new MockConnection($this->options);

$this->assertFalse(isset($db->foobar));
}

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

public function testMagicGet()
{
$db = new MockConnection($this->options);

$this->assertEquals('utf8', $db->charset);
}

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

public function testMagicGetMissing()
{
$db = new MockConnection($this->options);

$this->assertNull($db->foobar);
}
}
67 changes: 67 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1735,4 +1735,71 @@ public function testSoftDeleteWithTableJoinsFirst()
// Just making sure it didn't throw ambiguous deleted error
$this->assertEquals(1, $results->id);
}

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

public function testMagicIssetTrue()
{
$model = new UserModel();

$this->assertTrue(isset($model->table));
}

public function testMagicIssetFalse()
{
$model = new UserModel();

$this->assertFalse(isset($model->foobar));
}

public function testMagicIssetWithNewProperty()
{
$model = new UserModel();

$model->flavor = 'chocolate';

$this->assertTrue(isset($model->flavor));
}

public function testMagicIssetFromDb()
{
$model = new UserModel();

$this->assertTrue(isset($model->DBPrefix));
}

public function testMagicIssetFromBuilder()
{
$model = new UserModel();

$this->assertTrue(isset($model->QBNoEscape));
}

public function testMagicGet()
{
$model = new UserModel();

$this->assertEquals('user', $model->table);
}

public function testMagicGetMissing()
{
$model = new UserModel();

$this->assertNull($model->foobar);
}

public function testMagicGetFromDB()
{
$model = new UserModel();

$this->assertEquals('utf8', $model->charset);
}

public function testMagicGetFromBuilder()
{
$model = new UserModel();

$this->assertIsArray($model->QBNoEscape);
}
}
27 changes: 22 additions & 5 deletions tests/system/Encryption/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ public function testKeyCreation()
$this->assertEquals(16, strlen($this->encryption->createKey(16)));
}

public function testBogusProperty()
{
$this->assertNull($this->encryption->bogus);
}

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

public function testServiceSuccess()
Expand Down Expand Up @@ -129,4 +124,26 @@ public function testServiceShared()
$this->assertEquals('anything', $encrypter->key);
}

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

public function testMagicIssetTrue()
{
$this->assertTrue(isset($this->encryption->digest));
}

public function testMagicIssetFalse()
{
$this->assertFalse(isset($this->encryption->bogus));
}

public function testMagicGet()
{
$this->assertEquals('SHA512', $this->encryption->digest);
}

public function testMagicGetMissing()
{
$this->assertNull($this->encryption->bogus);
}

}
17 changes: 17 additions & 0 deletions tests/system/I18n/TimeDifferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,21 @@ public function testGetter()
$this->assertNull($diff->nonsense);
}

public function testMagicIssetTrue()
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');

$this->assertTrue(isset($diff->days));
$this->assertFalse(isset($diff->nonsense));
}

public function testMagicIssetFalse()
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');

$this->assertFalse(isset($diff->nonsense));
}

}
Loading