phpSoCo, for counting php lines of code

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Hrmm.. I`m not sure I downloaded it straight from http://code.google.com/p/redwidow-dna/downloads/list

I don`t have time to investigate right now, but I certainly will in the morning (unless you feel like doing a bit more poking around) :D
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

:P

Code: Select all

Statistic                       Value             
--------------------------------------------------
- Total Files                   1                 
- Total Lines                   1                 
- Total Characters              5030              
- Average Lines                 1                 
- Average Characters            5030              
- Total Classes                 0                 
- Total Functions               0                 

Applied Filters
--------------------------------------------------
- A_Utility_Loc_Filter_RemoveNonPHP
- A_Utility_Loc_Filter_RemoveBlank
- A_Utility_Loc_Filter_RemoveComment
- A_Utility_Loc_Filter_RemoveOpenBrace

Loaded Files
--------------------------------------------------
No files

Code: Select all

$linecount = new A_Utility_Loc();

#$linecount->addSource(new A_Utility_Loc_Iterator_Directory('C:\apache2\apache2\htdocs\smp\class'));
$linecount->addSource(new A_Utility_Loc_Iterator_File('C:\apache2\apache2\htdocs\smp\class\hash_base.php'));

$linecount->addFilter(new A_Utility_Loc_Filter_RemoveNonPHP);
$linecount->addFilter(new A_Utility_Loc_Filter_RemoveBlank);
$linecount->addFilter(new A_Utility_Loc_Filter_RemoveComment);
$linecount->addFilter(new A_Utility_Loc_Filter_RemoveOpenBrace);

$linecount->addCounter(new A_Utility_Loc_Counter_TotalFiles, 'Total Files');
$linecount->addCounter(new A_Utility_Loc_Counter_TotalLines, 'Total Lines');
$linecount->addCounter(new A_Utility_Loc_Counter_TotalCharacters, 'Total Characters');
$linecount->addCounter(new A_Utility_Loc_Counter_AverageLines, 'Average Lines');
$linecount->addCounter(new A_Utility_Loc_Counter_AverageCharacters, 'Average Characters');
$linecount->addCounter(new A_Utility_Loc_Counter_TotalClasses, 'Total Classes');
$linecount->addCounter(new A_Utility_Loc_Counter_TotalFunctions, 'Total Functions');

echo '<pre>' . $linecount->report(new A_Utility_Loc_Reporter_CLI) . '</pre>';

Code: Select all

C:\Users\scott>php -r "var_dump($a = is_file('c:\apache2\apache2\htdocs\smp\clas
s\hash_base.php'));"
bool(true)

C:\Users\scott>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Good catch!

I've patched this method in A/Utility/Loc/Iterator.php

Code: Select all

protected function getFileContents($filepath) 
	{
		return str_replace(array("\r\n", "\r"), PHP_EOL, file_get_contents($filepath));
	}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Jcart wrote:Good catch!

I've patched this method in A/Utility/Loc/Iterator.php

Code: Select all

protected function getFileContents($filepath) 
	{
		return str_replace(array("\r\n", "\r"), PHP_EOL, file_get_contents($filepath));
	}
I don't think that's going to work :P unless you add in \n to be replaced, too. Say the target system has \r line endings. It'll replace \r\n and \r with \r line endings, leaving \n line endings all over the place in files.

You could either

Code: Select all

return str_replace(array("\r\n", "\r", "\n"), PHP_EOL, file_get_contents($filepath));
or

Code: Select all

return str_replace(array_diff(array("\r\n", "\r", "\n"), array(PHP_EOL)), PHP_EOL, file_get_contents($filepath));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

look at that :P scottayy's coding critique became JCart's coding critique :P Sorry about that.. just thought I'd share an inconvenience I had in my library.

There's an oodle of parts of your code I don't understand. And to be quite honest, I'm tired of learning the way I have been learning. Trial & error, going into it blindly. I think I'd like a hardcover PHP OOP book, so I could sit back and soak in the ideas before blindly coding a mess that I think is nice.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Getting a bit late here :D

I had originally thought I was using your first example there.. but yes you are correct and have updated the code.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

scottayy wrote:look at that :P scottayy's coding critique became JCart's coding critique :P Sorry about that.. just thought I'd share an inconvenience I had in my library.

There's an oodle of parts of your code I don't understand. And to be quite honest, I'm tired of learning the way I have been learning. Trial & error, going into it blindly. I think I'd like a hardcover PHP OOP book, so I could sit back and soak in the ideas before blindly coding a mess that I think is nice.
Again, appologies for the thread derailment, I had a feeling this would happen .. hense my appology earlier :D

Feel free to ask any questions about the code, or the theory behind it. I would be happy to chat on a messenger as well if you think that will help.

But yes, you should certainly should find a book. They help oh so much.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Jcart wrote:
scottayy wrote:look at that :P scottayy's coding critique became JCart's coding critique :P Sorry about that.. just thought I'd share an inconvenience I had in my library.

There's an oodle of parts of your code I don't understand. And to be quite honest, I'm tired of learning the way I have been learning. Trial & error, going into it blindly. I think I'd like a hardcover PHP OOP book, so I could sit back and soak in the ideas before blindly coding a mess that I think is nice.
Again, appologies for the thread derailment, I had a feeling this would happen .. hense my appology earlier :D

Feel free to ask any questions about the code, or the theory behind it. I would be happy to chat on a messenger as well if you think that will help.

But yes, you should certainly should find a book. They help oh so much.
Recommendations for PHP OOP? I'd like reading -- with examples of usage -- but I'm not so much into the thing where you copy and paste code examples on your computer. I'd like a book for just reading with usage examples.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

A good place to start is this freely available book online

PHP5 Power Programming

Then..
sweatje wrote:Here was a list of Book References I put into my book:


GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.
PoEAA - Martin Fowler Patterns of Enterprise Application Architecture Addison-Wesley, 2003.

• Harry Fuecks, PHP Anthology: Object Oriented PHP Solutions, SitePoint Pty. Lt., 2003

• Allen Holub, Holub on Patterns: Learning Design Patterns by Looking at Code, Apress, 2004.

• Robert Martin, Agile Software Development, Prentice Hall, 2003.

• Clifton Nock, Data Access Patterns, Addison-Wesley, 2004.

• George Schlossnagle, Advanced PHP Programming, SAMS, 2004

• Alan Shalloway and James R. Trott, Design Patterns Explained, Addison-Wesley, 2005

• Rebecca Wirfs-Brock and Alan McKean, Roles, Responsibilities and Collaborations, Addison-Wesley, 2003.

• Matt Zandstra, PHP 5 Objects, Patterns, Practice, Apress, 2004.

Clearly not all of these are PHP only, but they are all great books for improving your overall programming knowledge.
Post Reply