Skip to content

Commit

Permalink
Add return types to tests methods. Fix todos in phpDocs. Add method's…
Browse files Browse the repository at this point in the history
… descriptions for Font class. (#509)
  • Loading branch information
likemusic authored Jan 28, 2022
1 parent 41841bc commit 43ca68f
Show file tree
Hide file tree
Showing 24 changed files with 173 additions and 141 deletions.
32 changes: 32 additions & 0 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public function translateChar(string $char, bool $use_default = true)
return $use_default ? self::MISSING : $fallbackDecoded;
}

/**
* Convert unicode character code to "utf-8" encoded string.
*/
public static function uchr(int $code): string
{
if (!isset(self::$uchrCache[$code])) {
Expand All @@ -128,6 +131,9 @@ public static function uchr(int $code): string
return self::$uchrCache[$code];
}

/**
* Init internal chars translation table by ToUnicode CMap.
*/
public function loadTranslateTable(): array
{
if (null !== $this->table) {
Expand Down Expand Up @@ -236,11 +242,21 @@ public function loadTranslateTable(): array
return $this->table;
}

/**
* Set custom char translation table where:
* - key - integer character code;
* - value - "utf-8" encoded value;
*
* @return void
*/
public function setTable(array $table)
{
$this->table = $table;
}

/**
* Decode hexadecimal encoded string. If $add_braces is true result value would be wrapped by parentheses.
*/
public static function decodeHexadecimal(string $hexa, bool $add_braces = false): string
{
// Special shortcut for XML content.
Expand Down Expand Up @@ -274,6 +290,9 @@ public static function decodeHexadecimal(string $hexa, bool $add_braces = false)
return $text;
}

/**
* Decode string with octal-decoded chunks.
*/
public static function decodeOctal(string $text): string
{
$parts = preg_split('/(\\\\[0-7]{3})/s', $text, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
Expand All @@ -290,6 +309,9 @@ public static function decodeOctal(string $text): string
return $text;
}

/**
* Decode string with html entity encoded chars.
*/
public static function decodeEntities(string $text): string
{
$parts = preg_split('/(#\d{2})/s', $text, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
Expand All @@ -306,6 +328,13 @@ public static function decodeEntities(string $text): string
return $text;
}

/**
* Check if given string is Unicode text (by BOM);
* If true - decode to "utf-8" encoded string.
* Otherwise - return text as is.
*
* @todo Rename in next major release to make the name correspond to reality (for ex. decodeIfUnicode())
*/
public static function decodeUnicode(string $text): string
{
if (preg_match('/^\xFE\xFF/i', $text)) {
Expand All @@ -330,6 +359,9 @@ protected function getFontSpaceLimit(): int
return $this->config->getFontSpaceLimit();
}

/**
* Decode text by commands array.
*/
public function decodeText(array $commands): string
{
$word_position = 0;
Expand Down
20 changes: 10 additions & 10 deletions tests/Integration/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@

class DocumentTest extends TestCase
{
protected function getDocumentInstance()
protected function getDocumentInstance(): Document
{
return new Document();
}

protected function getPDFObjectInstance(Document $document, ?Header $header = null)
protected function getPDFObjectInstance(Document $document, ?Header $header = null): PDFObject
{
return new PDFObject($document, $header);
}

protected function getPageInstance(Document $document, Header $header)
protected function getPageInstance(Document $document, Header $header): PDFObject
{
return new Page($document, $header);
}

protected function getPagesInstance(Document $document, Header $header)
protected function getPagesInstance(Document $document, Header $header): PDFObject
{
return new Pages($document, $header);
}

public function testSetObjects()
public function testSetObjects(): void
{
$document = $this->getDocumentInstance();
$object = $this->getPDFObjectInstance($document);
Expand All @@ -86,7 +86,7 @@ public function testSetObjects()
$this->assertTrue($document->getObjectById(2) instanceof PDFObject);
}

public function testGetObjects()
public function testGetObjects(): void
{
$document = $this->getDocumentInstance();
$object1 = $this->getPDFObjectInstance($document);
Expand All @@ -103,7 +103,7 @@ public function testGetObjects()
$this->assertTrue($objects[2] instanceof Page);
}

public function testDictionary()
public function testDictionary(): void
{
$document = $this->getDocumentInstance();
$objects = $document->getDictionary();
Expand All @@ -121,7 +121,7 @@ public function testDictionary()
$this->assertEquals($object2, $objects['Page']['all'][2]);
}

public function testGetObjectsByType()
public function testGetObjectsByType(): void
{
$document = $this->getDocumentInstance();
$object1 = $this->getPDFObjectInstance($document);
Expand All @@ -136,7 +136,7 @@ public function testGetObjectsByType()
$this->assertTrue($objects[2] instanceof Page);
}

public function testGetPages()
public function testGetPages(): void
{
$document = $this->getDocumentInstance();

Expand Down Expand Up @@ -218,7 +218,7 @@ public function testGetPages()
$this->assertTrue($pages[2] instanceof Page);
}

public function testGetPagesMissingCatalog()
public function testGetPagesMissingCatalog(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Missing catalog.');
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/Element/ElementArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

class ElementArrayTest extends TestCase
{
public function testParse()
public function testParse(): void
{
$document = $this->getDocumentInstance();

Expand Down Expand Up @@ -96,7 +96,7 @@ public function testParse()
$this->assertEquals(10, $offset);
}

public function testGetContent()
public function testGetContent(): void
{
$val_4 = new ElementNumeric('4');
$val_2 = new ElementNumeric('2');
Expand All @@ -106,7 +106,7 @@ public function testGetContent()
$this->assertCount(2, $content);
}

public function testContains()
public function testContains(): void
{
$val_4 = new ElementNumeric('4');
$val_2 = new ElementNumeric('2');
Expand All @@ -118,7 +118,7 @@ public function testContains()
$this->assertFalse($element->contains(8));
}

public function testResolveXRef()
public function testResolveXRef(): void
{
// Document with text.
$filename = $this->rootDir.'/samples/Document1_pdfcreator_nocompressed.pdf';
Expand All @@ -134,7 +134,7 @@ public function testResolveXRef()
$this->assertTrue(reset($pages) instanceof Page);
}

public function testGetDetails()
public function testGetDetails(): void
{
$document = $this->getDocumentInstance();
$content = '<</Type/Page/Types[8]/Sizes[1 2 3 4 5 <</Subtype/XObject>> [8 [9 <</FontSize 10>>]]]>>';
Expand Down Expand Up @@ -170,7 +170,7 @@ public function testGetDetails()
$this->assertEquals($details_reference, $details);
}

public function testToString()
public function testToString(): void
{
$val_4 = new ElementNumeric('4');
$val_2 = new ElementNumeric('2');
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Element/ElementBooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

class ElementBooleanTest extends TestCase
{
public function testParse()
public function testParse(): void
{
// Skipped.
$offset = 0;
Expand Down Expand Up @@ -102,7 +102,7 @@ public function testParse()
$this->assertEquals(7, $offset);
}

public function testGetContent()
public function testGetContent(): void
{
$element = new ElementBoolean('true');
$this->assertTrue($element->getContent());
Expand All @@ -111,7 +111,7 @@ public function testGetContent()
$this->assertFalse($element->getContent());
}

public function testEquals()
public function testEquals(): void
{
$element = new ElementBoolean('true');
$this->assertTrue($element->equals(true));
Expand All @@ -126,15 +126,15 @@ public function testEquals()
$this->assertFalse($element->equals(null));
}

public function testContains()
public function testContains(): void
{
$element = new ElementBoolean('true');
$this->assertTrue($element->contains(true));
$this->assertFalse($element->contains(false));
$this->assertFalse($element->contains(1));
}

public function testToString()
public function testToString(): void
{
$element = new ElementBoolean('true');
$this->assertEquals('true', (string) $element);
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/Element/ElementDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

class ElementDateTest extends TestCase
{
public function testParse()
public function testParse(): void
{
// Skipped.
$offset = 0;
Expand Down Expand Up @@ -136,21 +136,21 @@ public function testParse()
$this->assertEquals(0, $offset);
}

public function testGetContent()
public function testGetContent(): void
{
$element = new ElementDate(new DateTime('2013-09-01 23:55:55+02:00'));
$this->assertEquals(new DateTime('2013-09-01 21:55:55+00:00'), $element->getContent());
}

public function testGetContentInvalidParameter()
public function testGetContentInvalidParameter(): void
{
$this->expectException(Exception::class);

$element = new ElementDate('2013-09-01 23:55:55+02:00');
$this->assertEquals(new DateTime('2013-09-01 21:55:55+02:00'), $element->getContent());
}

public function testEquals()
public function testEquals(): void
{
$element = new ElementDate(new DateTime('2013-09-01 23:55:55+02:00'));
$element->setFormat('c');
Expand All @@ -164,15 +164,15 @@ public function testEquals()
$this->assertFalse($element->equals('ABC'));
}

public function testContains()
public function testContains(): void
{
$element = new ElementDate(new DateTime('2013-09-01 23:55:55+02:00'));

$this->assertTrue($element->contains('2013-09-01T21:55:55+00:00'));
$this->assertFalse($element->contains('2013-06-15'));
}

public function testToString()
public function testToString(): void
{
$element = new ElementDate(new DateTime('2013-09-01 23:55:55+02:00'));

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Element/ElementHexaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

class ElementHexaTest extends TestCase
{
public function testParse()
public function testParse(): void
{
// Skipped.
$offset = 0;
Expand Down
8 changes: 4 additions & 4 deletions tests/Integration/Element/ElementMissingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

class ElementMissingTest extends TestCase
{
public function testEquals()
public function testEquals(): void
{
$element = new ElementMissing();
$this->assertFalse($element->equals(null));
Expand All @@ -46,13 +46,13 @@ public function testEquals()
$this->assertFalse($element->equals(false));
}

public function testGetContent()
public function testGetContent(): void
{
$element = new ElementMissing();
$this->assertFalse($element->getContent());
}

public function testContains()
public function testContains(): void
{
$element = new ElementMissing();
$this->assertFalse($element->contains(null));
Expand All @@ -61,7 +61,7 @@ public function testContains()
$this->assertFalse($element->contains(false));
}

public function testToString()
public function testToString(): void
{
$element = new ElementMissing();
$this->assertEquals('', (string) $element);
Expand Down
Loading

0 comments on commit 43ca68f

Please sign in to comment.