Skip to content

Commit 554615c

Browse files
committed
Add tests
1 parent fcf374a commit 554615c

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
* For full license information, please view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
namespace PSR2R\Test\PSR2R\Sniffs\ControlStructures;
9+
10+
use PSR2R\Sniffs\ControlStructures\UnneededElseSniff;
11+
use PSR2R\Test\TestCase;
12+
13+
class UnneededElseSniffTest extends TestCase {
14+
15+
/**
16+
* @return void
17+
*/
18+
public function testUnneededElseSniffer(): void {
19+
$this->assertSnifferFindsErrors(new UnneededElseSniff(), 6);
20+
}
21+
22+
/**
23+
* @return void
24+
*/
25+
public function testUnneededElseFixer(): void {
26+
$this->assertSnifferCanFixErrors(new UnneededElseSniff());
27+
}
28+
29+
}

tests/_data/UnneededElse/after.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PSR2R;
4+
5+
class UnneededElseExample {
6+
7+
/**
8+
* All branches have returns - should trigger on elseif and else.
9+
*
10+
* @param int $value
11+
*
12+
* @return string
13+
*/
14+
public function allBranchesHaveReturns(int $value): string {
15+
if ($value > 0) {
16+
return 'positive';
17+
}
18+
if ($value < 0) {
19+
return 'negative';
20+
}
21+
22+
return 'zero';
23+
}
24+
25+
/**
26+
* First branch has NO return - should NOT trigger.
27+
*
28+
* @param int $id
29+
* @param string $referer
30+
*
31+
* @return void
32+
*/
33+
public function firstBranchNoReturn(int $id, string $referer): void {
34+
if ($id > 0 && $this->isPosted()) {
35+
$value = $this->toggle($id);
36+
} elseif ($id > 0 && !empty($referer)) {
37+
$value = $this->toggle($id);
38+
39+
return;
40+
} else {
41+
$this->error();
42+
43+
return;
44+
}
45+
46+
$this->autoRender = false;
47+
}
48+
49+
/**
50+
* Only else needs to be removed - elseif also has no return.
51+
*
52+
* @param int $value
53+
*
54+
* @return string|null
55+
*/
56+
public function onlyElseUnneeded(int $value): ?string {
57+
if ($value > 0) {
58+
return 'positive';
59+
}
60+
if ($value === 0) {
61+
$x = 'zero';
62+
} else {
63+
return 'negative';
64+
}
65+
66+
return null;
67+
}
68+
69+
/**
70+
* Long elseif chain with all returns.
71+
*
72+
* @param int $value
73+
*
74+
* @return string
75+
*/
76+
public function longChainAllReturns(int $value): string {
77+
if ($value > 100) {
78+
return 'very positive';
79+
}
80+
if ($value > 0) {
81+
return 'positive';
82+
}
83+
if ($value < 0) {
84+
return 'negative';
85+
}
86+
87+
return 'zero';
88+
}
89+
90+
/**
91+
* @param int $id
92+
*
93+
* @return void
94+
*/
95+
private function isPosted(): void {
96+
}
97+
98+
/**
99+
* @param int $id
100+
*
101+
* @return void
102+
*/
103+
private function toggle(int $id): void {
104+
}
105+
106+
/**
107+
* @return void
108+
*/
109+
private function error(): void {
110+
}
111+
112+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PSR2R;
4+
5+
class UnneededElseExample {
6+
7+
/**
8+
* All branches have returns - should trigger on elseif and else.
9+
*
10+
* @param int $value
11+
*
12+
* @return string
13+
*/
14+
public function allBranchesHaveReturns(int $value): string {
15+
if ($value > 0) {
16+
return 'positive';
17+
} elseif ($value < 0) {
18+
return 'negative';
19+
} else {
20+
return 'zero';
21+
}
22+
}
23+
24+
/**
25+
* First branch has NO return - should NOT trigger.
26+
*
27+
* @param int $id
28+
* @param string $referer
29+
*
30+
* @return void
31+
*/
32+
public function firstBranchNoReturn(int $id, string $referer): void {
33+
if ($id > 0 && $this->isPosted()) {
34+
$value = $this->toggle($id);
35+
} elseif ($id > 0 && !empty($referer)) {
36+
$value = $this->toggle($id);
37+
38+
return;
39+
} else {
40+
$this->error();
41+
42+
return;
43+
}
44+
45+
$this->autoRender = false;
46+
}
47+
48+
/**
49+
* Only else needs to be removed - elseif also has no return.
50+
*
51+
* @param int $value
52+
*
53+
* @return string|null
54+
*/
55+
public function onlyElseUnneeded(int $value): ?string {
56+
if ($value > 0) {
57+
return 'positive';
58+
} elseif ($value === 0) {
59+
$x = 'zero';
60+
} else {
61+
return 'negative';
62+
}
63+
64+
return null;
65+
}
66+
67+
/**
68+
* Long elseif chain with all returns.
69+
*
70+
* @param int $value
71+
*
72+
* @return string
73+
*/
74+
public function longChainAllReturns(int $value): string {
75+
if ($value > 100) {
76+
return 'very positive';
77+
} elseif ($value > 0) {
78+
return 'positive';
79+
} elseif ($value < 0) {
80+
return 'negative';
81+
} else {
82+
return 'zero';
83+
}
84+
}
85+
86+
/**
87+
* @param int $id
88+
*
89+
* @return void
90+
*/
91+
private function isPosted(): void {
92+
}
93+
94+
/**
95+
* @param int $id
96+
*
97+
* @return void
98+
*/
99+
private function toggle(int $id): void {
100+
}
101+
102+
/**
103+
* @return void
104+
*/
105+
private function error(): void {
106+
}
107+
108+
}

0 commit comments

Comments
 (0)