Skip to content

Commit

Permalink
Merge pull request #32 from citrus-framework/fix_php8
Browse files Browse the repository at this point in the history
php8化対応
  • Loading branch information
take64 committed Dec 23, 2021
2 parents caf47ff + 45c90cb commit abbaaf6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
"citrus-framework/exception": "^1.0"
},
"require-dev": {
"php": "^7.3",
"phpunit/phpunit": "^8.4",
"nunomaduro/phpinsights": "^v1.13"
"php": "^8.1",
"phpunit/phpunit": "^9.5",
"nunomaduro/phpinsights": "^v2.0"
},
"autoload": {
"psr-4": {
"Citrus\\": "src/",
"Test\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
8 changes: 4 additions & 4 deletions src/Variable/Singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ trait Singleton
*/
public static function sharedInstance(): self
{
static $singleton;
if (true === is_null($singleton))
static $singletons = [];
if (false === array_key_exists(static::class, $singletons))
{
$singleton = new static();
$singletons[static::class] = new static();
}
return $singleton;
return $singletons[static::class];
}
}
43 changes: 43 additions & 0 deletions tests/Variable/SingletonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright 2020, CitrusVariables. All Rights Reserved.
* @author take64 <take64@citrus.tk>
* @license http://www.citrus.tk/
*/

namespace Test\Variable;

use Citrus\Variable\Singleton;
use PHPUnit\Framework\TestCase;

/**
* シングルトンのテスト
*/
class SingletonTest extends TestCase
{
/**
* @test
*/
public function sharedInstance_想定通り()
{
// php8以降、メソッド内静的変数が共通領域になってしまった
$this->assertInstanceOf(TestA::class, TestA::sharedInstance());
$this->assertInstanceOf(TestB::class, TestB::sharedInstance());
}
}

class TestP
{
use Singleton;
}

class TestA extends TestP
{
}

class TestB extends TestP
{
}

0 comments on commit abbaaf6

Please sign in to comment.