Skip to main content
Start of main content.

Analysing churn in Drupal core

by lee.rowlands /

Share this post on social media

After reading a blog post by Matthias Noback on keeping an eye on code churn, I was motivated to run the churn php library over some modules in core to gauge the level of churn.

Is this something you might like to do on your modules? Read on for more information.

What is churn

As Matthias details in his blog post - churn is a measure of the number of times a piece of code has been changed over time. The red flags start to crop up when you have high complexity and high churn.

Enter churn-php

Churn php is a library that analyses PHP code that has its history in git to identify high churn/complexity scores.

You can either install it with composer require bmitch/churn-php --dev or run it using docker docker run --rm -ti -v $PWD:/app dockerizedphp/churn run /path/to/code

Some results from core

So I ran it for some modules I look after in core, as well as the Drupal\Core\Entity namespace.

Block Content

FileTimes ChangedComplexityScore
core/modules/block_content/src/Entity/BlockContent.php4161
core/modules/block_content/src/BlockContentForm.php3260.78
core/modules/block_content/src/Plugin/Block/BlockContentBlock.php2060.488
core/modules/block_content/src/Tests/BlockContentTestBase.php1660.39
core/modules/block_content/src/BlockContentTypeForm.php1840.347
core/modules/block_content/src/Controller/BlockContentController.php860.195

Comment

FileTimes ChangedComplexityScore
core/modules/comment/src/CommentForm.php60451
core/modules/comment/src/Entity/Comment.php55250.548
core/modules/comment/src/Tests/CommentTestBase.php33290.426
core/modules/comment/src/Controller/CommentController.php32200.274
core/modules/comment/src/CommentViewBuilder.php37160.25
core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php32180.24
core/modules/comment/src/Form/CommentAdminOverview.php29170.191
core/modules/comment/src/CommentAccessControlHandler.php17280.19
core/modules/comment/src/CommentLinkBuilder.php15290.17
core/modules/comment/src/CommentManager.php29150.157

Drupal\Core\Entity

FileTimes ChangedComplexityScore
core/lib/Drupal/Core/Entity/ContentEntityBase.php1151730.808
core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php611960.465
core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php562030.427
core/lib/Drupal/Core/Entity/Entity.php131430.212
core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php411050.16

Conclusion

So, what to do with these results?

Well I think if you're looking to simplify your code-base and identify places that would warrant refactoring, those with a high 'churn' score would be a good place to start.

What do you think? Let us know in the comments.