Skip to content

Commit 95ad95c

Browse files
author
Dominik Liebler
committed
On to PHP7^!
1 parent a7928d0 commit 95ad95c

File tree

6 files changed

+74
-28
lines changed

6 files changed

+74
-28
lines changed

Behavioral/Observer/Tests/ObserverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testAttachDetach()
5757
public function testUpdateCalling()
5858
{
5959
$subject = new User();
60-
$observer = $this->getMock('SplObserver');
60+
$observer = $this->createMock('SplObserver');
6161
$subject->attach($observer);
6262

6363
$observer->expects($this->once())

Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,20 @@ pseudoxml:
190190
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
191191
@echo
192192
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
193+
194+
composer.phar:
195+
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
196+
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
197+
php composer-setup.php
198+
php -r "unlink('composer-setup.php');"
199+
200+
install: vendor
201+
202+
vendor: composer.phar
203+
php composer.phar install
204+
205+
cs: install
206+
./vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor .
207+
208+
test: install cs
209+
./vendor/bin/phpunit

Structural/Decorator/Tests/DecoratorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testDecoratorTypeHintedForPhp7()
7373
*/
7474
public function testDecoratorOnlyAcceptRenderer()
7575
{
76-
$mock = $this->getMock('DesignPatterns\Structural\Decorator\RendererInterface');
76+
$mock = $this->createMock('DesignPatterns\Structural\Decorator\RendererInterface');
7777
$dec = $this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array($mock));
7878
$this->assertNotNull($dec);
7979
}

Structural/Proxy/Record.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@
88
class Record
99
{
1010
/**
11-
* @var array|null
11+
* @var string[]
1212
*/
13-
protected $data;
13+
private $data;
1414

1515
/**
16-
* @param null $data
16+
* @param string[] $data
1717
*/
18-
public function __construct($data = null)
18+
public function __construct(array $data = [])
1919
{
20-
$this->data = (array) $data;
20+
$this->data = $data;
2121
}
2222

2323
/**
24-
* magic setter.
24+
* magic setter
2525
*
2626
* @param string $name
2727
* @param mixed $value
2828
*
2929
* @return void
3030
*/
31-
public function __set($name, $value)
31+
public function __set(string $name, string $value)
3232
{
33-
$this->data[(string) $name] = $value;
33+
$this->data[$name] = $value;
3434
}
3535

3636
/**
37-
* magic getter.
37+
* magic getter
3838
*
3939
* @param string $name
4040
*
41-
* @return mixed|null
41+
* @return string|null
4242
*/
43-
public function __get($name)
43+
public function __get(string $name): string
4444
{
45-
if (array_key_exists($name, $this->data)) {
46-
return $this->data[(string) $name];
45+
if (isset($this->data[$name])) {
46+
return $this->data[$name];
4747
} else {
48-
return;
48+
return null;
4949
}
5050
}
5151
}

Structural/Proxy/RecordProxy.php

+16-12
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,53 @@
22

33
namespace DesignPatterns\Structural\Proxy;
44

5-
/**
6-
* Class RecordProxy.
7-
*/
85
class RecordProxy extends Record
96
{
107
/**
118
* @var bool
129
*/
13-
protected $isDirty = false;
10+
private $isDirty = false;
1411

1512
/**
1613
* @var bool
1714
*/
18-
protected $isInitialized = false;
15+
private $isInitialized = false;
1916

2017
/**
2118
* @param array $data
2219
*/
23-
public function __construct($data)
20+
public function __construct(array $data)
2421
{
2522
parent::__construct($data);
2623

2724
// when the record has data, mark it as initialized
2825
// since Record will hold our business logic, we don't want to
2926
// implement this behaviour there, but instead in a new proxy class
3027
// that extends the Record class
31-
if (null !== $data) {
28+
if (count($data) > 0) {
3229
$this->isInitialized = true;
3330
$this->isDirty = true;
3431
}
3532
}
3633

3734
/**
38-
* magic setter.
35+
* magic setter
3936
*
4037
* @param string $name
41-
* @param mixed $value
42-
*
43-
* @return void
38+
* @param string $value
4439
*/
45-
public function __set($name, $value)
40+
public function __set(string $name, string $value)
4641
{
4742
$this->isDirty = true;
43+
4844
parent::__set($name, $value);
4945
}
46+
47+
/**
48+
* @return bool
49+
*/
50+
public function isDirty(): bool
51+
{
52+
return $this->isDirty;
53+
}
5054
}

Structural/Proxy/Tests/ProxyTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Proxy\Tests;
4+
5+
use DesignPatterns\Structural\Decorator;
6+
use DesignPatterns\Structural\Proxy\RecordProxy;
7+
8+
class ProxyTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testWillSetDirtyFlagInProxy()
11+
{
12+
$recordProxy = new RecordProxy([]);
13+
$recordProxy->foobar = 'baz';
14+
15+
$this->assertTrue($recordProxy->isDirty());
16+
}
17+
18+
public function testProxyIsInstanceOfRecord()
19+
{
20+
$recordProxy = new RecordProxy([]);
21+
$recordProxy->foobar = 'baz';
22+
23+
$this->assertInstanceOf('DesignPatterns\Structural\Proxy\Record', $recordProxy);
24+
}
25+
}

0 commit comments

Comments
 (0)