Hot answers tagged

11

There are some posibilites, how to mock static methods in PHP, the best solution I have used is the AspectMock library, which can be pulled through composer (how to mock static methods is quite understandable from the documentation). However, it's a last-minute fix for a problem which should be fixed in a different way. If you still want to unit test the ...


8

I'd prefer Option #4: Avoid relying on inheritance. Just make UrlBuilder an interface, and give people some way to inject their own implementation to the process, overriding the default you provided. interface UrlBuilder { public function url($config); } class DefaultUrlBuilder implements UrlBuilder{ public function url($config) { $config = ...


4

First, I would suggest to split this up into separate methods: public function validate($value, Constraint $constraint) { $totalCount = QueryTotal($value); ShowMessageWhenTotalExceedsMaximum($totalCount,$constraint); } private function QueryTotal($value) { $searchEntity = EmailAlertToSearchAdapter::adapt($value); $queryBuilder = ...


3

I usually keep tests and src completely independent, and not nested under each other. Sources and test sources are typically defined like that. In an application, I usually have following: module /Application ... /src /Form Form.php // Application\Form\Form /test /unit ...


3

Technically, there is no quicker or slower alternative: it's the same request to the same resource, and the processing and sending of the image would probably take much more time than the displaying on client side. The only difference is the perception by the user, with a major difference between <img> and background-image. If <img> doesn't ...


2

There's no single explicit approach here for making "perfect" mail templates, as everything depends on the complexity of your templates and scalability you want to achieve. Through my experience, I've been working with different approaches, and all of them served well for the specific project needs. It's matter of personal taste and skill, one have to find ...


2

Neither approach is functionally better: the both do the job (except the self keyword would need to go in your namespace-based example). So the difference lies essentially in clarity and maintainability. If your intention is that none of these functions have any shared state whatsoever, I think that clearest approach is the one that explicitly forbids it: ...


2

Here are my two cents: Because the question is too big, I will restrict my points of view to two specific situations. First Scenario: For Fun Project: no need to share on the internet, just some testing. The goal is to learn something. For that specific case, I personally think you can handle everything with a functional approach, as long as you ...


1

You are running up against the challenges inherent to an ORM. ORM's are by definition leaky abstractions. Martin Fowler has an excellent article addressing your "moral and computer-sciency issues". http://martinfowler.com/bliki/OrmHate.html The truth is, ORM's are a pain and never feel right, especially for purists. This is because quite simply they are not ...


1

It is all a bit of semantics. A service would be a helper class, or in a sense a helper class would be a service. In terms of Laravel. Creating a service (which is a specific thing) would help code reuse and make testing easier. It is well worth getting over the hump of learning how to do. The number of services you need tends to grow as do the ...


1

I'm going to answer my own question after a day of research. In the end this ended up looking more towards cron jobs which support MVC web applications (which isn't exactly the same as my original question but yielded some interesting information none-the-less). Anyway, here's what I found: Q. The concept of routing all requests to one file is a little ...


1

First off, even though PHP is class case insensitive I would rewrite EXCEPTION to Exception as it's easier to read. Also, you're missing a few semi colons. I would fix those few things first! I'm not very familiar with the the Laravel database classes, but I assume you're using Eloquent? If so you may be able to catch the \Illuminate\Database\QueryException ...


1

This is the question as I understand it: Is there a superior way to classify files that contain code? Here are two example classifications. Not really. In my experience, both approaches are valid. The "better approach" depends on the codebase, the organization and possibly personal preference. Smaller/newer projects start off and benefit from the first ...


1

Actually this is matter of cohesion. From wikipedia: In computer programming, cohesion refers to the degree to which the elements of a module belong together. You can check this article about cohesion: https://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 For me the modular approach, as you call it is representation of functional cohesion, ...


1

A good rule is to avoid having objects that are in a illegal state. Therefore creating objects based on data from the database that is illegal (or indeed has become illegal because you have changed the rules) without anyone knowing about it would be bad. It would of course also be bad if noone were able to start your system because one record somewhere ...


1

The question is already a bit aged, but maybe someone still stumbles upon it. Most of the mvc-based PHP frameworks offer a feature called scaffolding. With this feature you can easily create simple masks for CRUD operations. Instead of ids ususally a display-field is used for defining relations. I often use CakePHP to achieve what you describe, but it ...


1

The examples shown is correct, but Zend\Form is not exactly what you want to use in your controllers. I'm talking about Exhibit A, where it shows how much code to create, populate and style a form with two elements. It is tedious to create forms that way. To avoid turning the controller into a form factory, you need to create a factory that will create the ...



Only top voted, non community-wiki answers of a minimum length are eligible