Skip to content

Conversation

@odinuv
Copy link
Member

@odinuv odinuv commented Apr 23, 2018

No description provided.

@odinuv odinuv requested review from Halama and tomasfejfar April 23, 2018 13:17
{
rewind($this->getFilePointer());
$sample = fread($this->getFilePointer(), 10000);
rewind($this->getFilePointer());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nespoléhal bych na to, že to nikdo nezavolá jen proto, že je to protected. Ta třída jde extendovat a budou z toho těžko odhalitelné chyby. Nebo co ji dát final?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tomu nerozumim, vadi ti, ze ma detectLineBreak side effect? no a co? nejmenuje se to getLineBreak (srovnej se soucasnou verzi, kde getLineBreak ma sideeffect), stejne tak ma openCsvfile a closeCsvFile maji side effect, pokud si tu tridu nekdo extenduje a zavola to nekde uprostred cteni/zapisu, tak je proste blbej

nevidim smysl v tom snazit se tomu nejak zabranit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

);
}
$this->lineBreak = $this->detectLineBreak();
rewind($this->getFilePointer());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proč je tady jiný rewind než na L176?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tj. proč tady není $this->rewind()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protoze to dela neco jinyho - header je vzdycky prvni radek, ale $this->rewind respektuje skip lines, kdyby se tady volalo $this->rewind, tak bude header nejaka blbost

return $this->header;
}

return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kdy to propadne sem? IMHO nikdy, ne?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u prazdnyho souboru

$return = [];
foreach ($row as $column) {
if (!(
is_scalar($column)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady mě ještě teď když to vidím zformátované napadlo - nešlo by z toho udělat if (!$stringable) - to by neovlivnilo early exit v tom boolean výrazu a přitom by byla ta podmínka pak čitelnější.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$isStringable
Diff 0 : 489.37952709198
Diff 1 : 497.38059091568
Diff 2 : 503.51376700401
Diff 3 : 505.61457920074
Diff 4 : 507.42060494423
Diff 5 : 504.46329498291
Diff 6 : 503.64453482628
Diff 7 : 501.33763980865
Diff 8 : 501.47956895828
Diff 9 : 497.10404992104

vs

soucasny stav
Diff 0 : 474.2037358284
Diff 1 : 463.99877309799
Diff 2 : 459.16944909096
Diff 3 : 457.29524803162
Diff 4 : 472.84506487846
Diff 5 : 491.63278388977
Diff 6 : 492.76865696907
Diff 7 : 489.76580095291
Diff 8 : 488.95872497559
Diff 9 : 478.8238589763

sice uz to neni takovej rozdil, ale je tam, takze ne, nebudu to prepisovat aby to bylo citelnejsi :) budes se muset naucit cist :trollface:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

njn :/

/**
* @return resource
*/
protected function getFilePointer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta metoda je stejná pro reader i writer, ne? Tak by mohla být v tom abstraktním předkovi.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je, a puvodne jsem ji tam mel, ale je to na palici, protoze ten pointer ma jinou inicializaci, takze zusatane v abstraktni tride nesmyslnej fragment, a vlastne je teda metoda stejna jen "shodou okolnosti"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tak nakonec jsem to udelal :) pridanim tech pointeru tam vzniklo vic spolecnyho kodu a zda se , ze to vyresila jedna abstraktni metoda

{
try {
$csv = new CsvReader(__DIR__ . '/nonexistent.csv');
$csv->getHeader();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevyhodí to exception i bez toho načtení headeru teď?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy & paste :( bohuzel to tam bylo uz z predchoziho pr :(

fixed

$fileName = __DIR__ . '/data/simple.csv';

$csvFile = new CsvReader($fileName);
self::assertEquals("\n", $csvFile->getLineBreak());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nechybí tady ještě nějaké ověření načtení jako v testu nad tím? A neměli by se jmenovat stejně? buď "...WithoutRewind" nebo "...NoReset"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


$writer->writeRow($rows[0]);
$reader->next();
self::assertEquals(false, $reader->current());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakto? Neměl by tam být už ten zapsaný řádek?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bylo by fajn tam dát komentář nebo message k tomu assertu

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nemel, protoze ten reader je nakonci, musi se mu tam dat rewind
je to counterintuitive a nereprezentuje to zadnej usecase, ale je to dokumentace toho jak se to chova pokud udelas takovyhle zverstvo :) Nicmene porad je to lepsi nez stavajici verze, ktera je schopna si prepsat existujici radky (protoze ma stejny pointer pro read i write)

{
$csvFile = new CsvWriter(sys_get_temp_dir() . '/test-write.csv');
self::assertEquals('test-write.csv', $csvFile->getBasename());
self::assertEquals("\"", $csvFile->getEnclosure());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace quotes " → '

"columns with\nnew line", "columns with\ttab",
],
[
'column with \n \t \\\\', 'second col',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je tady ' schválně? Takhle to je skutečně \n a ne nový řádek

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jo, koukám, že podle testu je to asi expected...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ano, je, testuje to, ze se to neinterpretuje

Copy link
Member

@Halama Halama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Komenty níže. Plus jsem si ještě všimnul že nejsou updatnuté examples v Readme.

/**
* @throws Exception
*/
protected function openCsvFile()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dal bych vše v reader i writer jako private a ideálně by ty classy mohly být final jak myslím už zmiňoval Tomáš.


use SplFileInfo;

class AbstractCsvFile extends SplFileInfo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Když už se to refaktoruje tak si myslím že by to němělo dědit ten SplFileInfo, ale měl by být jako private property. To by mohlo umožnit např. založit ten reader/writer už z existujícího file resource. Na což jsem párkrát narazil a jsou na to i issues #14 a #5

@odinuv
Copy link
Member Author

odinuv commented Apr 27, 2018

Jeste me napadlo, jestli nevyhodit jeden z tech exception kodu - je tam string code a number code https://github.com/keboola/php-csv/blob/master/src/Exception.php#L7

/**
* @var string
*/
private $fileName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nechceš si nastavit oneline field docy?
image

Copy link
Member Author

@odinuv odinuv May 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ja ti nevim, me se to nejak nelibi :) premyslel jsem nad tim proc a asi mam dva duvody, jednak k fieldum nekdy pisu komentar k cemu jsou a jednak me drazdi kdyz je phpdoc u metody jinej nez u fieldu :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Motivace proč to používat je veskrze:

  • fieldy jsou na začátku třídy a takhle máš 1(blank)+3(doc)+1(def) x N místo 1+1+1 x N řádků. Tj. prostě víc scrollovaní
  • fieldy většinou (!) nemívají description a ostatní součásti docblocku (a pokud ano, tak ho můžeš udělat víceřádkový

Ale více méně souhlasím, že i tvoje argumenty jsou validní ;)

@odinuv
Copy link
Member Author

odinuv commented May 2, 2018

jeste me napadlo, ze vlasne muzu vyhodit ten mode, kdyz je tam pointer

protected function closeFile()
{
if (is_resource($this->filePointer)) {
fclose($this->filePointer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nemělo by ho to zavřít jen když ho to samo otevřelo? Vím že nějaká knihovna se takhle chovala že to vždy zavírala a pěkně mě to štvalo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a to samé pro reader.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nj, to me uplne uslo!

Copy link
Member

@Halama Halama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Myslím že by to nemělo zavírat resources které jsou tomu předány zvenčí.

@odinuv
Copy link
Member Author

odinuv commented May 13, 2018

@tomasfejfar @Halama merge?

@Halama
Copy link
Member

Halama commented May 13, 2018

nějak se mi nezdá tohle co jsi nenápadně propašoval do masteru:) Když error_get_last vrání null tak to failne.

if (($ret === false) || (($ret === 0) && (strlen($str) > 0))) {
throw new Exception(
"Cannot write to CSV file " . $this->fileName .
' Error: ' . error_get_last()['message'] . ' Return: ' . json_encode($ret) .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Myslím že se může stát že v error_get_last nic nebude a pak to failne při přístupu k null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, no to mi uslo, ze error_get_last vraci null, protoze jsem to zkousel bez erroru. mazec:

php > var_export(null['test']);
php > NULL

takze error to neudela, ale vrchol cistoty to teda neni :) tak tam asi dam json_encode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zajímavý to s tím null jsem neznal :)

Copy link
Member

@Halama Halama May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing variables of other types (not including arrays or objects implementing the appropriate interfaces) using [] or {} silently returns NULL.

http://php.net/manual/en/language.types.string.php
Jsem se tam dostal přes tohle https://stackoverflow.com/questions/10990321/why-does-php-not-complain-when-i-treat-a-null-value-as-an-array-like-this

Tohle je taky super zápis $z = null['neco']['sdfdsf']; :)

… any

tests: added tests for different types of errors
@Halama
Copy link
Member

Halama commented May 14, 2018

hraju si s tím, moc netuším proč ten fopen háže tu chybu https://travis-ci.org/keboola/php-csv/jobs/378425803#L574

@odinuv
Copy link
Member Author

odinuv commented May 14, 2018

no zrovna na to taky koukam jak tele na novy vrata

@Halama
Copy link
Member

Halama commented May 14, 2018

aha on ten error_get_last tam bude z jineho testu:( Tohle projde php vendor/bin/phpunit --debug --filter testInvalidPointer

@Halama
Copy link
Member

Halama commented May 14, 2018

bych tam dal na začátek writeRow error_clear_last();

@Halama
Copy link
Member

Halama commented May 14, 2018

nebo to volat jenom když to vrátí false předtím

@Halama
Copy link
Member

Halama commented May 14, 2018

public function writeRow(array $row)
    {
        $str = $this->rowToStr($row);
        $ret = @fwrite($this->getFilePointer(), $str);

        /* According to http://php.net/fwrite the fwrite() function
         should return false on error. However not writing the full
         string (which may occur e.g. when disk is full) is not considered
         as an error. Therefore both conditions are necessary. */
        if (($ret === false) || (($ret === 0) && (strlen($str) > 0))) {
            throw new Exception(
                "Cannot write to CSV file " . $this->fileName .
                ($ret === false ? 'Error: ' . error_get_last()['message'] : '') .
                ' Return: ' . json_encode($ret) .
                ' To write: ' . strlen($str) . ' Written: ' . $ret,
                Exception::WRITE_ERROR
            );
        }
    }

@odinuv
Copy link
Member Author

odinuv commented May 14, 2018

uz asi vim

@odinuv
Copy link
Member Author

odinuv commented May 14, 2018

aha, zase se me neupdatoval github!, sakra - jo presne tak

@Halama
Copy link
Member

Halama commented May 14, 2018

Myslím že bysme ještě do tests/bootstrap.php měli přidat error_reporting(-1); https://github.com/aws/aws-sdk-php/blob/5177cbe73c96d25ff759e406ac81b8e25557482c/tests/bootstrap.php#L2
Teď to nefailne na notice apod. Zkoumal jsem to chování null...

Copy link
Member

@Halama Halama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Za mě ok.

@Halama
Copy link
Member

Halama commented May 14, 2018

Dotlačíš prosím ty fixy error handlingu i do 1.4.x ? Hodil bych to pak do toho Redshift extractoru.

odinuv added a commit that referenced this pull request May 14, 2018
@odinuv
Copy link
Member Author

odinuv commented May 14, 2018

done

@tomasfejfar
Copy link
Contributor

Jen ideálně za mě nemergovat master to branche, ale rebasovat branch nad master ;)

@Halama
Copy link
Member

Halama commented May 18, 2018

Releasnu to

@odinuv
Copy link
Member Author

odinuv commented May 18, 2018

jo

@Halama Halama merged commit d46e83e into master May 18, 2018
@Halama Halama deleted the odin-reader-writer branch May 18, 2018 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants