Skip to content

Commit

Permalink
Merge pull request #25 from kamil-tekiela/loadFromFile
Browse files Browse the repository at this point in the history
Fix loadFromFile params
  • Loading branch information
MauricioFauth committed Sep 13, 2023
2 parents 861d3b4 c894da2 commit 8a8e82e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 50,6 @@ parameters:
count: 1
path: src/ShapeFile.php

-
message: "#^Parameter \\#2 \\$shpFile of method PhpMyAdmin\\\\ShapeFile\\\\ShapeRecord\\:\\:loadFromFile\\(\\) expects resource, resource\\|false given\\.$#"
count: 1
path: src/ShapeFile.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 4
Expand Down
2 changes: 1 addition & 1 deletion src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 457,7 @@ private function loadRecords(): bool
/* Need to start at offset 100 */
while (! $this->eofSHP()) {
$record = new ShapeRecord(-1);
$record->loadFromFile($this, $this->shpFile, $this->dbfFile);
$record->loadFromFile($this, $this->dbfFile);
if ($record->lastError !== '') {
$this->setError($record->lastError);

Expand Down
37 changes: 14 additions & 23 deletions src/ShapeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 42,6 @@ class ShapeRecord
/** @var resource */
private $shpFile;

/** @var resource|false */
private $dbfFile = false;

private ShapeFile|null $shapeFile = null;

private int $size = 0;
Expand All @@ -69,14 66,11 @@ public function __construct(public int $shapeType)
* Loads record from files.
*
* @param ShapeFile $shapeFile The ShapeFile object
* @param resource $shpFile Opened SHP file
* @param resource|false $dbfFile Opened DBF file
*/
public function loadFromFile(ShapeFile $shapeFile, $shpFile, $dbfFile): void
public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void
{
$this->shapeFile = $shapeFile;
$this->shpFile = $shpFile;
$this->dbfFile = $dbfFile;
$this->loadHeaders();

/* No header read */
Expand Down Expand Up @@ -111,11 105,11 @@ public function loadFromFile(ShapeFile $shapeFile, $shpFile, $dbfFile): void
$this->setError(sprintf('Failed to parse record, read=%d, size=%d', $this->read, $this->size));
}

if (! ShapeFile::supportsDbase()) {
if (! ShapeFile::supportsDbase() || $dbfFile === false) {
return;
}

$this->loadDBFData();
$this->loadDBFData($dbfFile);
}

/**
Expand All @@ -128,7 122,6 @@ public function loadFromFile(ShapeFile $shapeFile, $shpFile, $dbfFile): void
public function saveToFile($shpFile, $dbfFile, int $recordNumber): void
{
$this->shpFile = $shpFile;
$this->dbfFile = $dbfFile;
$this->recordNumber = $recordNumber;
$this->saveHeaders();

Expand All @@ -149,11 142,11 @@ public function saveToFile($shpFile, $dbfFile, int $recordNumber): void
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
};

if (! ShapeFile::supportsDbase() || $this->dbfFile === false) {
if (! ShapeFile::supportsDbase() || $dbfFile === false) {
return;
}

$this->saveDBFData();
$this->saveDBFData($dbfFile);
}

/**
Expand Down Expand Up @@ -788,28 781,26 @@ public function getContentLength(): int|null
return $result;
}

private function loadDBFData(): void
/** @param resource $dbfFile Opened DBF file */
private function loadDBFData($dbfFile): void
{
if ($this->dbfFile === false) {
return;
}

$this->dbfData = @dbase_get_record_with_names($this->dbfFile, $this->recordNumber);
$this->dbfData = @dbase_get_record_with_names($dbfFile, $this->recordNumber);
unset($this->dbfData['deleted']);
}

private function saveDBFData(): void
/** @param resource $dbfFile */
private function saveDBFData($dbfFile): void
{
if ($this->dbfData === [] || $this->dbfFile === false) {
if ($this->dbfData === []) {
return;
}

unset($this->dbfData['deleted']);
if ($this->recordNumber <= dbase_numrecords($this->dbfFile)) {
if (! dbase_replace_record($this->dbfFile, array_values($this->dbfData), $this->recordNumber)) {
if ($this->recordNumber <= dbase_numrecords($dbfFile)) {
if (! dbase_replace_record($dbfFile, array_values($this->dbfData), $this->recordNumber)) {
$this->setError("I wasn't possible to update the information in the DBF file.");
}
} elseif (! dbase_add_record($this->dbfFile, array_values($this->dbfData))) {
} elseif (! dbase_add_record($dbfFile, array_values($this->dbfData))) {
$this->setError("I wasn't possible to add the information to the DBF file.");
}
}
Expand Down

0 comments on commit 8a8e82e

Please sign in to comment.