Skip to content

Commit 28dadbc

Browse files
committed
add examples.
1 parent 87f2a48 commit 28dadbc

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

test/fixtures/example/Example1.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/** ローカル変数酷使度: 131 */
4+
function createCsvLine1($values) {
5+
$result = "";
6+
$count = count($values);
7+
8+
for ($i = 0; $i < $count; $i++) {
9+
$value = $values[$i];
10+
11+
// 文字列なら " で囲む
12+
if (is_string($value)) {
13+
$formattedValue = '"' . $value . '"';
14+
} else {
15+
$formattedValue = $value;
16+
}
17+
18+
if ($i == 0) {
19+
$result .= $formattedValue;
20+
} else {
21+
$result .= "," . $formattedValue;
22+
}
23+
}
24+
25+
return $result;
26+
}

test/fixtures/example/Example2.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/** ローカル変数酷使度: 106 */
4+
function createCsvLine2($values) {
5+
$result = "";
6+
7+
foreach ($values as $value) {
8+
// 文字列なら " で囲む
9+
if (is_string($value)) {
10+
$formattedValue = '"' . $value . '"';
11+
} else {
12+
$formattedValue = $value;
13+
}
14+
15+
if (empty($result)) {
16+
$result .= $formattedValue;
17+
} else {
18+
$result .= "," . $formattedValue;
19+
}
20+
}
21+
22+
return $result;
23+
}

test/fixtures/example/Example3.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/** ローカル変数酷使度: 45 */
4+
function createCsvLine3($values) {
5+
$columns = [];
6+
7+
foreach ($values as $value) {
8+
// 文字列なら " で囲む
9+
if (is_string($value)) {
10+
$formattedValue = '"' . $value . '"';
11+
} else {
12+
$formattedValue = $value;
13+
}
14+
15+
$columns[] = $formattedValue;
16+
}
17+
18+
return implode(',', $columns);
19+
}

test/fixtures/example/Example4.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** ローカル変数酷使度: 15 */
4+
function createCsvLine4($values) {
5+
$columns = [];
6+
7+
foreach ($values as $value) {
8+
$columns[] = is_string($value) ? '"' . $value . '"' : $value;;
9+
}
10+
11+
return implode(',', $columns);
12+
}

test/fixtures/example/Example5.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/** ローカル変数酷使度: 6 + 3 = 9 */
4+
function createCsvLine5($values) {
5+
return implode(',', array_map(function ($value) {
6+
return is_string($value) ? '"' . $value . '"' : $value;
7+
}, $values));
8+
}

test/fixtures/example/ExampleTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
require_once __DIR__ . '/Example1.php';
8+
require_once __DIR__ . '/Example2.php';
9+
require_once __DIR__ . '/Example3.php';
10+
require_once __DIR__ . '/Example4.php';
11+
require_once __DIR__ . '/Example5.php';
12+
13+
class ExampleTest extends TestCase
14+
{
15+
public function testCreateCsvLine1(): void
16+
{
17+
$this->assertSame('', createCsvLine1([]));
18+
$this->assertSame('1,2,3', createCsvLine1([1, 2, 3]));
19+
$this->assertSame('1,2,"helo"', createCsvLine1([1, 2, "helo"]));
20+
}
21+
22+
public function testCreateCsvLine2(): void
23+
{
24+
$this->assertSame('', createCsvLine2([]));
25+
$this->assertSame('1,2,3', createCsvLine2([1, 2, 3]));
26+
$this->assertSame('1,2,"helo"', createCsvLine2([1, 2, "helo"]));
27+
}
28+
29+
public function testCreateCsvLine3(): void
30+
{
31+
$this->assertSame('', createCsvLine3([]));
32+
$this->assertSame('1,2,3', createCsvLine3([1, 2, 3]));
33+
$this->assertSame('1,2,"helo"', createCsvLine3([1, 2, "helo"]));
34+
}
35+
36+
public function testCreateCsvLine4(): void
37+
{
38+
$this->assertSame('', createCsvLine4([]));
39+
$this->assertSame('1,2,3', createCsvLine4([1, 2, 3]));
40+
$this->assertSame('1,2,"helo"', createCsvLine4([1, 2, "helo"]));
41+
}
42+
43+
public function testCreateCsvLine5(): void
44+
{
45+
$this->assertSame('', createCsvLine5([]));
46+
$this->assertSame('1,2,3', createCsvLine5([1, 2, 3]));
47+
$this->assertSame('1,2,"helo"', createCsvLine5([1, 2, "helo"]));
48+
}
49+
}

0 commit comments

Comments
 (0)