# SplFileObject Countable

- Thread: https://externals.io/message/51672
- Original archive: https://news-web.php.net/php.internals/51672
- Started by Sebastian Marek on 2011-03-11T23:22:04+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #51672 — Sebastian Marek — 2011-03-11T23:22:04+00:00

URL: https://externals.io/message/51672#51672

Hi,

I have recently used SplFileObject to work with files. I was lacking a bit
of functionality in there regarding counting number of lines in a given
file. Although I could just load contents of the file to an array and check
it size, I thought it would be nice to have SplFileObject Countable.

Here is an example implementation I made tonight that seems to work well -
https://gist.github.com/866767 . Is it something you think would be worth
adding to the existing SplFileObject? Is the implementation any good? I
reused some of the existing logic to avoid breaking 'spl_filesystem_object'
internal integrity, but maybe it would make sense to make it work with the
stream itself.

If it's fine I can write some unit tests to cover this and then submit a
complete patch.

Regards
-- 
Sebastian Marek
proofek@gmail.com

Follow me online at:
Blog: http://criticallog.thornet.net/
Twitter: http://twitter.com/proofek
Linkedin: http://uk.linkedin.com/in/sebastianmarek
Ohloh: http://www.ohloh.net/accounts/proofek
<http://twitter.com/proofek

---

## #51675 — Etienne Kneuss — 2011-03-13T21:39:28+00:00

URL: https://externals.io/message/51672#51675 · In reply to: #51672

> Hi,
> 
> I have recently used SplFileObject to work with files. I was lacking a bit
> of functionality in there regarding counting number of lines in a given
> file. Although I could just load contents of the file to an array and check
> it size, I thought it would be nice to have SplFileObject Countable.
> 
> Here is an example implementation I made tonight that seems to work well -
> https://gist.github.com/866767 . Is it something you think would be worth
> adding to the existing SplFileObject? Is the implementation any good? I
> reused some of the existing logic to avoid breaking 'spl_filesystem_object'
> internal integrity, but maybe it would make sense to make it work with the
> stream itself.

In my oppinion, it does not make sense to implement countable for this
use. Countable should be implemented on classes where calling `count()` on
them is not ambiguous, i.e. the size of a collection.

There is no reason why `count()` on a file should be the number of lines,
why not words, or bytes?

I'd however be happy with a new countLines() method.

Best,

> 
> If it's fine I can write some unit tests to cover this and then submit a
> complete patch.
> 
> Regards
> -- 
> Sebastian Marek
> proofek@gmail.com
> 
> Follow me online at:
> Blog: http://criticallog.thornet.net/
> Twitter: http://twitter.com/proofek
> Linkedin: http://uk.linkedin.com/in/sebastianmarek
> Ohloh: http://www.ohloh.net/accounts/proofek
> <http://twitter.com/proofek

---

## #51689 — Sebastian Marek — 2011-03-14T22:24:09+00:00

URL: https://externals.io/message/51672#51689 · In reply to: #51675

Hello,

That makes perfect sense. I have raised a feature request and have attached
a patch containing both countLines() method implementation and phpt tests.

thx



> 
> > […]
> bit
> > […]
> check
> > […]
> -
> > […]
> worth
> > […]
> 'spl_filesystem_object'
> > […]
> the
> > […]
>
> In my oppinion, it does not make sense to implement countable for this
> use. Countable should be implemented on classes where calling `count()` on
> them is not ambiguous, i.e. the size of a collection.
>
> There is no reason why `count()` on a file should be the number of lines,
> why not words, or bytes?
>
> I'd however be happy with a new countLines() method.
>
> Best,
>
> > […]
>



-- 
Sebastian Marek
proofek@gmail.com

Follow me online at:
Blog: http://criticallog.thornet.net/
Twitter: http://twitter.com/proofek
Linkedin: http://uk.linkedin.com/in/sebastianmarek
Ohloh: http://www.ohloh.net/accounts/proofek
<http://twitter.com/proofek

---

## #51690 — Nick Pope — 2011-03-15T09:35:03+00:00

URL: https://externals.io/message/51672#51690 · In reply to: #51689

Hi Sebastian,

Since Etienne brought it up, would it not also make sense for you to 
implement countWords() and countBytes()?

Also, unicode issues aside, it might still be nice to implement 
countChars().

And finally, so that it provides all most of the functionality of the 
GNU wc program, there could also be getMaxLineLength().  This would need 
a different name however as this method would be to count the longest 
line of the file rather than indicate the maximum length of a line to 
read as the current SplFileObject::getMaxLineLen() method does.

Just my thoughts,

Nick



> Hello,
>
> That makes perfect sense. I have raised a feature request and have attached
> a patch containing both countLines() method implementation and phpt tests.
>
> thx
>
> 
>
> > […]

---

## #51692 — Peter Cowburn — 2011-03-15T10:01:26+00:00

URL: https://externals.io/message/51672#51692 · In reply to: #51690

Hi all,


> Hi Sebastian,
>
> Since Etienne brought it up, would it not also make sense for you to
> implement countWords() and countBytes()?

For countBytes() how is that different from SplFileInfo's getSize() ?
As for countWords() and the others, I think this crosses the line (in
my view) over what a "File Object" should provide.

>
> Also, unicode issues aside, it might still be nice to implement
> countChars().
>
> And finally, so that it provides all most of the functionality of the GNU wc
> program, there could also be getMaxLineLength().  This would need a
> different name however as this method would be to count the longest line of
> the file rather than indicate the maximum length of a line to read as the
> current SplFileObject::getMaxLineLen() method does.
>
> Just my thoughts,
>
> Nick
>
>
> 
> > […]

Having not looked at the implementation yet, I have no comments on the
patch itself. However, I would prefer a "get" prefixed method name,
getLineCount() or similar.

> > […]

---

## #51691 — Sebastian Marek — 2011-03-15T10:02:08+00:00

URL: https://externals.io/message/51672#51691 · In reply to: #51690

Hello Nick,


Might be fun, I'll have a look at it. Is the stuff I have done so far any
good? I've got only a little C experience, so any feedback is good!

cheers



> Hi Sebastian,
>
> Since Etienne brought it up, would it not also make sense for you to
> implement countWords() and countBytes()?
>
> Also, unicode issues aside, it might still be nice to implement
> countChars().
>
> And finally, so that it provides all most of the functionality of the GNU
> wc program, there could also be getMaxLineLength().  This would need a
> different name however as this method would be to count the longest line of
> the file rather than indicate the maximum length of a line to read as the
> current SplFileObject::getMaxLineLen() method does.
>
> Just my thoughts,
>
> Nick
>
>
>
> 
>
> > […]
>
> --
> 
> 
>
>


-- 
Sebastian Marek
proofek@gmail.com

Follow me online at:
Blog: http://criticallog.thornet.net/
Twitter: http://twitter.com/proofek
Linkedin: http://uk.linkedin.com/in/sebastianmarek
Ohloh: http://www.ohloh.net/accounts/proofek
<http://twitter.com/proofek

---

## #51695 — Nick Pope — 2011-03-15T10:54:14+00:00

URL: https://externals.io/message/51672#51695 · In reply to: #51692

> For countBytes() how is that different from SplFileInfo's getSize() ?
> As for countWords() and the others, I think this crosses the line (in
> my view) over what a "File Object" should provide.

Hi Peter & Sebastian,

Bah.  It isn't.  Obviously that was going to exist already, but was 
thinking about methods looking like countXXXXX() too much :)

Perhaps you are right, Peter, on your opinions about what a "file 
object" should provide.  I would think, in that case, that a 
getLineCount() shouldn't exist on the "file object" either.

Currently we have:

     SplFileInfo  -- information/metadata about a file.
          |
          V
    SplFileObject -- modify/access contents of a file.

It could make sense to subclass this to provide methods for accessing 
information about the contents of a file.  As for a name, I'm not sure 
what would be best, but perhaps something like "SplFileData".  Maybe 
someone else can come up with a better name?

Then you could have the following methods:

getByteCount() -- the same as getSize(), but for here for consistency.
getCharCount()
getLineCount()
getWordCount()
getLongestLine()

Another consideration is how these should behave if we have a binary 
file - am not sure what GNU wc does, but perhaps that is a starting 
point for deciding what should happen.

Also thought might be given to what other useful methods could be 
provided for getting information about the contents of a file.  Some 
ideas (sensible or otherwise!):

getShortestLine()
getEolFormat()
setEolFormat() -- allow switching between LF, CR and CRLF.
getLineIndent() -- Could return '\t', '  ', '    ', etc.
setLineIndent()
hasEmptyLines()
hasTrailingWhiteSpace()
...

It may also make sense to cache the result while the file is not 
modified as some of these operations could be expensive.

Anyway - I'll stop rambling.  I think this would be better than just 
hacking in a method for the sake of it.

Cheers,

Nick

---

## #51696 — Alexey Zakhlestin — 2011-03-15T11:07:01+00:00

URL: https://externals.io/message/51672#51696 · In reply to: #51695

> Currently we have:
>
>    SplFileInfo  -- information/metadata about a file.
>         |
>         V
>   SplFileObject -- modify/access contents of a file.
>
> It could make sense to subclass this to provide methods for accessing
> information about the contents of a file.  As for a name, I'm not sure what
> would be best, but perhaps something like "SplFileData".  Maybe someone else
> can come up with a better name?

probably SplTextFileObject, as these methods make sense for text files


-- 
Alexey Zakhlestin, http://twitter.com/jimi_dini
http://www.milkfarmsoft.com/
