How to measure # of lines of code in project? - php

How can I measure number of lines of code in my PHP web development projects?
Edit: I'm interested in windows tools only

Check CLOC, it's a source code line counter that supports many languages, I always recommend it.
It will differentiate between actual lines of code, blank lines or comments, it's very good.
In addition there are more code counters that you can check:
SLOCCount
sclc
USC's CODECOUNT
loc
Ohcount

phploc
No experience if it runs on Windows, tho, but searching on the web showed some results of people getting it to work on Windows..

If you are on a linux box, the easiest way is probably directed by this SO question:
count (non-blank) lines-of-code in bash

Our SD Source Code Search Engine is a GUI for searching across large bodies of source code such as a PHP web site. It is fast because it preindexes the source code.
As a side effect of the indexing process, it also computes metrics on the source code base, including SLOC, Comments, blank lines, Cyclomatic and Halsted complexity numbers.
And it runs on Windows.

If you are using VS code, install this extension called Lines of Code (LOC)
After installation, use ctrl+p, type linecount, and you will find below options.
Count Workspace files will give the output in the following format.
This works on any OS with VS Code with the above plugin installed.

npm install -g cloc
Go to the project folder
Delete the node_modules and dist folder (if you have any), Because you don't need to calculate loc in dist and node_modules
inside the project folder
run this command: cloc *
Output will look like this :

If you're using a full fledged IDE the quick and dirty way is to count the number of "\n" patterns using the search feature (assuming it supports regexes)

Related

PHP as independent application ( binary, compile, pack, no php on host )

If I would like to distribute PHP application with installer(package system of OS) how should I proceed? I don't want PHP files to be there, just working application, so when I type 'app' into console, it ends up being launching application, without need to install PHP on system(no php installation on host required). I would also like the application to have patch-able byte-code, so it's in parts, loaded when needed and only part needs to be replaced on update.
What I would do now is following:
->Compile PHP with extensions for specific platform.
->Make binary application which launches '/full/php app' when app is launched.
->Pack it in installer in a way, that there would be binary added to path when added, launching specific installation of PHP which is alongside the app with argument of start point->App would be running.
Problem is:
Maybe I don't want my PHP files to be exposed(in application, there will be available source anyway) is there some ready made stuff to do this? Is there some better way than I proposed?
Alternative: Modifying OP Cache to work with "packing" application to deliver byte codes to modified OP Cache which just reads the cache.
My suggestion would be a tiny tool I just finished, for almost exactly the same problem. (Oh yes I tried all the others but they're old and rusty, sometimes they're stuck with 4.x syntax, have no support, have no proper documentation, etc)
So here's RapidEXE:
http://deneskellner.com/sw/rapidexe
In the classical way, it's not a really-real compiler, just a glorified packer, but does exactly what you need: the output exe will be standalone, carrying everything with it and transparently building an ad-hoc runtime environment. Don't worry, it all happens very fast.
It uses PHP 7.2 / Win64 by default but has 5.x too, for XP compatibility.
It's freeware, obviously. (MIT License.)
(Just telling this because I don't want anyone to think I'm advertising or something. I just took a few minutes to read the guidelines about own-product answers and I'm trying to stay within the Code of the Jedi here.)
However...
I would also like the application to have patch-able byte-code, so it's in parts, loaded when needed and only part needs to be replaced on update.
It's easier to recompile the exe. You can extract the payload pieces of course but the source pack is one big zip; there seems to be no real advantage of handling it separately. Recompiling a project is just one command.
Maybe I don't want my PHP files to be exposed(in application, there will be available source anyway)
In this case, the exe contains your source compressed but eventually they get extracted into a temp folder. They're deleted immediately after run but, well, this is no protection whatsoever. Obfuscation seems to be the only viable option.
If something goes wrong, feel free to comment or drop me a line on developer-at-deneskellner-dot-com. (I mean, I just finished it, it's brand new, it may misbehave so consider it something like a beta for now.)
Happy compiling!
PHP doesn't do that natively, but here are a few ideas:
Self-extracting archive
Many archival programs allow you to create a self-extracting archive and some even allow to run a program after extraction. Configure it so that it extracts php.exe and all your code to a temp folder and then runs ir from there; deleting after the script has complete.
Transpilers/compilers
There's the old HPHC which translates PHP code to C++, and its wikipedia age also contains links to other, similar projects. Perhaps you can take advantage of those.
Modified PHP
PHP itself is opensource. You should be able to modify it withot too much difficulty to take the source code from another location, like some resource compiled directly inside the php.exe.
Use Zend Guard tool that compiles and converts the plain-text PHP scripts into a platform-independent binary format known as a 'Zend Intermediate Code' file. These encoded binary files can then be distributed instead of the plain text PHP. Zend Guard loaders are available for Windows and Linux platform that enables PHP to run the scripts encoded by Zend Guard.
Refer to http://www.zend.com/en/products/zend-guard
I would like to add another answer for anyone who might be Googling for answers.
Peach Pie compiler/runtime
There is an alternative method to run (and build apps from) .php source codes, without using the standard php.exe runtime. The solution is based on C#/.NET and is actually able to compile php source files to .NET bytecode.
This allows you to distribute your program without exposing its source code.
You can learn more about the project at:
https://www.peachpie.io/
You've got 3 overlapping questions.
1. Can I create a stand-alone executable from a PHP application?
Answered in this question. TL;DR: yes, but it's tricky, and many of the tools you might use are semi-abandoned.
2. Can I package my executable for distribution on client machines?
Yes, though it depends on how you answer question 1. If you use the .Net compiler, your options are different to the C++ option.
3. Can I protect my source code once I've created the application?
Again, depends on how you answer question 1. Many compilers include an "obfuscator" option which makes it hard to make sense of any information you get from decompiling the app. However, a determined attacker can probably get through that (this is why software piracy is possible).

Best way to append changes to web application [duplicate]

I'm trying to make sense on the best way to do automatize a series of things in a row in order to deploy a web-app and haven't yet came up with a suitable solution. I would like to:
use google's compiler.jar to minify my JS
use yahoo's yui-compressor.jar to minify my CSS
access a file and change a string so that header files like "global.css?v=21" get served the correct version
deploy the app (sftp, mercurial or rsync?) omitting certain directories like "/userfiles"
Can you guys put me on the right track to solve this?
Thank you!
you may want to check out phing http://phing.info/ (they are in the process of moving servers so may be down this weekend), but it can do all of what you are wanting and is written in php.
A quick google search should bring up plenty of tutorials to get you started.
You can run php from the command line to do all sorts of fun things.
$ php script_name.php arg1 arg2
See: command line, argv, argc, exec
Running PHP from the command line is very fast. I've been doing this a lot lately for various automation tasks.
I generally run Python projects so this may or may not be an option for you: but apart from writing your own scripts you could look into the following:
Fabric
Buildout
maven

How to find a correct php file to edit in large live running web system?

I have a huge live running web site wich has lot of issues. This has developed by lot of programmers around the world. So If I want to fix some errors its really hard to find where it is and so on....These files are in zend framework. There are 100s of php files, css files java sripts files, etc. Is there any way that I can do global search a file or how to understand the file linkage of this kind of large systems?
If you are using Linux there are a bunch of ways to find the correct file -
On the command line
Find a file by name -
find . -name filename
Or the faster find a file by name -
sudo updatedb #Only needed the first time you run locate
locate filename
Search a directory's contents recursively for a string -
grep -ri somestring .
Here's a new trick I learnt recently - Using Ack! If you are on a Debian/Ubuntu system you can install ack by doing -
sudo apt-get install ack-grep
Then you can do -
ack-grep somestring
This is blazing fast!
In IDEs
In most IDEs or advanced editors you should be able to simply control click or right click and select "go to definition" to navigate to a function's definition.
I can attest that this works in Netbeans, Eclipse, Komodo, Emacs etc.
If you are open to adopting a new IDE, I suggest Netbeans. It's PHP mode is full of goodies including improved navigation. For example, to quickly open a file you can simply do Alt-Shift-O and type the filename.
First of all I think you'll need to learn a bit about Zend Framework, how it's MVC framework works and where the models, views and controllers are stored. This'll help you guess where to go looking for problems.
In terms of searching for specific lines of code or patterns most operating systems or IDEs will allow you perform a recursive search of a set of files. I think that's outside the scope of this site.
if you are using aptana you can search your whole workspace by going to search->search. If you are using dreamweaver you can open find menu and search through your current site
It will be pretty hard to locate the right file in a quick way if you don't have any knowledge about the application at all. I would recommend reading up on Zend Framework and how it's working.
debug_backtrace() can come in handy if you need to show a backtrace. This can be useful if you need to know from where a function has been called.
If you're searching for a file that outputs something, a simple "search all directories, recursive" search will probably do just fine. Most IDEs (even "simple" one like Notepad++) or even OSes have this built in.

Is there such a thing as a converter from php to html?

Don't think that I'm mad, I understand how php works!
That being said. I develop personal website and I usually take advantage of php to avoid repetion during the development phase nothing truly dynamic, only includes for the menus, a couple of foreach and the likes.
When the development phase ends I need to give the website in html files to the client. Is there a tool (crawler?) that can do this for me instead of visiting each page and saving the interpreted html?
You can use wget to download recursively all the pages linked.
You can read more about this here: http://en.wikipedia.org/wiki/Wget#Recursive_download
If you need something more powerful that recursive wget, httrack works pretty well. http://www.httrack.com/
Pavuk offers much finer control than wget. And will rewrite the URLs in the grabbed pages if required.
If you want to use a crawler, I would go for the mighty wget.
Otherwise you could also use some build tool like make.
You need to create a file nameed Makefile in the same folder of your php files.
It should contain this:
all: 1st_page.html 2nd_page.html 3rd_page.html
1st_page.html: 1st_page.php
php command
2nd_page.html: 2nd_page.php
php command
3rd_page.html: 3rd_page.php
php command
Note that the php command is not preceded by spaces but by a tabulation.
(See this page for the php command line syntax.)
After that, whenever you want to update your html files just type
make
in your terminal to automatically generate them.
It could seem a lot of work for just a simple job, but make is a very handy tool that you will find useful to automate other tasks as well.
Maybe, command line will help?
If you're on windows, you can use Free Download Manager to crawl a web-site.

How to auto-deploy web-app

I'm trying to make sense on the best way to do automatize a series of things in a row in order to deploy a web-app and haven't yet came up with a suitable solution. I would like to:
use google's compiler.jar to minify my JS
use yahoo's yui-compressor.jar to minify my CSS
access a file and change a string so that header files like "global.css?v=21" get served the correct version
deploy the app (sftp, mercurial or rsync?) omitting certain directories like "/userfiles"
Can you guys put me on the right track to solve this?
Thank you!
you may want to check out phing http://phing.info/ (they are in the process of moving servers so may be down this weekend), but it can do all of what you are wanting and is written in php.
A quick google search should bring up plenty of tutorials to get you started.
You can run php from the command line to do all sorts of fun things.
$ php script_name.php arg1 arg2
See: command line, argv, argc, exec
Running PHP from the command line is very fast. I've been doing this a lot lately for various automation tasks.
I generally run Python projects so this may or may not be an option for you: but apart from writing your own scripts you could look into the following:
Fabric
Buildout
maven

Categories