-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor - split into Reader and Writer classes #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
tests: use temp directory for writer
| { | ||
| rewind($this->getFilePointer()); | ||
| $sample = fread($this->getFilePointer(), 10000); | ||
| rewind($this->getFilePointer()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
src/CsvReader.php
Outdated
| ); | ||
| } | ||
| $this->lineBreak = $this->detectLineBreak(); | ||
| rewind($this->getFilePointer()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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 []; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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ší.
There was a problem hiding this comment.
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 ![]()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
njn :/
src/CsvWriter.php
Outdated
| /** | ||
| * @return resource | ||
| */ | ||
| protected function getFilePointer() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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ď?
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
tests/CsvReadTest.php
Outdated
|
|
||
| $writer->writeRow($rows[0]); | ||
| $reader->next(); | ||
| self::assertEquals(false, $reader->current()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
tests/CsvWriteTest.php
Outdated
| { | ||
| $csvFile = new CsvWriter(sys_get_temp_dir() . '/test-write.csv'); | ||
| self::assertEquals('test-write.csv', $csvFile->getBasename()); | ||
| self::assertEquals("\"", $csvFile->getEnclosure()); |
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
Halama
left a comment
There was a problem hiding this 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.
src/CsvWriter.php
Outdated
| /** | ||
| * @throws Exception | ||
| */ | ||
| protected function openCsvFile() |
There was a problem hiding this comment.
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áš.
src/AbstractCsvFile.php
Outdated
|
|
||
| use SplFileInfo; | ||
|
|
||
| class AbstractCsvFile extends SplFileInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
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 |
tests: add check for header with skip lines
feat: allow working with file resources directly
src/CsvReader.php
Outdated
| /** | ||
| * @var string | ||
| */ | ||
| private $fileName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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í ;)
|
jeste me napadlo, ze vlasne muzu vyhodit ten mode, kdyz je tam pointer |
src/CsvWriter.php
Outdated
| protected function closeFile() | ||
| { | ||
| if (is_resource($this->filePointer)) { | ||
| fclose($this->filePointer); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Halama
left a comment
There was a problem hiding this 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čí.
|
@tomasfejfar @Halama merge? |
|
nějak se mi nezdá tohle co jsi nenápadně propašoval do masteru:) Když |
src/CsvWriter.php
Outdated
| 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) . |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
|
hraju si s tím, moc netuším proč ten |
|
no zrovna na to taky koukam jak tele na novy vrata |
|
aha on ten |
|
bych tam dal na začátek |
|
nebo to volat jenom když to vrátí |
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
);
}
} |
|
uz asi vim |
|
aha, zase se me neupdatoval github!, sakra - jo presne tak |
|
Myslím že bysme ještě do |
Halama
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Za mě ok.
|
Dotlačíš prosím ty fixy error handlingu i do 1.4.x ? Hodil bych to pak do toho Redshift extractoru. |
|
done |
|
Jen ideálně za mě nemergovat master to branche, ale rebasovat branch nad master ;) |
|
Releasnu to |
|
jo |

No description provided.