Skip to content

Commit

Permalink
Add PSalm to Anydataset and gitpod
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jul 20, 2022
1 parent 3e91919 commit ca47e95
Show file tree
Hide file tree
Showing 16 changed files with 392 additions and 168 deletions.
5 changes: 5 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tasks:
- init: |
echo 'Build composer'
command: |
composer install
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
"phpunit/phpunit": "5.7.*|7.4.*|^9.5",
"vimeo/psalm": "^4.24"
},
"license": "MIT"
}
15 changes: 15 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
46 changes: 30 additions & 16 deletions src/AnyDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class AnyDataset

/**
* Path to anydataset file
* @var string
* @var string|null
*/
private $filename;

/**
* @param string $filename
* @param null|string $filename
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
Expand All @@ -80,18 +80,23 @@ public function __construct($filename = null)
});
}

/**
* @return string|null
*/
public function getFilename()
{
return $this->filename;
}

/**
*
* @param string|null $file
* @param mixed $closure
* @return void
*/
private function defineSavePath($file, $closure)
{
if (!is_null($file)) {
if (!is_string($file)) {
throw new InvalidArgumentException('I expected a string as a file name');
}

$ext = pathinfo($file, PATHINFO_EXTENSION);
if (empty($ext) && substr($file, 0, 6) !== "php://") {
$file .= '.anydata.xml';
Expand All @@ -106,6 +111,7 @@ private function defineSavePath($file, $closure)
* Private method used to read and populate anydataset class from specified file
*
* @param string $filepath Path and Filename to be read
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
Expand All @@ -121,7 +127,7 @@ private function createFrom($filepath)
$fields = $row->getElementsByTagName("field");
foreach ($fields as $field) {
$attr = $field->attributes->getNamedItem("name");
if (is_null($attr)) {
if (is_null($attr) || is_null($attr->nodeValue)) {
throw new InvalidArgumentException('Malformed anydataset file ' . basename($filepath));
}

Expand All @@ -146,7 +152,8 @@ public function xml()
}

/**
* @param string $filename
* @param string|null $filename
* @return void
* @throws DatabaseException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
Expand All @@ -164,7 +171,7 @@ public function save($filename = null)
/**
* Append one row to AnyDataset.
*
* @param Row|array|\stdClass|object $singleRow
* @param Row|array|\stdClass|object|null $singleRow
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
Expand All @@ -191,6 +198,7 @@ public function appendRow($singleRow = [])
* Enter description here...
*
* @param GenericIterator $iterator
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function import($iterator)
Expand All @@ -204,10 +212,11 @@ public function import($iterator)
* Insert one row before specified position.
*
* @param int $rowNumber
* @param mixed $row
* @param Row|array|\stdClass|object $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function insertRowBefore($rowNumber, $row = null)
public function insertRowBefore($rowNumber, $row)
{
if ($rowNumber > count($this->collection)) {
$this->appendRow($row);
Expand All @@ -216,14 +225,22 @@ public function insertRowBefore($rowNumber, $row = null)
if (!($row instanceof Row)) {
$singleRow = new Row($row);
}

/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
array_splice($this->collection, $rowNumber, 0, '');
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
$this->collection[$rowNumber] = $singleRow;
}
}

/**
*
* @param mixed $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function removeRow($row = null)
Expand Down Expand Up @@ -291,8 +308,7 @@ public function getArray($fieldName, $itf = null)
{
$iterator = $this->getIterator($itf);
$result = array();
while ($iterator->hasNext()) {
$singleRow = $iterator->moveNext();
foreach ($iterator as $singleRow) {
$result[] = $singleRow->get($fieldName);
}
return $result;
Expand All @@ -310,13 +326,11 @@ public function sort($field)
}

$this->collection = $this->quickSortExec($this->collection, $field);

return;
}

/**
* @param Row[] $seq
* @param $field
* @param string $field
* @return array
*/
protected function quickSortExec($seq, $field)
Expand Down
13 changes: 6 additions & 7 deletions src/AnyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,23 @@ public function __construct($list)
}

/**
* How many elements have
* @return int
* @inheritDoc
*/
public function count()
{
return count($this->list);
}

/**
* Ask the Iterator is exists more rows. Use before moveNext method.
* @return bool True if exist more rows, otherwise false
* @inheritDoc
*/
public function hasNext()
{
return ($this->curRow < $this->count());
}

/**
* Return the next row.
*
* @return Row|null
* @inheritDoc
*/
public function moveNext()
{
Expand All @@ -62,6 +58,9 @@ public function moveNext()
return $this->list[$this->curRow++];
}

/**
* @inheritDoc
*/
public function key()
{
return $this->curRow;
Expand Down
11 changes: 10 additions & 1 deletion src/Formatter/BaseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ abstract class BaseFormatter implements FormatterInterface
*/
protected $object;

/**
* @inheritDoc
*/
abstract public function raw();

/**
* @inheritDoc
*/
abstract public function toText();

/**
* @inheritDoc
*/
public function saveToFile($filename)
{
if (empty($filename)) {
Expand All @@ -26,7 +35,7 @@ public function saveToFile($filename)
}

/**
* $object AnyDataset|Row
* @param GenericIterator|Row $object
*/
public function __construct($object)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

class JsonFormatter extends BaseFormatter
{
/**
* @inheritDoc
*/
public function raw()
{
return json_decode($this->toText());
}


/**
* @inheritDoc
*/
public function toText()
{
if ($this->object instanceof GenericIterator) {
Expand Down
21 changes: 16 additions & 5 deletions src/Formatter/XmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\Util\XmlUtil;
use DOMDocument;
use DOMNode;

class XmlFormatter extends BaseFormatter
{
/**
* Return a DOMNode representing AnyDataset
*
* @param array $collection
* @return DOMNode
*/
protected function anydatasetXml($collection)
{
$anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset></anydataset>");
Expand All @@ -21,6 +29,10 @@ protected function anydatasetXml($collection)
return $anyDataSet;
}

/**
* @param array $row
* @return DOMDocument
*/
protected function rowXml($row)
{
$node = XmlUtil::createXmlDocumentFromStr("<row></row>");
Expand All @@ -41,10 +53,7 @@ protected function rowXml($row)


/**
* Returns the AnyDataset XmlDocument representive object
*
* @return \DOMDocument XmlDocument object
* @throws \ByJG\Util\Exception\XmlUtilException
* @inheritDoc
*/
public function raw()
{
Expand All @@ -54,7 +63,9 @@ public function raw()
return $this->rowXml($this->object->toArray());
}


/**
* @inheritDoc
*/
public function toText()
{
return $this->raw()->saveXML();
Expand Down
Loading

0 comments on commit ca47e95

Please sign in to comment.