Skip to content

Commit

Permalink
Some in-code improvements: use sprintf, PHP 5.4 array notation
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 9, 2015
1 parent 2599f48 commit 3dc53a3
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/Exception/DuplicateHeadersException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
class DuplicateHeadersException extends ReaderException implements ExceptionInterface
{
/**
* @param array $duplicates
*/
public function __construct(array $duplicates)
{
parent::__construct(sprintf('File contains duplicate headers: %s', implode($duplicates, ', ')));
Expand Down
6 changes: 5 additions & 1 deletion src/Exception/UnexpectedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class UnexpectedTypeException extends \UnexpectedValueException implements Excep
*/
public function __construct($value, $expectedType)
{
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, is_object($value) ? get_class($value) : gettype($value)));
parent::__construct(sprintf(
'Expected argument of type "%s", "%s" given',
$expectedType,
is_object($value) ? get_class($value) : gettype($value)
));
}
}
7 changes: 3 additions & 4 deletions src/Filter/DateTimeThresholdFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ public function __invoke(array $item)
throw new \LogicException('Make sure you set a threshold');
}

return
call_user_func($this->valueConverter, $item[$this->timeColumnName])
>=
$this->threshold;
$threshold = call_user_func($this->valueConverter, $item[$this->timeColumnName]);

return $threshold >= $this->threshold;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Filter/ValidatorFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class ValidatorFilter
/**
* @var array
*/
private $constraints = array();
private $constraints = [];

/**
* @var array
*/
private $violations = array();
private $violations = [];

/**
* @param ValidatorInterface $validator
Expand All @@ -57,7 +57,7 @@ public function __construct(ValidatorInterface $validator)
public function add($field, Constraint $constraint)
{
if (!isset($this->constraints[$field])) {
$this->constraints[$field] = array();
$this->constraints[$field] = [];
}

$this->constraints[$field][] = $constraint;
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/ArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function getFields()
return array_keys($this[0]);
}

return array();
return [];
}
}
8 changes: 4 additions & 4 deletions src/Reader/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CsvReader implements CountableReaderInterface, \SeekableIterator
*
* @var array
*/
protected $columnHeaders = array();
protected $columnHeaders = [];

/**
* Number of column headers, stored and re-used for performance
Expand All @@ -56,7 +56,7 @@ class CsvReader implements CountableReaderInterface, \SeekableIterator
*
* @var array
*/
protected $errors = array();
protected $errors = [];

/**
* Strict parsing - skip any lines mismatching header length
Expand Down Expand Up @@ -374,7 +374,7 @@ protected function readHeaderRow($rowNumber)
*/
protected function incrementHeaders(array $headers)
{
$incrementedHeaders = array();
$incrementedHeaders = [];
foreach (array_count_values($headers) as $header => $count) {
if ($count > 1) {
$incrementedHeaders[] = $header;
Expand Down Expand Up @@ -405,7 +405,7 @@ protected function incrementHeaders(array $headers)
*/
protected function mergeDuplicates(array $line)
{
$values = array();
$values = [];

$i = 0;
foreach ($this->columnHeaders as $count) {
Expand Down
8 changes: 4 additions & 4 deletions src/Reader/DbalReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DbalReader implements CountableReaderInterface
* @param string $sql
* @param array $params
*/
public function __construct(Connection $connection, $sql, array $params = array())
public function __construct(Connection $connection, $sql, array $params = [])
{
$this->connection = $connection;

Expand Down Expand Up @@ -94,7 +94,7 @@ public function getFields()
$this->rewind();
}
if (false === $this->data) {
return array();
return [];
}

return array_keys((array) $this->data);
Expand All @@ -108,7 +108,7 @@ public function getFields()
*
* @return $this
*/
public function setSql($sql, array $params = array())
public function setSql($sql, array $params = [])
{
$this->sql = (string) $sql;

Expand Down Expand Up @@ -211,7 +211,7 @@ public function count()

private function doCalcRowCount()
{
$statement = $this->prepare('SELECT COUNT(*) FROM ('.$this->sql.')', $this->params);
$statement = $this->prepare(sprintf('SELECT COUNT(*) FROM (%s)', $this->sql), $this->params);
$statement->execute();

$this->rowCount = (int) $statement->fetchColumn(0);
Expand Down
6 changes: 3 additions & 3 deletions src/Reader/DoctrineReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function rewind()
{
if (!$this->iterableResult) {
$query = $this->objectManager->createQuery(
sprintf('select o from %s o', $this->objectName)
sprintf('SELECT o FROM %s o', $this->objectName)
);
$this->iterableResult = $query->iterate(array(), Query::HYDRATE_ARRAY);
$this->iterableResult = $query->iterate([], Query::HYDRATE_ARRAY);
}

$this->iterableResult->rewind();
Expand All @@ -100,7 +100,7 @@ public function rewind()
public function count()
{
$query = $this->objectManager->createQuery(
sprintf('select count(o) from %s o', $this->objectName)
sprintf('SELECT COUNT(o) FROM %s o', $this->objectName)
);

return $query->getSingleScalarResult();
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Factory/DbalReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Connection $connection)
*
* @return DbalReader
*/
public function getReader($sql, array $params = array())
public function getReader($sql, array $params = [])
{
return new DbalReader($this->connection, $sql, $params);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Reader/OneToManyReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class OneToManyReader implements CountableReaderInterface
protected $rightReader;

/**
* @var string Left Join Field
* @var string
*/
protected $leftJoinField;

/**
* @var string Right Join Field
* @var string
*/
protected $rightJoinField;

Expand Down Expand Up @@ -86,7 +86,7 @@ public function current()
)
);
}
$leftRow[$this->nestKey] = array();
$leftRow[$this->nestKey] = [];

$leftId = $this->getRowId($leftRow, $this->leftJoinField);
$rightRow = $this->rightReader->current();
Expand Down Expand Up @@ -172,7 +172,7 @@ public function rewind()
*/
public function getFields()
{
return array_merge($this->leftReader->getFields(), array($this->nestKey));
return array_merge($this->leftReader->getFields(), [$this->nestKey]);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Reader/PdoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PdoReader implements CountableReaderInterface
* @param string $sql
* @param array $params
*/
public function __construct(\PDO $pdo, $sql, array $params = array())
public function __construct(\PDO $pdo, $sql, array $params = [])
{
$this->pdo = $pdo;
$this->statement = $this->pdo->prepare($sql);
Expand All @@ -54,10 +54,10 @@ public function getFields()
// Grab the first row to find keys
$row = $this->statement->fetch(\PDO::FETCH_ASSOC);
// Return field keys, or empty array no rows remain
return array_keys($row ? $row : array());
return array_keys($row ? $row : []);
} else {
// If the statement errors return empty
return array();
return [];
}
}

Expand Down Expand Up @@ -101,6 +101,7 @@ public function valid()
public function rewind()
{
$this->loadData();

reset($this->data);
}

Expand Down
32 changes: 14 additions & 18 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

namespace Ddeboer\DataImport;

use DateTime;
use DateInterval;
use Ddeboer\DataImport\Exception\ExceptionInterface;

/**
* Simple Container for Workflow
* Results
* Simple Container for Workflow Results
*
* @author Aydin Hassan <aydin@hotmail.co.uk>
*/
class Result
{

/**
* Identifier given to the import/export
*
Expand All @@ -23,17 +19,17 @@ class Result
protected $name;

/**
* @var DateTime
* @var \DateTime
*/
protected $startTime;

/**
* @var DateTime
* @var \DateTime
*/
protected $endTime;

/**
* @var DateInterval
* @var \DateInterval
*/
protected $elapsed;

Expand All @@ -53,18 +49,18 @@ class Result
protected $totalProcessedCount = 0;

/**
* @var ExceptionInterface[]
* @var \SplObjectStorage
*/
protected $exceptions;

/**
* @param string $name
* @param DateTime $startTime
* @param DateTime $endTime
* @param integer $totalCount
* @param ExceptionInterface[] $exceptions
* @param string $name
* @param \DateTime $startTime
* @param \DateTime $endTime
* @param integer $totalCount
* @param \SplObjectStorage $exceptions
*/
public function __construct($name, DateTime $startTime, DateTime $endTime, $totalCount, \SplObjectStorage $exceptions)
public function __construct($name, \DateTime $startTime, \DateTime $endTime, $totalCount, \SplObjectStorage $exceptions)
{
$this->name = $name;
$this->startTime = $startTime;
Expand All @@ -85,23 +81,23 @@ public function getName()
}

/**
* @return DateTime
* @return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}

/**
* @return DateTime
* @return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}

/**
* @return DateInterval
* @return \DateInterval
*/
public function getElapsed()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Step/MappingStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MappingStep implements StepInterface
/**
* @var array
*/
private $mappings;
private $mappings = [];

/**
* @var PropertyAccessor
Expand Down Expand Up @@ -57,7 +57,7 @@ public function process(&$item)
$value = $this->accessor->getValue($item, $from);
$this->accessor->setValue($item, $to, $value);

$from = str_replace(array('[',']'), '', $from);
$from = str_replace(['[',']'], '', $from);

// Check if $item is an array, because properties can't be unset.
// So we don't call unset for objects to prevent side affects.
Expand All @@ -66,9 +66,9 @@ public function process(&$item)
}
}
} catch (NoSuchPropertyException $exception) {
throw new MappingException('Unable to map item',null,$exception);
throw new MappingException('Unable to map item', null, $exception);
} catch (UnexpectedTypeException $exception) {
throw new MappingException('Unable to map item',null,$exception);
throw new MappingException('Unable to map item', null, $exception);
}
}
}
2 changes: 1 addition & 1 deletion src/Step/StepInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface StepInterface
/**
* Any processing done on each item in the data stack
*
* @param array &$item
* @param mixed &$item
*
* @return boolean False return value means the item should be skipped
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Step/ValidatorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Validator $validator)
public function add($field, Constraint $constraint)
{
if (!isset($this->constraints[$field])) {
$this->constraints[$field] = array();
$this->constraints[$field] = [];
}

$this->constraints[$field][] = $constraint;
Expand Down
2 changes: 1 addition & 1 deletion src/ValueConverter/MappingValueConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MappingValueConverter
/**
* @var array
*/
private $mapping;
private $mapping = [];

/**
* @param array $mapping
Expand Down
Loading

0 comments on commit 3dc53a3

Please sign in to comment.