Skip to content

Commit

Permalink
Merge pull request #50 from bobvandevijver/add-typings
Browse files Browse the repository at this point in the history
Add typings based on PHPDoc
  • Loading branch information
tobias-93 authored Mar 5, 2024
2 parents 7214101 + 7ba8cbc commit 05d84ff
Show file tree
Hide file tree
Showing 46 changed files with 321 additions and 833 deletions.
4 changes: 2 additions & 2 deletions src/Command/LatexTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(private readonly LatexGeneratorInterface $latexGener
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this
->setName('bobv:latex:test')
->setDescription('Generate a test LaTeX file (+ pdf)')
Expand Down Expand Up @@ -56,6 +56,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$output->writeln($generatedLocation);

return 0; // Success
return Command::SUCCESS;
}
}
10 changes: 1 addition & 9 deletions src/DependencyInjection/BobvLatexExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\String\UnicodeString;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class BobvLatexExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container) {
public function load(array $configs, ContainerBuilder $container): void {
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

Expand Down
9 changes: 0 additions & 9 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see
* {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder(): TreeBuilder {
$treeBuilder = new TreeBuilder('bobv_latex');

Expand Down
10 changes: 2 additions & 8 deletions src/Exception/BibliographyGenerationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@

class BibliographyGenerationException extends LatexException
{

/**
* @param string $texLocation
* @param int $exitCode
* @param string|null $exitCodeText
*/
public function __construct($texLocation, $exitCode, $exitCodeText = NULL) {
if ($exitCodeText !== NULL) {
public function __construct(string $texLocation, int $exitCode, ?string $exitCodeText = null) {
if ($exitCodeText !== null) {
$exitCodeText = sprintf(' (%s)', $exitCodeText);
} else {
$exitCodeText = '';
Expand Down
12 changes: 2 additions & 10 deletions src/Exception/ImageNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
namespace Bobv\LatexBundle\Exception;

/**
* Class ImageNotFoundException
* Simple \Exception extend for better error origin check
*/
class ImageNotFoundException extends \Exception
{
private $imageLocation;

public function __construct($imageLocation)
public function __construct(private readonly string $imageLocation)
{
$this->imageLocation = $imageLocation;

parent::__construct("The image used is not found. Did you provide the complete path? (provided path = $imageLocation)");
}

/**
* @return mixed
*/
public function getImageLocation()
public function getImageLocation(): string
{
return $this->imageLocation;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Exception/LatexException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Bobv\LatexBundle\Exception;

/**
* Class LatexException
* Simple \Exception extend for better error origin check
*/
class LatexException extends \Exception{

class LatexException extends \Exception
{
}
5 changes: 2 additions & 3 deletions src/Exception/LatexNotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Bobv\LatexBundle\Exception;

/**
* Class LatexNotImplementedException
* Simple \Exception extend for better error origin check
*/
class LatexNotImplementedException extends LatexException{

class LatexNotImplementedException extends LatexException
{
}
66 changes: 21 additions & 45 deletions src/Exception/LatexParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,24 @@
namespace Bobv\LatexBundle\Exception;

/**
* Class LatexException
* Simple \Exception extend for better error origin check
*/
class LatexParseException extends LatexException
{


const LOG_GET_LINES = 4;
const LOG_MAX_LINES = 10;
const TEX_GET_LINES = 8;
const TEX_MAX_LINES = 20;

protected $filteredTexSource = array();
protected array $filteredTexSource = [];

protected $filteredLogSource = array();
protected array $filteredLogSource = [];

/**
* @param string $texLocation
* @param int $exitCode
* @param array|NULL $pdfLatexOutput
*/
public function __construct($texLocation, $exitCode, array $pdfLatexOutput = NULL, $exitCodeText = NULL)
public function __construct(string $texLocation, int $exitCode, ?array $pdfLatexOutput = null, ?string $exitCodeText = null)
{
if($exitCodeText !== NULL){
if ($exitCodeText !== null) {
$exitCodeText = sprintf(' (%s)', $exitCodeText);
}else{
} else {
$exitCodeText = '';
}

Expand All @@ -41,10 +33,8 @@ public function __construct($texLocation, $exitCode, array $pdfLatexOutput = NUL

/**
* Return an extended error message together with the extracted errors
*
* @return string
*/
public function getExtendedMessage()
public function getExtendedMessage(): string
{
$message = $this->getMessage();

Expand All @@ -57,28 +47,24 @@ public function getExtendedMessage()
}

/**
* Try to find usefull information on the error that has occurred
*
* @param array $errorOutput
* @param string $texLocation
*
* @return array Contains the filtered output which should only contain information about the errors
* Try to find useful information on the error that has occurred
* This is stored in the object properties $filteredLogSource and $filteredTexSource
*/
protected function findErrors(array $errorOutput, $texLocation = NULL)
protected function findErrors(array $errorOutput, ?string $texLocation = NULL): void
{
$refWarning = strtolower('LaTeX Warning: Reference');
$filteredErrors = array();
$filteredErrors[] = "---";
$filteredErrors = [];
$filteredErrors[] = '---';

array_walk($errorOutput, function ($value, $key) use (&$errorOutput, &$texLocation, &$filteredErrors, $refWarning) {

// Find lines with an error
if (preg_match_all('/error|missing|not found|undefined|too many|runaway|\$|you can\'t use|invalid/ui', $value) > 0) {
if (substr(strtolower($errorOutput[$key]), 0, strlen($refWarning)) !== $refWarning) {
if (!str_starts_with(strtolower($errorOutput[$key]), $refWarning)) {
// Get the lines surrounding the error, but do not include empty lines

// Get lines before the error
$temp = array();
$temp = [];
for ($count = 0, $i = 0; $count < self::LOG_GET_LINES && $i < self::LOG_MAX_LINES; $i++) {
if (isset($errorOutput[$key - $i])) {
$value = trim(preg_replace('/\s+/', ' ', $errorOutput[$key - $i]));
Expand All @@ -105,7 +91,7 @@ protected function findErrors(array $errorOutput, $texLocation = NULL)
}
}

$filteredErrors[] = "---";
$filteredErrors[] = '---';
}
}

Expand All @@ -116,16 +102,16 @@ protected function findErrors(array $errorOutput, $texLocation = NULL)

// Try to find matching tex lines
// Check if a line number can be found in the errors
$this->filteredTexSource[] = "---";
$this->filteredTexSource[] = '---';
if ($texLocation !== NULL) {
$lineNumber = array();
$lineNumber = [];
$texFile = new \SplFileObject($texLocation);
foreach ($this->filteredLogSource as $logLine) {
preg_match('/l\.(\d+)/ui', $logLine, $lineNumber);
if (count($lineNumber) == 2) {

// Get lines before the linenumber
$temp = array();
$temp = [];
for ($count = 0, $i = 0; $count < self::TEX_GET_LINES && $i < self::TEX_MAX_LINES; $i++) {
$texFile->seek($lineNumber[1] - $i);
if ($texFile->valid()) {
Expand All @@ -138,7 +124,7 @@ protected function findErrors(array $errorOutput, $texLocation = NULL)
break;
}
}
$this->filteredTexSource = array_merge($this->filteredTexSource, array($lineNumber[0]), array_reverse($temp));
$this->filteredTexSource = array_merge($this->filteredTexSource, [$lineNumber[0]], array_reverse($temp));

// Get lines after the line number
for ($count = 0, $i = 1; $count < self::TEX_GET_LINES && $i < self::TEX_MAX_LINES; $i++) {
Expand All @@ -153,29 +139,19 @@ protected function findErrors(array $errorOutput, $texLocation = NULL)
break;
}
}

$this->filteredTexSource[] = "---";

$this->filteredTexSource[] = '---';
}
}
}
}

/**
* @return string
*/
public function getFilteredTexSource()
public function getFilteredTexSource(): string
{
return implode("\n", $this->filteredTexSource);
}

/**
* @return string
*/
public function getFilteredLogSource()
public function getFilteredLogSource(): string
{
return implode("\n", $this->filteredLogSource);
}


}
Loading

0 comments on commit 05d84ff

Please sign in to comment.