Skip to content

Commit

Permalink
code refactoring with PSR2
Browse files Browse the repository at this point in the history
  • Loading branch information
yupmin committed Oct 27, 2015
1 parent 985b754 commit 80bd6c6
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.DS_Store
/build/
composer.lock
/vendor/
6 changes: 2 additions & 4 deletions src/Chardet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Yupmin\PHPChardet;

use Yupmin\PHPChardet\ChardetCommandBuilder;
use Yupmin\PHPChardet\ChardetOutputParser;

class Chardet
{
/**
* @param $filePath
* @param string $filePath
* @return ChardetContainer
*/
public function analyze($filePath)
{
Expand Down
6 changes: 5 additions & 1 deletion src/ChardetCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Yupmin\PHPChardet;

use Yupmin\PHPChardet\ChardetCommandRunner;
use Symfony\Component\Filesystem\Filesystem;

class ChardetCommandBuilder
{
/**
* @param string $filePath
* @return ChardetCommandRunner
* @throws \Exception
*/
public function buildChardetCommandRunner($filePath)
{
$fileSystem = new Filesystem();
Expand Down
23 changes: 23 additions & 0 deletions src/ChardetCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@

class ChardetCommandRunner
{
/**
* @var string
*/
protected $filePath;

/**
* @var ProcessBuilder
*/
protected $processBuilder;

/**
* @var string
*/
protected $command = 'chardet';

/**
* @var array
*/
protected $arguments = array();

/**
* @param string $filePath
* @param array|null $arguments
* @param ProcessBuilder|null $processBuilder
*/
public function __construct($filePath, array $arguments = null, $processBuilder = null)
{
$this->filePath = $filePath;
Expand All @@ -29,6 +49,9 @@ public function __construct($filePath, array $arguments = null, $processBuilder
}
}

/**
* @return string
*/
public function run()
{
$this->processBuilder->add($this->filePath);
Expand Down
6 changes: 3 additions & 3 deletions src/ChardetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class ChardetContainer
private $confidence;

/**
* @return filepath
* @return string
*/
public function getFilePath()
{
return $this->filePath;
}

/**
* @return charset
* @return string
*/
public function getCharset()
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public function setCharset($charset)
}

/**
* @param string $confidence
* @param float $confidence
*/
public function setConfidence($confidence)
{
Expand Down
4 changes: 1 addition & 3 deletions src/ChardetContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Yupmin\PHPChardet;

use Yupmin\PHPChardet\ChardetContainer;

class ChardetContainerBuilder
{
/**
Expand Down Expand Up @@ -41,7 +39,7 @@ public function setCharset($charset)
}

/**
* @param string $confidence
* @param float $confidence
*/
public function setConfidence($confidence)
{
Expand Down
27 changes: 16 additions & 11 deletions src/ChardetOutputParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Yupmin\PHPChardet;

use Yupmin\PHPChardet\ChardetContainerBuilder;

class ChardetOutputParser
{
/**
Expand All @@ -23,26 +21,33 @@ class ChardetOutputParser

/**
* @param string $output
* @throws \Exception
*/
public function parse($output)
{
/* output case :
- 2.3.0
tests/fixtures/subtitle-test1.srt: EUC-KR with confidence 0.99
tests/fixtures/chardet-output.txt: no result
- 2.0.1
tests/fixtures/subtitle-test1.srt: EUC-KR (confidence: 0.99)
tests/fixtures/chardet-output.txt: None (confidence: 0.00) */
/* output case :
- 2.3.0
tests/fixtures/subtitle-test1.srt: EUC-KR with confidence 0.99
tests/fixtures/chardet-output.txt: no result
- 2.0.1
tests/fixtures/subtitle-test1.srt: EUC-KR (confidence: 0.99)
tests/fixtures/chardet-output.txt: None (confidence: 0.00) */

if ((preg_match('/(.+): (.+) .+confidence:? ([^\)]+)/', $output, $matches) === 0)
|| (isset($matches[2]) && $matches[2] == 'None')) {
|| (isset($matches[2]) && $matches[2] == 'None')
) {
throw new \Exception('This file is not analyzed.');
}

$this->parsedFilePath = $matches[1];
$this->parsedCharset = strtolower($matches[2]);
$this->parsedConfidence = (float) $matches[3];
$this->parsedConfidence = (float)$matches[3];
}

/**
* @return ChardetContainer
* @throws \Exception
*/
public function getChardetContainer()
{
if ($this->parsedFilePath === null || $this->parsedCharset === null || $this->parsedConfidence === null) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPChardet/ChardetCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ChardetCommandBuilderTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->filePath = __DIR__.'/../fixtures/chardet-input.txt';
$this->filePath = __DIR__ . '/../fixtures/chardet-input.txt';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPChardet/ChardetCommandRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ChardetCommandRunnerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->filePath = __DIR__.'/../fixtures/chardet-input.txt';
$this->outputPath = __DIR__.'/../fixtures/chardet-output.txt';
$this->filePath = __DIR__ . '/../fixtures/chardet-input.txt';
$this->outputPath = __DIR__ . '/../fixtures/chardet-output.txt';
}

public function testRun()
Expand Down
1 change: 1 addition & 0 deletions tests/PHPChardet/ChardetOutputParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function setUp()
{

}

/**
* @expectedException \Exception
*/
Expand Down
11 changes: 6 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ function includeIfExists($file)
}
}

if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php'))
&& (!$loader = includeIfExists(__DIR__.'/../../../.composer/autoload.php'))) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php'))
&& (!$loader = includeIfExists(__DIR__ . '/../../../.composer/autoload.php'))
) {
die('You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL);
}
return $loader;

0 comments on commit 80bd6c6

Please sign in to comment.