Refactoring of Kdyby Packages
Total Page:16
File Type:pdf, Size:1020Kb
Masaryk University Faculty of Informatics Refactoring of Kdyby packages Bachelor’s Thesis Filip Procházka Brno, Spring 2017 This is where a copy of the official signed thesis assignment and a copy ofthe Statement of an Author is located in the printed version of the document. Declaration Hereby I declare that this paper is my original authorial work, which I have worked out on my own. All sources, references, and literature used or excerpted during elaboration of this work are properly cited and listed in complete reference to the due source. Filip Procházka Advisor: RNDr. Jaroslav Bayer i Acknowledgement I would like to thank the Kdyby community that kept using the pack- ages even through I had no time to maintain them for almost year and a half. To my advisor RNDr. Jaroslav Bayer for helpful insights and in- spiring conversations. To my roommates and friends for surviving the constant whining about not getting enough sleep and having drank too much coffee. To my girlfriend and family for not letting megive up. And finally big thanks to my computer for surviving it till theend, even though for few days it looked like that my new SSD was dying again. ii Abstract The primary goal of this thesis is to provide up-to-date versions of Kdyby packages that are compatible with current versions of libraries they integrate into Nette. A significant portion of that is creating more maintainable and coherent code resulting in easier releasing of new package versions. A detailed roadmap was compiled that will be the basis of future architectural changes in Kdyby packages. A custom Kdyby Coding Standard was created. Versions of almost all of the more successful Kdyby packages were released that support current Nette, Doctrine, and Symfony. Moreover the code of most Kdyby packages is now checked with a static analysis tool and adheres to the strict coding standard. iii Keywords Kdyby, package, Packagist, Composer, Nette Framework, Doctrine ORM, ORM, Symfony Framework, integration, refactoring, clean code, coding style, coding standard, PHP iv Contents 1 Introduction1 2 Background for understanding Kdyby2 2.1 A brief history of Kdyby ....................2 2.2 Techniques and design patterns ................2 2.3 Technologies used .......................3 3 Current state of Kdyby9 3.1 State of the project .......................9 3.2 State of each package ..................... 10 4 Roadmap of refactoring 26 4.1 Deprecations ......................... 26 4.2 Common requirements .................... 27 4.3 Specific requirements for each package ............ 30 5 The refactoring process of Kdyby 34 5.1 Implementing PHPStan compiler ............... 34 5.2 Designing new Kdyby Coding Standard ........... 36 5.3 Refactoring summary ..................... 37 6 Conclusion and Future work 39 v 1 Introduction Kdyby is an Open-source software (OSS) project that I, Filip Prochá- zka, lead and maintain. It is a set of PHP [1] libraries, which aim to ease the writing of web applications. Through my career, I have used Kdyby in core business appli- cations of companies such as Damejidlo.cz and Rohlik.cz. A lot of people consider my work useful enough to incorporate it into their applications as well. As of writing this, the more successful libraries have hundreds of thousands of downloads. Five of Kdyby libraries have over quarter million downloads, and one is approaching half a million with a staggering amount of 497 thousands of downloads1. In conclusion, a sober estimate would be, that hundreds of real production applications use Kdyby libraries. If I account only for the two largest projects that I can confirm are using Kdyby packages, over a billion Czech crowns2 has flown through Kdyby. Over the years, I have had problems keeping up with the demand and the packages began to get obsolete. Therefore the primary goal of this thesis is to resolve compatibility of Kdyby packages with new versions of libraries they integrate with, fix as many bugs as possible and increase the quality of their code. I will also review the state of each library and decide its future. I will either deprecate it and provide the users a suggestion for a better alternative, or fix the problems and refactor the library. 1. https://packagist.org/packages/kdyby/ 2. Rohlik.cz loni prodal zboží za miliardu, letos chce konečně zisk http://tyinternety.cz/e-commerce/rohlik-cz-loni-dosahl-na- miliardovy-obrat-letos-chce-konecne-zisk/ 1 2 Background for understanding Kdyby 2.1 A brief history of Kdyby In 2006 I started working on my own Content management system (CMS). A prototype was used in production on few websites I created. The oldest preserved version is archived on my GitHub1. It is a great learning material on how to not write a CMS. Then the concept of OSS was introduced to me, and I have decided to start working on everything openly, under a free license [2]. No new working version of Kdyby CMS was released since then because I have rewritten it from scratch exactly ten times. In 2012, I decomposed the entire system into separate libraries that can be used more or less independently and have their own release cycle. This approach was preserved to this day. 2.2 Techniques and design patterns 2.2.1 Dependency Injection Inversion of control is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework. Dependency Injection (DI) is a technique whereby one object sup- plies the dependencies of another object. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern [3]. 2.2.2 Aspect Oriented Programming Complex systems have to deal with a lot of other problems that are not the center of the business logic, but also important. This might be for example transaction handling or logging. Aspect-oriented Pro- gramming (AOP) is a paradigm that aims to increase modularity by helping to separate these cross-cutting concerns. It allows changing 1. https://github.com/fprochazka/kdyby-cms-old 2 2. Background for understanding Kdyby behavior of existing code without modifying the code itself using ad- vices that are applied using pointcut specification [4]. A good example is a Transactional annotation in Java world. A pointcut is defined to bind to every method with such annotation and the advice wraps the method in a transaction. 2.2.3 Observer pattern Observer a pattern for writing modular code. It allows creating ex- tension points in the library or application that another library or application can hook into and change or extend the behavior. Typ- ically, the extension points are called hooks or events, and the new functionality is provided with objects called listeners [5]. Symfony provides an implementation called Symfony Framework EventDispatcher Component (Symfony EventDispatcher) that the sub- scriber implementations can listen on. 2.3 Technologies used 2.3.1 Git and GitHub Git is a Version Control System that is decentralized and considered very fast [6]. GitHub is a collaboration platform for software develop- ment using Git. Each project has a page on GitHub called a repository that can be used to inspect the Git history, files, and other metadata. On the project repository page, there are issues and pull requests. Pull requests are a way to ask the maintainer of the repository to incorporate provided code patch to the repository. It can be a bugfix or feature. There are tools around pull requests that allow collaboration, a code review, and discussion about the provided code so that the main- tainers can help the contributors to provide the best code possible. Kdyby is hosted and developed on GitHub, with the aid of several other maintainers and the community that contributes bugfixes and features. 3 2. Background for understanding Kdyby 2.3.2 Continuous Integration Continuous Integration (CI) is a practice of merging all developer working copies to a shared mainline several times a day, to prevent merging conflicts [7]. But nowadays, the term has established to mean CI servers that run prepared task on the provided code. In practice, it means that as the developer is working on a feature or bugfix, they push the work in progress code into a repository, the code is then picked up by a CI server that executes the tests, checks coding style and runs various other tasks to verify that the code was not broken. When the work is finished and all the task on CI server completed with success, the code can be probably safely integrated, providing that the tests for new or changed functionality were added. Some popular CI services are Travis CI2, CircleCI3 and GitLab CI4. Kdyby is using the Travis CI that is free for OSS projects. 2.3.3 Semantic Versioning Semantic Versioning (SemVer) is a standard that defines how software should be versioned to allow safe upgrading. An application might be written with a particular release of the library and upgrading to the newest version might break it because of dropped compatibility. SemVer5 defines major, minor and patch releases that signal what versions are compatible with each other. 2.3.4 Nette Framework Nette Framework (Nette) is an OSS framework for creating web appli- cations in PHP. Nette is separated into many packages [8]. The DI component nette/di6 provides a Dependency Injection Container (DIC) that holds the services. The component also allows preparing a preconfigured DIC, which is then compiled into aPHP 2. https://travis-ci.org/ 3. https://circleci.com/ 4. https://about.gitlab.com/features/gitlab-ci-cd/ 5. http://semver.org/ 6. https://github.com/nette/di 4 2. Background for understanding Kdyby class that contains optimized code for the service creation.