Skip to content

Commit 66c73c0

Browse files
committed
Fixed exceptions in reader/writer
1 parent 421b6e6 commit 66c73c0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/PhpWord/IOFactory.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ abstract class IOFactory
3737
*/
3838
public static function createWriter(PhpWord $phpWord, $name)
3939
{
40-
try {
41-
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
42-
43-
return new $fqName($phpWord);
44-
} catch (\Exception $ex) {
45-
throw new Exception("Could not instantiate \"{$name}\" class.");
40+
if ($name !== 'IWriter' && $name !== 'ODText' && $name !== 'RTF' && $name !== 'Word2007') {
41+
throw new Exception("\"{$name}\" is not a valid writer.");
4642
}
43+
44+
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
45+
return new $fqName($phpWord);
4746
}
4847

4948
/**
@@ -53,13 +52,12 @@ public static function createWriter(PhpWord $phpWord, $name)
5352
*/
5453
public static function createReader($name)
5554
{
56-
try {
57-
$fqName = "PhpOffice\\PhpWord\\Reader\\{$name}";
58-
59-
return new $fqName();
60-
} catch (\Exception $ex) {
61-
throw new Exception("Could not instantiate \"{$name}\" class.");
55+
if ($name !== 'IReader' && $name !== 'Word2007') {
56+
throw new Exception("\"{$name}\" is not a valid reader.");
6257
}
58+
59+
$fqName = "PhpOffice\\PhpWord\\Reader\\{$name}";
60+
return new $fqName();
6361
}
6462

6563
/**

test/PhpWord/Tests/IOFactoryTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ final public function testExistingWriterCanBeCreated()
2424
/**
2525
* @covers ::createWriter
2626
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
27-
* @expectedExceptionMessage Could not instantiate "Word2006" class.
2827
*/
2928
final public function testNonexistentWriterCanNotBeCreated()
3029
{
@@ -45,7 +44,6 @@ final public function testExistingReaderCanBeCreated()
4544
/**
4645
* @covers ::createReader
4746
* @expectedException \PhpOffice\PhpWord\Exceptions\Exception
48-
* @expectedExceptionMessage Could not instantiate "Word2006" class.
4947
*/
5048
final public function testNonexistentReaderCanNotBeCreated()
5149
{

0 commit comments

Comments
 (0)