Skip to content

Commit 78256a1

Browse files
committed
Some fixes, code coverage improvements
1 parent 97e37d2 commit 78256a1

14 files changed

+311
-73
lines changed

.scrutinizer.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
filter:
2+
excluded_paths:
3+
- 'tests/*'
4+
checks:
5+
php:
6+
code_rating: true
7+
variable_existence: true
8+
useless_calls: true
9+
use_statement_alias_conflict: true
10+
unused_variables: true
11+
unused_properties: true
12+
unused_parameters: true
13+
unused_methods: true
14+
unreachable_code: true
15+
sql_injection_vulnerabilities: true
16+
security_vulnerabilities: true
17+
precedence_mistakes: true
18+
precedence_in_conditions: true
19+
parameter_non_unique: true
20+
no_property_on_interface: true
21+
no_non_implemented_abstract_methods: true
22+
deprecated_code_usage: true
23+
closure_use_not_conflicting: true
24+
closure_use_modifiable: true
25+
avoid_useless_overridden_methods: true
26+
avoid_conflicting_incrementers: true
27+
assignment_of_null_return: true
28+
verify_property_names: true
29+
verify_argument_usable_as_reference: true
30+
verify_access_scope_valid: true
31+
use_self_instead_of_fqcn: true
32+
too_many_arguments: true
33+
single_namespace_per_use: true
34+
return_doc_comment_if_not_inferrable: true
35+
return_doc_comments: true
36+
require_scope_for_methods: true
37+
require_scope_for_properties: true
38+
require_php_tag_first: true
39+
require_braces_around_control_structures: true
40+
psr2_control_structure_declaration: true
41+
psr2_switch_declaration: true
42+
psr2_class_declaration: true
43+
no_eval: true
44+
no_else_if_statements: true
45+
avoid_corrupting_byteorder_marks: true
46+
argument_type_checks: true
47+
php5_style_constructor: true
48+
parameter_doc_comments: true
49+
no_duplicate_arguments: true
50+
missing_arguments: true
51+
instanceof_class_exists: true
52+
foreach_traversable: true
53+
no_unnecessary_function_call_in_for_loop: true

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ php:
55
- 5.5
66
- 5.6
77
script:
8-
- phpunit --configuration tests/phpunit.xml
8+
- phpunit --configuration tests/phpunit.xml --coverage-clover=coverage.clover
9+
after_script:
10+
- wget https://scrutinizer-ci.com/ocular.phar
11+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

src/Exceptions/FileNotWriteableException.php renamed to src/Exceptions/FileNotWritableException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Geekwright\Po\Exceptions;
44

55
/**
6-
* FileNotWriteableException
6+
* FileNotWritableException
77
*
88
* File could not be written
99
*
@@ -14,6 +14,6 @@
1414
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
1515
* @link https://github.com/geekwright/Po
1616
*/
17-
class FileNotWriteableException extends \Exception
17+
class FileNotWritableException extends \Exception
1818
{
1919
}

src/PoFile.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Geekwright\Po\Exceptions\UnrecognizedInputException;
66
use Geekwright\Po\Exceptions\FileNotReadableException;
7-
use Geekwright\Po\Exceptions\FileNotWriteableException;
7+
use Geekwright\Po\Exceptions\FileNotWritableException;
88

99
/**
1010
* PoFile - represent all entries in a GNU gettext style PO or POT file as a
@@ -292,13 +292,17 @@ public function removeEntry(PoEntry $entry)
292292
*
293293
* @return void
294294
*
295-
* @throws FileNotWriteableException
295+
* @throws FileNotWritableException
296296
*/
297297
public function writePoFile($file)
298298
{
299299
$source = $this->dumpString();
300-
if (false===file_put_contents($file, $source)) {
301-
throw new FileNotWriteableException($file);
300+
$status = is_writable($file);
301+
if ($status === true) {
302+
$status = file_put_contents($file, $source);
303+
}
304+
if (false === $status) {
305+
throw new FileNotWritableException($file);
302306
}
303307
}
304308

src/PoInitAbstract.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ public function addPgettextTags($tags)
172172
*/
173173
public function msginitFile($filename)
174174
{
175-
$source = file_get_contents($filename);
175+
if (!is_readable($filename)) {
176+
$source = false;
177+
} else {
178+
$source = file_get_contents($filename);
179+
}
176180
if (false===$source) {
177181
throw new FileNotReadableException($filename);
178182
}
@@ -197,7 +201,7 @@ abstract public function msginitString($source, $refname);
197201
*
198202
* @return string
199203
*/
200-
protected function escapeForPo($string)
204+
public function escapeForPo($string)
201205
{
202206
if ($string[0]=='"' || $string[0]=="'") {
203207
$string = substr($string, 1, -1);

tests/Exceptions/FileNotWriteableExceptionTest.php renamed to tests/Exceptions/FileNotWritableExceptionTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
/**
55
* Generated by PHPUnit_SkeletonGenerator on 2015-02-18 at 00:42:43.
66
*/
7-
class FileNotWriteableExceptionTest extends \PHPUnit_Framework_TestCase
7+
class FileNotWritableExceptionTest extends \PHPUnit_Framework_TestCase
88
{
99
/**
10-
* @var FileNotWriteableException
10+
* @var FileNotWritableException
1111
*/
1212
protected $object;
1313

@@ -17,7 +17,7 @@ class FileNotWriteableExceptionTest extends \PHPUnit_Framework_TestCase
1717
*/
1818
protected function setUp()
1919
{
20-
$this->object = new FileNotWriteableException;
20+
$this->object = new FileNotWritableException;
2121
}
2222

2323
/**
@@ -30,8 +30,7 @@ protected function tearDown()
3030

3131
public function testException()
3232
{
33-
$this->setExpectedException('Geekwright\Po\Exceptions\FileNotWriteableException');
33+
$this->setExpectedException('Geekwright\Po\Exceptions\FileNotWritableException');
3434
throw $this->object;
3535
}
36-
3736
}

tests/PoEntryTest.php

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function tearDown()
2929
}
3030

3131
/**
32+
* @covers Geekwright\Po\PoEntry::__construct
3233
* @covers Geekwright\Po\PoEntry::add
3334
* @covers Geekwright\Po\PoEntry::set
3435
* @covers Geekwright\Po\PoEntry::get
@@ -49,6 +50,11 @@ public function testAddGetSet()
4950
$expected = array($value, $value2);
5051
$actual = $entry->get(PoTokens::TRANSLATOR_COMMENTS);
5152
$this->assertEquals($expected, $actual);
53+
54+
$entry->add(PoTokens::REFERENCE, 'ref');
55+
$actual = $entry->get(PoTokens::REFERENCE);
56+
$expected = array('ref');
57+
$this->assertEquals($expected, $actual);
5258
}
5359

5460
/**
@@ -57,11 +63,11 @@ public function testAddGetSet()
5763
*/
5864
public function testAddQuoted()
5965
{
60-
$this->object->set(PoTokens:: MESSAGE, null);
61-
$this->object->addQuoted(PoTokens:: MESSAGE, '""');
62-
$this->object->addQuoted(PoTokens:: MESSAGE, '"First\n"');
63-
$this->object->addQuoted(PoTokens:: MESSAGE, '"Second\n"');
64-
$actual = $this->object->getAsString(PoTokens:: MESSAGE);
66+
$this->object->set(PoTokens::MESSAGE, null);
67+
$this->object->addQuoted(PoTokens::MESSAGE, '""');
68+
$this->object->addQuoted(PoTokens::MESSAGE, '"First\n"');
69+
$this->object->addQuoted(PoTokens::MESSAGE, '"Second\n"');
70+
$actual = $this->object->getAsString(PoTokens::MESSAGE);
6571
$this->assertEquals("First\nSecond\n", $actual);
6672
}
6773

@@ -71,27 +77,60 @@ public function testAddQuoted()
7177
*/
7278
public function testAddQuotedAtPosition()
7379
{
74-
$this->object->set(PoTokens:: TRANSLATED, null);
75-
$this->object->addQuotedAtPosition(PoTokens:: TRANSLATED, 1, '""');
76-
$this->object->addQuotedAtPosition(PoTokens:: TRANSLATED, 1, '"First\n"');
77-
$this->object->addQuotedAtPosition(PoTokens:: TRANSLATED, 1, '"Second\n"');
78-
$actual = $this->object->getAsStringArray(PoTokens:: TRANSLATED);
80+
$this->object->set(PoTokens::TRANSLATED, null);
81+
$this->object->addQuotedAtPosition(PoTokens::TRANSLATED, 1, '""');
82+
$this->object->addQuotedAtPosition(PoTokens::TRANSLATED, 1, '"First\n"');
83+
$this->object->addQuotedAtPosition(PoTokens::TRANSLATED, 1, '"Second\n"');
84+
$actual = $this->object->getAsStringArray(PoTokens::TRANSLATED);
7985
$this->assertEquals(array(1=>"First\nSecond\n"), $actual);
86+
87+
$this->object->set(PoTokens::TRANSLATED, null);
88+
$this->object->set(PoTokens::TRANSLATED, array("First\n"));
89+
$this->object->addQuotedAtPosition(PoTokens::TRANSLATED, 0, '"Second\n"');
90+
$actual = $this->object->getAsStringArray(PoTokens::TRANSLATED);
91+
$this->assertEquals(array(0=>"First\nSecond\n"), $actual);
8092
}
8193

8294
/**
8395
* @covers Geekwright\Po\PoEntry::dumpEntry
96+
* @covers Geekwright\Po\PoEntry::dumpEntryComments
97+
* @covers Geekwright\Po\PoEntry::formatQuotedString
8498
*/
8599
public function testDumpEntry()
86100
{
87101
$entry = new PoEntry;
88-
$entry->set(PoTokens:: MESSAGE, 'Hello.');
89-
$entry->set(PoTokens:: TRANSLATED, 'Bonjour!');
90-
$entry->set(PoTokens:: TRANSLATOR_COMMENTS, 'Just saying hello');
102+
$entry->set(PoTokens::MESSAGE, 'Hello.');
103+
$entry->set(PoTokens::TRANSLATED, 'Bonjour!');
104+
$entry->set(PoTokens::TRANSLATOR_COMMENTS, 'Just saying');
105+
$entry->add(PoTokens::TRANSLATOR_COMMENTS, 'hello');
106+
$entry->set(PoTokens::REFERENCE, 'ref');
107+
108+
$actual = $entry->dumpEntry();
109+
110+
$expected = "# Just saying\n# hello\n#: ref\nmsgid \"Hello.\"\nmsgstr \"Bonjour!\"\n\n";
111+
$this->assertEquals($expected, $actual);
112+
113+
$entry = new PoEntry;
114+
$entry->set(PoTokens::MESSAGE, '');
115+
$entry->add(PoTokens::MESSAGE, 'Hello.');
116+
$entry->set(PoTokens::TRANSLATED, '');
117+
$entry->add(PoTokens::TRANSLATED, 'Bonjour!');
118+
$entry->add(PoTokens::CONTEXT, 'context');
119+
120+
$actual = $entry->dumpEntry();
121+
122+
$expected = "msgctxt \"context\"\nmsgid \"\"\n\"Hello.\"\nmsgstr \"\"\n\"Bonjour!\"\n\n";
123+
$this->assertEquals($expected, $actual);
124+
125+
$entry = new PoEntry;
126+
$entry->add(PoTokens::MESSAGE, 'One');
127+
$entry->add(PoTokens::PLURAL, 'Several');
128+
$entry->addQuotedAtPosition(PoTokens::TRANSLATED, 0, 'Onewa');
129+
$entry->addQuotedAtPosition(PoTokens::TRANSLATED, 1, 'Everalsa');
91130

92131
$actual = $entry->dumpEntry();
93132

94-
$expected = "# Just saying hello\nmsgid \"Hello.\"\nmsgstr \"Bonjour!\"\n\n";
133+
$expected = "msgid \"One\"\nmsgid_plural \"Several\"\nmsgstr[0] \"Onewa\"\nmsgstr[1] \"Everalsa\"\n\n";
95134
$this->assertEquals($expected, $actual);
96135
}
97136

@@ -101,19 +140,28 @@ public function testDumpEntry()
101140
*/
102141
public function testHasFlag()
103142
{
104-
$this->object->set(PoTokens:: FLAG, null);
105-
$this->object->set(PoTokens:: FLAG, 'fuzzy');
143+
$this->object->set(PoTokens::FLAG, null);
144+
$this->assertFalse($this->object->hasFlag('fuzzy'));
145+
$this->object->set(PoTokens::FLAG, 'fuzzy');
106146

107147
$this->assertTrue($this->object->hasFlag('fuzzy'));
108148
$this->assertFalse($this->object->hasFlag('futzy'));
109149

110-
$this->object->set(PoTokens:: FLAG, null);
111-
$this->object->set(PoTokens:: FLAG, ' php-format , fuzzy, OdD-StUfF');
150+
$this->object->set(PoTokens::FLAG, null);
151+
$this->assertFalse($this->object->hasFlag('fuzzy'));
152+
$this->object->addFlag('fuzzy');
153+
$this->assertTrue($this->object->hasFlag('fuzzy'));
154+
$this->assertFalse($this->object->hasFlag('futzy'));
155+
$this->object->addFlag('futzy');
156+
$this->assertTrue($this->object->hasFlag('futzy'));
157+
158+
$this->object->set(PoTokens::FLAG, null);
159+
$this->object->set(PoTokens::FLAG, ' php-format , fuzzy, OdD-StUfF');
112160
$this->assertTrue($this->object->hasFlag('php-format'));
113161
$this->assertTrue($this->object->hasFlag('fuzzy'));
114162
$this->assertTrue($this->object->hasFlag('odd-stuff'));
115163
$this->assertFalse($this->object->hasFlag('futzy'));
116-
$this->object->add(PoTokens:: FLAG, 'separate');
164+
$this->object->add(PoTokens::FLAG, 'separate');
117165
$this->assertTrue($this->object->hasFlag('separate'));
118166
$this->object->addFlag('futzy');
119167
$this->assertTrue($this->object->hasFlag('fuzzy'));

0 commit comments

Comments
 (0)