Skip to content

Commit 7bb1d8f

Browse files
committed
Fix issues reported by PHPStan
Fixed all PHPStan issues up to level 4 and added PHPStan to CI scripts. Fixed an issue reported by PhpStorm. Added a new test-case to test a basic setup. This also tests the removal of empty elements. Fixes #27
1 parent b90c0da commit 7bb1d8f

File tree

13 files changed

+174
-101
lines changed

13 files changed

+174
-101
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ before_script:
99
- travis_retry composer install --prefer-source --no-interaction --dev
1010

1111
script:
12-
- vendor/bin/php-cs-fixer fix src --dry-run --rules=@PSR2
13-
- vendor/bin/phpspec run --no-interaction --verbose
12+
- composer run lint-ci
13+
- composer run test-ci
14+
- composer run analyse
1415

1516
after_success:
1617
- bash <(curl -s https://codecov.io/bash)

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require-dev": {
2020
"friends-of-phpspec/phpspec-code-coverage": "^5.0",
2121
"friendsofphp/php-cs-fixer": "^2.14",
22-
"phpspec/phpspec": "^6.1"
22+
"phpspec/phpspec": "^6.1",
23+
"phpstan/phpstan": "^0.12.57"
2324
},
2425
"autoload": {
2526
"psr-4": {
@@ -28,7 +29,10 @@
2829
},
2930
"scripts": {
3031
"test": "phpspec run --ansi",
31-
"lint": "php-cs-fixer fix src --rules=@PSR2"
32+
"test-ci": "phpspec run --no-interaction --verbose",
33+
"lint": "php-cs-fixer fix src --rules=@PSR2",
34+
"lint-ci": "php-cs-fixer fix src --dry-run --rules=@PSR2",
35+
"analyse": "phpstan analyse --level 4 src"
3236
},
3337
"config": {
3438
"platform": {

composer.lock

Lines changed: 61 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/XmlTv/XmlTvSpec.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace spec\XmlTv;
44

5+
use XmlTv\Exceptions\ValidationException;
56
use XmlTv\Tv;
67
use XmlTv\XmlElement;
78
use XmlTv\XmlTv;
@@ -21,7 +22,25 @@ function it_generates_an_xml_file_from_an_empty_tv_object()
2122
$this->generate(new Tv(), false)->shouldReturn($xml);
2223
}
2324

24-
function it_generates_an_xml_file()
25+
function it_generates_a_basic_xml_file()
26+
{
27+
$xml = file_get_contents(__DIR__.'/../epg-basic.xml');
28+
29+
$tv = new Tv();
30+
$channel = new Tv\Channel('test');
31+
$channel->addDisplayName(new Tv\Elements\DisplayName('foo'));
32+
$programme = new Tv\Programme('test', '1', '2');
33+
$programme->addTitle(new Tv\Elements\Title('bar'));
34+
$programme->video = new Tv\Elements\Video('yes');
35+
$programme->audio = new Tv\Elements\Audio('no');
36+
37+
$tv->addChannel($channel);
38+
$tv->addProgramme($programme);
39+
40+
$this->generate($tv, false)->shouldReturn($xml);
41+
}
42+
43+
function it_generates_a_xml_file()
2544
{
2645
$xml = file_get_contents(__DIR__.'/../epg.xml');
2746
$date = gmdate(Tv::DATE_FORMAT, 100000);
@@ -85,12 +104,19 @@ function it_generates_an_xml_file()
85104

86105
function it_throws_if_the_generated_xml_does_not_validate()
87106
{
88-
$this->shouldThrow('XmlTv\Exceptions\ValidationException')->duringGenerate(new InvalidTv(), true);
107+
$this->shouldThrow(ValidationException::class)->duringGenerate(new InvalidTv(), true);
89108
}
90109

91-
function it_triggers_a_warning_if_an_element_cannot_be_serialized()
110+
function it_throws_if_an_element_cannot_be_serialized()
92111
{
93-
$this->shouldTrigger(E_USER_WARNING)->duringGenerate(new UnserializableChild(), false);
112+
$this->shouldThrow(\TypeError::class)->duringGenerate(new UnserializableChild(), false);
113+
}
114+
115+
function it_provides_support_for_legacy_code()
116+
{
117+
$xml = '<?xml version="1.0"?>' . PHP_EOL . '<tv/>' . PHP_EOL;
118+
119+
$this->generate(new LegacyMethods(), false)->shouldReturn($xml);
94120
}
95121
}
96122

@@ -108,6 +134,15 @@ class UnserializableChild extends Tv
108134
public function xmlSerialize(): XmlElement
109135
{
110136
return (new XmlElement('tv'))
111-
->withOptionalChild(new \stdClass());
137+
->withChild(new \stdClass());
138+
}
139+
}
140+
141+
class LegacyMethods extends Tv
142+
{
143+
public function xmlSerialize(): XmlElement
144+
{
145+
return (new XmlElement('tv'))
146+
->withOptionalChild(new XmlElement('foo'));
112147
}
113148
}

spec/epg-basic.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<tv generator-info-name="jaylinski/xmltv" generator-info-url="https://github.com/jaylinski/xmltv">
3+
<channel id="test">
4+
<display-name>foo</display-name>
5+
</channel>
6+
<programme channel="test" start="1" stop="2">
7+
<title>bar</title>
8+
<video>
9+
<present>yes</present>
10+
</video>
11+
<audio>
12+
<present>no</present>
13+
</audio>
14+
</programme>
15+
</tv>

src/Tv.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public function __construct(string $date = '', string $sourceInfoUrl = '', strin
5858
$this->sourceDataUrl = $sourceDataUrl;
5959
}
6060

61-
/**
62-
* @param Channel $channel
63-
*/
6461
public function addChannel(Channel $channel): void
6562
{
6663
array_push($this->channel, $channel);
@@ -74,9 +71,6 @@ public function getChannels(): array
7471
return $this->channel;
7572
}
7673

77-
/**
78-
* @param Programme $programme
79-
*/
8074
public function addProgramme(Programme $programme): void
8175
{
8276
array_push($this->programme, $programme);

src/Tv/Channel.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class Channel implements XmlSerializable
3030
*/
3131
private $url = [];
3232

33-
/**
34-
* Channel constructor.
35-
*
36-
* @param string $id
37-
*/
3833
public function __construct(string $id)
3934
{
4035
$this->id = $id;

src/Tv/Elements/Audio.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(string $present = '', string $stereo = '')
3333
public function xmlSerialize(): XmlElement
3434
{
3535
return (new XmlElement('audio'))
36-
->withOptionalChild(new XmlElement('present', $this->present))
37-
->withOptionalChild(new XmlElement('stereo', $this->stereo));
36+
->withChild(new XmlElement('present', $this->present))
37+
->withChild(new XmlElement('stereo', $this->stereo));
3838
}
3939
}

src/Tv/Elements/Subtitles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public function xmlSerialize(): XmlElement
3131
{
3232
return (new XmlElement('subtitles'))
3333
->withAttribute('type', $this->type)
34-
->withOptionalChild($this->language);
34+
->withChild($this->language);
3535
}
3636
}

src/Tv/Elements/Video.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function __construct(string $present = '', string $colour = '', string $a
4646
public function xmlSerialize(): XmlElement
4747
{
4848
return (new XmlElement('video'))
49-
->withOptionalChild(new XmlElement('present', $this->present))
50-
->withOptionalChild(new XmlElement('colour', $this->colour))
51-
->withOptionalChild(new XmlElement('aspect', $this->aspect))
52-
->withOptionalChild(new XmlElement('quality', $this->quality));
49+
->withChild(new XmlElement('present', $this->present))
50+
->withChild(new XmlElement('colour', $this->colour))
51+
->withChild(new XmlElement('aspect', $this->aspect))
52+
->withChild(new XmlElement('quality', $this->quality));
5353
}
5454
}

0 commit comments

Comments
 (0)