Total lines of code

My company considers purchasing Sonar (https://www.sonarsource.com/plans-and-pricing/). And we need to estimate LOC (lines of code).

Can someone tell me how many lines of code (all files, including .js, .html etc.) in total has a typical out-of-the box application of:

  • Yii 2 Basic,
  • Yii 2 Advanced,
  • Yii 3

Or can someone advice some Windows-based tools that can calculate total number of lines in all files in selected folder? Thank you.

I don’t how reliable is the method that I’ve found (Count all lines of an IT project – onezeronull.com), but using it I managed to estimate that for Yii 2 we have:

  • Yii 2 Basic: 171 + 23 849 + 20 3474 + 619 = 228 113 lines of code
  • Yii 2 Advanced: 652 + 256 006 + 1 267 + 143 = 258 068 lines of code

So typical Yii 2 application has close to or around quarter of million lines of code.

These numbers are excluding vendor folder naturally (so framework files itself any many external libraries). If we would like to include them as well, we would need to add two million lines of code to the above numbers.

It depends very much on the application.

I am asking and performing my checks on bare-bone Yii 2 app as taken from the official repository using composer create-project.

Thus, I believe that this isn’t very depended and these numbers will change slightly only during future updates of the repository (given the fact that there will be many updates since you are all focusing on Yii 3).

As written in the introduction, this question was initiated by deciding on which Sonar’s package to purchase. Sonar has one of the packages set at 250k LOC (lines of code) with 500k LOC and 1M LOC following.

Based on the above calculations I can surely assure myself that 250k LOC is not enough, that 500k LOC is a minimum and 1M LOC must be considered. That’s pretty much all that I have expected. Thanks!

What about writing a Python script to do that?
It should be doable and will give accurate information than estimate which depends on actual code being analyzed

What about writing a Python script to do that?

Sounds good, although not available to me → I don’t know Python.

It should be doable and will give accurate information than estimate which depends on actual code being analyzed

Depends on what you understand as “accurate information”. This solution (though a bit unhandy) counts the exact number of lines of code in each and every file by counting total number of line delimiters used in all three systems.

I’d say that this is pretty much accurate.

Easy to learn or hire someone to do it
Here is a script that breaks lines by semi colon. So counts by semicolon as only criteria. You can expand it to check braces for example if you so wish. You can combine it with directory traversing as well as some fixed ignored paths to get a complete solution

Also you can use Yii2 command controller to accomplish the same. PHP have rich directory traversing functions that you can use.

All the best!


#full Path to file
path = '...'
lineCount = 0
with open(path) as file: 
    for codeLine in file.read().replace('\n', '').split(';'):
        if codeLine.strip():
            lineCount +=1
            print('++++++++'+str(codeLine))

print("Total Line count is: ", str(lineCount))

Hi Stefano, thanks for your time and for sharing this with me and others. I’ll definitely consider using this.

BTW: Splitting by semicolon is not an option in this scenario. If I ever decide to pay an enormous fee for Sonar, I will expect it to validate security of all my files, including HTML and Javascript, not just PHP code.

These files are split by line delimiter rather than semicolon.

Why Python if PHP would do :slight_smile:

1 Like

Preference :grinning:
I find Python easier to do scripting tasks like this.

I wonder how can LOC relate to project value.
If I would copy same line 1 million times - will it be 4x more valuable than Yii2 app?
Quality of the code & added value - that’s what matters, not the number of lines.
You can have 50% of remarks in the code, shitty / buggy code … etc
At least, I would recommend some complexity measuring tools - e.g. https://phpmetrics.org/ to check with.

I don’t think that LoC relates to project value. For the reasons that you have outlined and for many more.

I need LoC to purchase an accurate package of Sonar. Differences between packages in terms of price are huge, so I appreciate and consider every method of measuring LoC provided by members here.

I also think that LoC tells you nothing. But, that’s how Sonar builds their business.

One of my project totally 44k LOC. When you are using correctly the framework. Use dependency manager and exclude vendor directory, it is enough for version control…

image