Completed
Push — master ( faa8b2...2212f7 )
by Joao
01:49
created
src/Singleton.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -4,47 +4,47 @@
 block discarded – undo
4 4
 
5 5
 trait Singleton
6 6
 {
7
-    protected function __construct()
8
-    {
9
-    }
10
-
11
-    /**
12
-     * @throws SingletonException
13
-     */
14
-    final public function __clone()
15
-    {
16
-        throw new SingletonException('You can not clone a singleton.');
17
-    }
18
-
19
-    /**
20
-     * @throws SingletonException
21
-     */
22
-    final public function __sleep()
23
-    {
24
-        throw new SingletonException('You can not serialize a singleton.');
25
-    }
26
-
27
-    /**
28
-     * @throws SingletonException
29
-     */
30
-    final public function __wakeup()
31
-    {
32
-        throw new SingletonException('You can not deserialize a singleton.');
33
-    }
34
-
35
-    /**
36
-     * @return static
37
-     */
38
-    public static function getInstance()
39
-    {
40
-        static $instances;
41
-
42
-        $calledClass = get_called_class();
43
-
44
-        if (!isset($instances[$calledClass])) {
45
-            $instances[$calledClass] = new $calledClass();
46
-        }
47
-        return $instances[$calledClass];
48
-    }
7
+	protected function __construct()
8
+	{
9
+	}
10
+
11
+	/**
12
+	 * @throws SingletonException
13
+	 */
14
+	final public function __clone()
15
+	{
16
+		throw new SingletonException('You can not clone a singleton.');
17
+	}
18
+
19
+	/**
20
+	 * @throws SingletonException
21
+	 */
22
+	final public function __sleep()
23
+	{
24
+		throw new SingletonException('You can not serialize a singleton.');
25
+	}
26
+
27
+	/**
28
+	 * @throws SingletonException
29
+	 */
30
+	final public function __wakeup()
31
+	{
32
+		throw new SingletonException('You can not deserialize a singleton.');
33
+	}
34
+
35
+	/**
36
+	 * @return static
37
+	 */
38
+	public static function getInstance()
39
+	{
40
+		static $instances;
41
+
42
+		$calledClass = get_called_class();
43
+
44
+		if (!isset($instances[$calledClass])) {
45
+			$instances[$calledClass] = new $calledClass();
46
+		}
47
+		return $instances[$calledClass];
48
+	}
49 49
 
50 50
 }
Please login to merge, or discard this patch.
tests/SingletonTest.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -6,51 +6,51 @@
 block discarded – undo
6 6
 
7 7
 class SingletonTest extends \PHPUnit\Framework\TestCase
8 8
 {
9
-    public function testSingleton()
10
-    {
11
-        $sample1 = Sample1::getInstance();
12
-        $this->assertEquals(10, $sample1->property);
13
-        $sample1->property = 20;
14
-        $this->assertEquals(20, $sample1->property);
15
-
16
-        $sample1Other = Sample1::getInstance();
17
-        $this->assertEquals(20, $sample1Other->property);
18
-        $sample1->property = 40;
19
-        $this->assertEquals(40, $sample1Other->property);
20
-        $this->assertEquals(40, $sample1->property);
21
-
22
-        //
23
-
24
-        $sample2 = Sample2::getInstance();
25
-        $sample2->property2 = 50;
26
-        $this->assertEquals(50, $sample2->property2);
27
-
28
-        $sample2Other = Sample2::getInstance();
29
-        $this->assertEquals(50, $sample2Other->property2);
30
-        $sample2->property2 = 90;
31
-        $this->assertEquals(90, $sample2Other->property2);
32
-        $this->assertEquals(90, $sample2->property2);
33
-
34
-        //
35
-
36
-        $this->assertEquals(40, $sample1->property);
37
-    }
38
-
39
-    /**
40
-     * @expectedException \ByJG\DesignPattern\SingletonException
41
-     */
42
-    public function testClone()
43
-    {
44
-        $sample1 = Sample1::getInstance();
45
-        $sample2 = clone $sample1;
46
-    }
47
-
48
-    /**
49
-     * @expectedException \ByJG\DesignPattern\SingletonException
50
-     */
51
-    public function testSerialize()
52
-    {
53
-        $sample1 = Sample1::getInstance();
54
-        $serialize = serialize($sample1);
55
-    }
9
+	public function testSingleton()
10
+	{
11
+		$sample1 = Sample1::getInstance();
12
+		$this->assertEquals(10, $sample1->property);
13
+		$sample1->property = 20;
14
+		$this->assertEquals(20, $sample1->property);
15
+
16
+		$sample1Other = Sample1::getInstance();
17
+		$this->assertEquals(20, $sample1Other->property);
18
+		$sample1->property = 40;
19
+		$this->assertEquals(40, $sample1Other->property);
20
+		$this->assertEquals(40, $sample1->property);
21
+
22
+		//
23
+
24
+		$sample2 = Sample2::getInstance();
25
+		$sample2->property2 = 50;
26
+		$this->assertEquals(50, $sample2->property2);
27
+
28
+		$sample2Other = Sample2::getInstance();
29
+		$this->assertEquals(50, $sample2Other->property2);
30
+		$sample2->property2 = 90;
31
+		$this->assertEquals(90, $sample2Other->property2);
32
+		$this->assertEquals(90, $sample2->property2);
33
+
34
+		//
35
+
36
+		$this->assertEquals(40, $sample1->property);
37
+	}
38
+
39
+	/**
40
+	 * @expectedException \ByJG\DesignPattern\SingletonException
41
+	 */
42
+	public function testClone()
43
+	{
44
+		$sample1 = Sample1::getInstance();
45
+		$sample2 = clone $sample1;
46
+	}
47
+
48
+	/**
49
+	 * @expectedException \ByJG\DesignPattern\SingletonException
50
+	 */
51
+	public function testSerialize()
52
+	{
53
+		$sample1 = Sample1::getInstance();
54
+		$serialize = serialize($sample1);
55
+	}
56 56
 }
Please login to merge, or discard this patch.